简体   繁体   中英

PDF to JPG with PHP unexpected behaviour

I'm using Imagick to convert all pages in a PDF to JPGs. But something is off, probably because of my lack of PDF knowledge.

The problem
The PDF I'm converting is a book. All pages turns out great except the cover page/first page. If I open the pdf in a PDF previewer it looks as it should but my converted image displays the cover page + a backside of the book. If I open the first page of the PDF in Illustrator I get the same result as my image. I also have a problem with some pdf's containing margins which is not displayed in a PDF previewer but as whitespace in my images.

在此输入图像描述

But when converted to an image, the backside is white. It's like it doesn't exist but the image dimensions equals both of them.

Cause and Solution
Why does this happen and can I ignore the back somehow? Or should this be fixed in the PDF? Shit in shit out...

Code example

//...
$imagick->readImage('book.pdf[0]');
//...
file_put_contents('cover.jpg', $imagick):

Possible solution
Seems like if I use trimImage I get the expected result. So I have a bonus question. How does trimImage work? How does it know what to trim, let's say for example if I have a white image with text in the middle?

I managed to solve this and the solution was really simple. The solution is to use cropbox . You can set Imagick options by using setOption .

$imagick->setOption('pdf:use-cropbox', 'true');

Available options can be found here: http://www.imagemagick.org/script/command-line-options.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM