Page 1 of 1

Convert pdf to png and back again

Posted: 2009-11-10T12:47:06-07:00
by CGRemakes
Well, doing some research, it appears that it is best to use GS to get the PDF to a raster image. However, I need to make it so it is then reconverted from an image back into a PDF file. This means that the positioning/size/resolution, etc needs to be the same when it's converted from an image to PDF as it was when it is converted from a PDF to an image. I'm sure it's not possible to get it the EXACT quality as the original, but it needs to be close. I've gotten it close using the following commands:

Code: Select all

gs -q -sDEVICE=png16m -r288 -dBATCH -dNOPAUSE -dFirstPage=1 -dLastPage=1 -sOutputFile=test.png 1667_1_1242147993.pdf
to convert to a png file at a high resolution.

I then use ImageMagick to convert it back to the original size using the following command:

Code: Select all

convert test.png -resample 72 test.pdf
It is really close, other than everything seems to be slightly larger on test.pdf than it did on the original 1667_1_1242147993.pdf

Re: Convert pdf to png and back again

Posted: 2009-11-10T21:26:48-07:00
by anthony
use

Code: Select all

convert test.png -units PixelsPerInch -density 288 -resample 72 test.pdf
To ensure the PNG has the correct density for the conversion.

Note that PNG image file dormat does not actually save density as dots-per-inch, but as dots-per-centimeter and this has caused some confusion and problems in resolution handling.

Re: Convert pdf to png and back again [RESOLVED]

Posted: 2009-11-11T08:23:29-07:00
by CGRemakes
Hmm... I had to remove the resample 72, or it printed it 1/4 size of the original. However, the final results were still the same (too large). Using the note you mentioned, I tried doing a jpg instead of a png, but the results were the same. Any other thoughts?

EDIT:

Got it figured out, and the speed/results are EXCELLENT (I cannot tell a difference in quality from the original PDF)! I used the same command for gs, but for ImageMagick, I used the following command:

Code: Select all

convert test.png -units PixelsPerInch -density 288 -resize "2448x3168" test.pdf

Re: Convert pdf to png and back again

Posted: 2009-11-11T16:39:31-07:00
by anthony
That seems very strange that a image size change without density change makes such a difference.
Perhaps it is the way IM handles generation of PDF from an image.

One idea is to do with the border of the page. The GS output would probably include a blank border surrounding the page. But IM in its generation of the new page probably adds another border around the page. Could this be the difference you see?

Remember PDF files usually do not have a fixed density. But PDF can contain raster images which have a 'ideal' or 'fixed' density. As the image would fill page IM may ignore density and just fill the page which would change 'density' handling.


IM you must remember is a raster image processor. It is not designed with vector image formats in mind such as PDF. As such PDF is handled as separate input and output techniques. It makes no guarantee of PDF to PDF compatibility.

See A Word about Vector Formats
http://www.imagemagick.org/Usage/formats/#vector