简体   繁体   English

使用ImageMagick的convert(和PHP)将PDF页面转换为图像的问题

[英]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. 我正在尝试创建一个小型Web服务,以将PDF文件转换为一系列图像。

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. 当我通过ssh连接到[Mediatemple(gs)]服务器时,运行convert /full/path/to/source.pdf full/path/to/target.jpg时,一切正常。

When executing the same command through PHP's exec() function, only the first few pages of the PDF file get converted. 通过PHP的exec()函数执行同一命令时,仅转换PDF文件的前几页。 Sometimes the remaining pages are 0-length jpg files, sometimes they don't appear at all. 有时其余页面是0长度的jpg文件,有时它们根本不显示。

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). 我正在使用set_time_limit(0)。

If I'm connected via SSH, and run the command convert -limit area 90 ... , it completes successfully in approx. 如果我通过SSH连接,并运行命令convert -limit area 90 ... ,则它会在大约30 convert -limit area 90 ...成功完成。 30 seconds. 30秒。 When doing the same via PHP-s exec(), it runs for ~90 seconds and produced broken images (as described above) as output. 通过PHP-s exec()执行相同操作时,它将运行约90秒,并产生损坏的图像(如上所述)作为输出。

Is there a simple way to make the convert task run with the same privileges and resources as from SSH? 有没有简单的方法可以使convert任务以与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? 此外,由于解决服务器问题确实很令人沮丧,我对此几乎无能为力-是否有完善的现有Web服务(API)将PDF转换为图像?

Re your update: Can you try converting a tiny small PDF to see whether it's a memory issue after all? 重新更新:是否可以尝试转换一个很小的小PDF毕竟是否是内存问题?

Also, ImageMagick has a debug command line option: 另外,ImageMagick具有debug命令行选项:

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. 我认为它可以调试到stdout,您必须捕获它记录的内容并输出。 Manual here 这里的手册

And another thing, I think to convert PDFs, ImageMagick needs to call ghostscript (I think the binary is gs ). 另外,我认为要转换PDF,ImageMagick需要调用ghostscript(我认为二进制文件是gs )。 Can you try and make sure that is callable with the rights of the user PHP runs as? 您是否可以尝试确保可调用的PHP用户身份运行为? ImageMagick should return an error if it isn't, but you never know. 如果不是,则ImageMagick 应该返回错误,但是您永远不会知道。

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. 我认为服务器资源限制(主要是一些内存限制)正在干扰ImageMagick,并且这样做的方式非常不一致。 Limiting ImageMagick's memory use still produced errors, and failed to convert files successfully. 限制ImageMagick的内存使用仍然会产生错误,并且无法成功转换文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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