GhostScript - text misplaced after converting from pdf to jpg - php

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.

Related

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 / ghostscript Error on conversion

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?

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

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!

FPDF error: This document (testcopy.pdf) probably uses a compression technique which is not supported by the free parser shipped with FPDI

I am running the following code and giving me this error : FPDF error: This document (testcopy.pdf) probably uses a compression technique which is not supported by the free parser shipped with FPDI. I used another pdf named test.pdf and that works fine but it is giving me error in testcopy.pdf.
I think this is parser problem. Anyone know any other parser that can be used with fpdf to avoid this error?
My code:
require('fpdf17/fpdf.php');
require('fpdf17/fpdi.php');
// initiate FPDI
$pdf = new FPDI();
while (ob_get_level())
ob_end_clean();
header("Content-Encoding: None", true);
// set the sourcefile
$pagecount = $pdf->setSourceFile('testcopy.pdf');
I want to split pdf in two pdfs and want to attach both pdfs in file attachments field.How to save pdf to server. Can it be possible with fpdf?
Yes, Exactly the FPDF library supports only PDF version 1.4 & lesser. So to over come from this issue use GhostScript. This script helps to change the PDF versions dynamically.
(1) Download the Ghostscript here.
(2) Install the Ghostscript and define the environment variable PATH.
(3) Then, use the below php code to change the PDF version.
shell_exec( "gswin32 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=".$new_pdf." ".$old_pdf."");
(4) Now we get PDF with version 1.4 as output, then continue with FPDF.
Enjoy!
I had the same issue that PDFMerger does not work with certain PDF.
It seems that Adobe keep updating compression way of pdf extension.
I found solution with Chrome.
1. Open the PDF file(which does not work with PDFMerger) with Chrome
2. Right Click and Print
3. Change destination as [Save as PDF]
4. This exported PDF file is compatible with PDFMerger
The file needs to be PDF 1.4 (Acrobat 5) or lower. If you have Adobe Acrobat Pro you can change this with Document->Decrease File Size.
Looking through the answers so far, I wasn't able to resolve the issue.
Here's why and you may have the same issue so this answer would be invaluable to you too.
I did not have access to acrobat, so I couldn't downgrade the version of the original PDF. I wasn't in a position to purchase the newer version of the library which has upgraded encryption.
In the end I used an online PDF converter to downgrade my PDF file to version 5 (1.4)
If you are not able to find one, here's a link for completeness.
https://docupub.com/pdfconvert/
(bear in mind that the contents of a link may change)
Here is my answer from another topic:
"It can be overcome by re-saving the template and change compression type.
In Adobe Acrobat Pro go to File->Save As->Optimised PDF-> Popup left list - Clean Up,
Object Compression Options -> Change to "Compress document structure" -> OK to save"
Ref. Editing password protected pdfs with fpdi
GPL Ghostscript 8.70 (2009-07-31) / Centos 7
ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=new_file.pdf file.pdf
For those that don't have any of the pro versions of Acrobat:
I was having this problem with new versions of Word when trying to use the Office functionality to save to a PDF. It seems the PDF conversion tool supplied in Office is using the newer formats. Here's how I fixed it.
Install NitroPDF free version (May work with another free PDF creator that can be downloaded)
Go to the Word document and select 'Print'.
In the print options, instead of your printer, select 'Nitro PDF Creator (Reader 3)' or whatever the current version is.
A dialog box will open and allow you to select where to save the PDF.
The version saved was able to be opened by FPDF.
Hope it helps
In the case of Mac, you can open the PDF file in Preview and "export as PDF". The exported one can then be used.
I had a very similar error like you. My solution was to convert the pdf to a lower pdf version. Than everything worked like expected.
I hope that simple trick will help you!

Categories