Merging png images into one pdf file

UnixPdfMergePng

Unix Problem Overview


How can I merge several .png files into one PDF file in Unix?

Unix Solutions


Solution 1 - Unix

From looking through the documentation on ImageMagick, it might be as easy as:

convert 1.png 2.png myfile.pdf

See comments about possible risks. If that doesn't work, PDFjam claims to be able to solve your problem.

Solution 2 - Unix

If I want to merge some scans to one PDF file, I do this:

pdfjoin --a4paper --fitpaper false --rotateoversize false scan01.png scan02.png

This gives you a PDF document with DIN-A4 page size, where every png file is centered on it's own page. Images that are too big for one DIN-A4 page are resized proportionally to fit on one page. Smaller images are not resized (not made bigger).

You have to name all png files on the command line, but you can also use wildcards to eg merge all png files in the current directory:

pdfjoin --a4paper --fitpaper false --rotateoversize false *.png

The pdfjoin command is part of PDFjam as mentioned in the answer by Jeremiah Willcock. So you will most likely have to install a package named pdfjam or texlive-extra-utils with your distros package manager. PDFjam is able to use png files as input since Version 2.07, released in 2010-11-13.

Solution 3 - Unix

ImageMagick’s convert tool is my preference.

> The convert program is a member of the ImageMagick suite of tools. Use it to convert between image formats as well as resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more. > > convert [input-option] input-file [output-option] output-file`

If you want the image files (and thus, their quality and file size) unaltered, and just put a PDF container around them:

convert In.png In-2.png Someother-*.png Result.pdf

In case you want to have a smaller file size, and you are okay with a loss in quality, you can convert them to the JPEG format first. (ImageMagick also supports changing the PNG compression level, but usually your input files are already using the highest level.)

convert 1.png 2.png -compress jpeg -quality 50 Result.pdf

Use a value between 0 and 100 for the quality option.

Alternatively, you can reach a lower file size (and quality) by resampling the images to a certain resolution.

convert *.png 2.png -resample 300 Result.pdf

The value for resample refers to the number of pixels per inches. ImageMagick reads the original density from EXIF part of the input images, falling back to 72 dpi. You can use the density parameter to set a custom resolution for the input images.

You can of course also combine the compress, quality, and resample parameters.

Solution 4 - Unix

I stole this, but this is the solution I used from Jeremiah Willcock and another answer website. Not digging through history at the moment. I lied, I did. (Tully @https://askubuntu.com/a/626301)
I needed a file small enough to email.

To combine images into a PDF (from in working directory use command line:

user@box:/home/user/scans/:$ 
convert 1.png 2.png convertoutput.pdf

To shrink using ghostscript after combining (I used on kde default system almost):

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default \
   -dNOPAUSE -dQUIET -dBATCH -dDetectDuplicateImages -dCompressFonts=true \
   -r150 -sOutputFile=output.pdf convertoutput.pdf

My file had 14 images (19MB after convert, gs made it 1.6MB, quality was still great). The output file is called output.pdf.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontwidizleView Question on Stackoverflow
Solution 1 - UnixJeremiah WillcockView Answer on Stackoverflow
Solution 2 - UnixPascal RosinView Answer on Stackoverflow
Solution 3 - UnixMichael SchmidView Answer on Stackoverflow
Solution 4 - Unixpctech101View Answer on Stackoverflow