Search code examples
pdfghostscriptimagemagick-convert

How can I convert pdf to png now that imagemagik no longer works on my shared hosting server


---EDIT---

As discussed below and in the comments, my ISP took a default policy.xml on June 25 for Imagemagick which turns off convert for pdf files, which is what I need. I am getting crickets from my requests to modify it and I don't consider that a good solution as it could revert back on the next upgrade. I have found that Ghostscript will convert pdf files. I would appreciate any input if I am on the right track but so far this is very promising.

My existing call is this

convert -density 300 -quality 100 \"" . Aqualarm.pdf . "\"  -resize 800  -sharpen 0x1.0 -flatten \"" . Aqualarm.png  . "\"

The proposed Ghostscript version is this

gs -dNOPAUSE -dBATCH -r300 -dDownScaleFactor=3  -sDEVICE=png16m -sOutputFile="Aqualarm_%03d.png" Aqualarm.pdf

In this case Aqualarm is a test file.

---EDIT 2---

Using Ghostscript as described above worked with one modification. convert numbers files starting with 0 and gs numbers files starting with 1. I had to put a test in and if file 0 was missing, I changed the index to 1. Other than that I am happy with the result. This is apparently a common problem even on non shared host systems. The issue is that updates to ImageMagick will overwrite edits to the policy.xml so what you do there to make things work, might stop working on the next update. Since ImageMagick uses Ghostscript to do this anyway, I don't see any reason not to bypass the middleman.

From other reading I found that the reason ImageMagick disabled pdf by default is because of an error in Ghostscript that was fixed a few version back.

---END EDIT---

My website is on a shared hosting server. For years I have used ImageMagick "convert" to turn pdf documents into png. Now I get an error message as described here

ImageMagick security policy 'PDF' blocking conversion

The message is: convert-im6.q16: attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408.

The suggested solution is to modify policy.xml but of course on a shared host I do not have access to that file.

I have also seen the suggestion to install pdftoppm but even after hours of searching I cannot find out how to do that locally, without root access.

Is there a way that will work on a shared host server?

Thank you for reading.


Solution

  • I decided that getting policy.xml changed to allow pdf was not a good solution because it might just be overwritten at the next release and put me right back where I am. Research uncovered that ImageMagick uses Ghostscript to do the pdf conversions so why put up with an unreliable middleman. More research found some command line batch instructions to do the conversion. However, the resolution was terrible. Only when I got up to close to the resolution of 300 did I get good results but the file was huge. Ghostscript has a command that allows high internal resolution and then a downscale factor to bring the file to a smaller size. Why this is better than just directly converting to the file size I want is a mystery but this is the recommended solution and experimentation showed it to be of high quality. The final solution is as follows:

    $gscommand = "gs -dNOPAUSE -dBATCH -r300 -dDownScaleFactor=3  -sDEVICE=png16m -sOutputFile=\"" .$file . "_%d.png\"  " . $file . ".pdf";
    $returnedvalue = exec($gscommand);
    

    In closing, this seems to be a pretty common problem without a solution other than use a different program. One recommended is pdftoppm which I did not find how to install on a shared host system and with Ghostscript doing the job there is no need to figure that out.

    I hope this post helps others faced with this problem.