imagemagick / ghostscript Error on conversion - php

We are using imagemagick to convert PDFs to JPEGs. Using the code below:
shell_exec('convert source.pdf output.jpg');
Im getting this error:
Segmentation fault
convert: Postscript delegate failed `/var/www/location/source.pdf': # error/pdf.c/ReadPDFImage/663.
convert: missing an image filename `/var/www/location/output.jpg' # error/convert.c/ConvertImageCommand/3011.
And works most of the time. There are some PDFs though that we can convert. Those are with alot of text and tables have some problem and do not convert at all.
I used GS script to convert and same thing. It does not convert.
shell_exec('gs -sDEVICE=jpeg -o output.jpg source.pdf');
Is there any way to convert those pdfs to jpg using imagemagick/gs?
What are things to avoid on pdf so that it will be converted to jpg using imagemagick/gs?

Related

PHP Convert SVG To Jpg Missing Elements

I am using php imagemagic to convert svg to jpg and i use the below command for the conversion.
convert -density 250 source.svg target.jpg
I can able to convert svg to jpg successfully but some elements in svg are missing. Please check below example
Input SVG
Output JPG
Here you can clearly see that the light grey shades on the sides are replaced with white color. Can someone let me know how to fix this.
FYI here is the link to download the actual SVG
https://drive.google.com/file/d/1vC5yaXds7ogcsTWjaDzkXZZFyKzLCsgf/view?usp=sharing
Which SVG renderer are you using? It could be the internal Imagemagick MSVG/XML, the RSVG delegate or Inkscape (in order of increasing functionality). You can find out by adding -verbose to your command line.
convert -verbose -density 250 source.svg target.jpg
I used Imagemagick 6.9.10.11 Q16 Mac OSX with its internal MSVG/XML renderer, RSVG 2.42.2_2 and Inkscape 0.92.3_4. All three produced different results. The RSVG was the worst. The Inkscape was the best. Here are my results using your command.
convert -density 250 MSVG:source.svg target_msvg.jpg
convert -density 250 RSVG:source.svg target_rsvg.jpg
convert -density 250 source.svg target_inkscape.jpg
Usually RSVG does better than Imagemagick's MSVG. But here it seems to do worse. It could be due to the way I modified my delegates.xml file in order to be able to run RSVG while Inkscape was installed. Also the MSVG renderer has been improved over the last few releases. So an older version may not produce as good of a result.
Finally I found a workaround for this. I converted svg to canvas image (base64 format) via javascript and through php i have converted the base64 data to jpg image.

Converting SVG to another format fail due to delegate not being found

I have PHP code running on Debian stretch with ImageMagick. It tries to convert SVG to another format. Here is how it starts
$im = new Imagick();
$im->readImageBlob($svg);
The variable $svg contains valid SVG code in a string. If I copy this string to a text file with a .svg extension then it opens just fine. But readImageBlob throws an exception saying no delegate for this image format.
I have seen similar questions solved by installing more packages to the system. But I've already installed libxml2-dev, librsvg2-bin, libmagickcore-6.q16-3-extra and libfreetype6-dev.
I have no idea what else I am missing.
I had to prepend the $svg with <?xml version="1.0" ?> and it worked. It least it does read in the SVG and attempts to create png/jpeg. One piece of text gets misplaced during convertion though. So the task in general is still failed. But this is another issue. I think the question is answered.
What do you get from running the following command from PHP exec()
convert -list format
The line SVG should say RSVG or MSVG/XML. Does it show that? if you need RSVG, you will have to install that delegate and then reinstall Imagemagick so that Imagemagick can find it. Imagemagick is used by Imagick. They are not the same. The RSVG delegate can be found by a Google Search or from linuxfromscratch.org/blfs/view/svn/general/librsvg.html. Your svg file seems to render properly for me in Imagemagick using RSVG, but I am not sure what it should look like. It is just a graph set of horizontal lines.
I do not know much about using readImageBlob(). Just use readImage(), where you supply the path to a saved svg image file. That should work. Try that and see what you get.
Here is what I get using RSVG 2.42.2 in Imagemagick 6.9.10.3 Q16 Mac OSX were I have saved your text in a file called test.svg.
convert test.svg test.png
If I force the use of the Imagemagick MSVG/XML, it does not look as good.
convert MSVG:test.svg test2.png

ImageMagick PDF crop retain quality

I have got some PDF files i need to crop (crop to trimbox etc), which I can do with the following command
convert -define pdf:use-trimbox=true -density 300 original.pdf outcome.pdf
It does the job however the outcome.pdf quality if not as sharp as original PDF. When I crop them on my desktop software (Acrobat Pro) the result it same quality but in ImageMagick I can not keep the same quality in the outcome.
My question is how can i crop a pdf page without compromising from the quality?
i have been searching and trying different settings for weeks but not been succesfull.
Most likely the problem is that ImageMagick is having the PDF rendered to a bitmap by Ghostscript, and then exporting the bitmap wrapped up in a PDF file. Without seeing the original I can't say for sure, but if the original contained JPEG images, then most likely you are ending up with JPEG being applied twice, or simply rendering at all is causing the problem.
Your best bet is going to be to use a tool which can simply apply a CropBox to the page(s). You can do this with Ghostscript, for example (which may also modify the PDF in other ways, including the double JPEG quantisation, so beware).
gs -sDEVICE=pdfwrite \
-sOutputFile=cropped.pdf \
-dBATCH -dNOPAUSE \
-c "<</ColorImageFilter /FlateEncode>> setdistillerparams" \
-f <input.pdf> \
-c "[ /CropBox [ 0 0 100 100] /PAGES pdfmark" \
-f
The first section between -c and -f tells the pdfwrite device to use FlateEncode for colour images, the default is JPEG, using Flate will ensure you don't get quantisation applied twice.
The second section between -c and -f tells the pdfwrite device to write a CropBox to the file and to make it 0,0 to 100,100. The units are the usual units in PDF; 1/72 inch, you can use fractional values.
I'm sure there are other tools which will do this, possible even more easily.
Have you tryed to increase the density? That's the purpose:
http://www.imagemagick.org/script/command-line-options.php#density
Otherwise try:
-quality 100
From:
Convert PDF to image with high resolution

GhostScript - text misplaced after converting from pdf to jpg

I am using PHP exec() and GhostScript to convert my PDF files to JPG, however, there seems to be an issue with text - the letters gets misplaced. Here are example screenshots how it turns out:
This is how it is on PDF - http://screencast.com/t/vmF2kJdlts
And this is how it turns out on JPG - http://screencast.com/t/BTFNMkrc
Here's the command I'm using:
exec("/usr/bin/gs -dNOPAUSE -sDEVICE=jpeg -sOutputFile=test.jpg -dJPEGQ=100 -r814x1149 -q test.pdf", $out, $rcode);
The PDF itself is generated with dompdf and custom installed font if that helps.
Any suggestions what I am doing wrong?
After converting it with GS9.07win the text looks fine (text is rendered with default font, Arial). Most probably the problem with OpenSans and OpenSans-Bold fonts substitution or incomplete glif maps.

PHP Imagemagick PDF to GIF

I have the following code which is supposed to convert the first page of PDF to thumbnail:
<?php
$strPDF = "http://www.domain.com/b.pdf";
exec("/usr/bin/convert \"{$strPDF}[0]\" -colorspace RGB -geometry 200 \"output.gif\"");
?>
My host server is Siteground, and apparently /usr/bin/convert is where ImageMagick convert function is. This is my first time using ImageMagick and I'm not sure if it is doing anything. Is my code correct? And if it is, I can't seem to find output.gif.
Give the output file a full path. Example : /home/marlboro/www/output.gif
Also, first download the PDF to your local machine (use curl/wget).
Before using exec from PHP you should be 100% sure the method works when called manually from shell!

Categories