简体   繁体   中英

Issue with converting PDF pages to images with ImageMagick's convert (and PHP)

I'm trying to create a small web service to convert PDF files to a series of images.

When I run convert /full/path/to/source.pdf full/path/to/target.jpg when connected to the [Mediatemple (gs)] server via ssh, everything works correctly.

When executing the same command through PHP's exec() function, only the first few pages of the PDF file get converted. Sometimes the remaining pages are 0-length jpg files, sometimes they don't appear at all.

Also, the bottom area of the first pages is sometimes black, as if convert stopped half-way on the page.

What could be causing this problem?

UPDATES:

I'm using set_time_limit(0).

If I'm connected via SSH, and run the command convert -limit area 90 ... , it completes successfully in approx. 30 seconds. When doing the same via PHP-s exec(), it runs for ~90 seconds and produced broken images (as described above) as output.

Is there a simple way to make the convert task run with the same privileges and resources as from SSH?

Also, since it must be really frustrating working around a server problem I have little control over -- is there a good existing web service (API) that would convert PDFs to images?

Re your update: Can you try converting a tiny small PDF to see whether it's a memory issue after all?

Also, ImageMagick has a debug command line option:

convert -debug "All" /full/path/to/source.pdf full/path/to/target.jpg

I think it debugs to stdout, you would have to catch what it logs and output it. Manual here

And another thing, I think to convert PDFs, ImageMagick needs to call ghostscript (I think the binary is gs ). Can you try and make sure that is callable with the rights of the user PHP runs as? ImageMagick should return an error if it isn't, but you never know.

Gave up trying to get it work on a shared server, and moved the app to another server - everything working perfectly now.

I believe that the server resource limits (mainly some memory limit) was interfering with ImageMagick, and did so in a very inconsistent manner. Limiting ImageMagick's memory use still produced errors, and failed to convert files successfully.

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