100

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

Zaz
  • 39,637
  • 10
  • 70
  • 92
twidizle
  • 1,245
  • 2
  • 12
  • 16

4 Answers4

161

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

convert 1.png 2.png myfile.pdf

If that doesn't work, PDFjam claims to be able to solve your problem.

Jeremiah Willcock
  • 27,461
  • 5
  • 68
  • 75
  • 8
    That's amazing. So easy. It automatically sizes the images and everything.. I love open-source. – Nathan Wallace Apr 07 '14 at 16:18
  • Is there a way I can control the quality of the document or the file size of the resulting PDF? I need to upload some documents with a maximum file size of 2.5 MiB. I've tried with the `-quality` option but the file size is always ~9.5 MiB. – arielnmz Jul 28 '14 at 05:01
  • Is it possible to append image files to existing pdf? – Kiran Reddy Nov 21 '17 at 05:21
  • 3
    Convert all the files in a folder of a specified type using `convert *.jpg file.pdf` – haakym Feb 23 '18 at 14:25
  • 2
    @arielnmz, you can compress the file by using the parameters `-compress jpeg -quality 50`. See [my answer](https://stackoverflow.com/a/50897456/2136929) – Michael Schmid Jun 17 '18 at 14:17
  • Weird. I tried convert natscan*.jpg natscan.pdf and get the error convert: not authorized `natscan.pdf' @ error/constitute.c/WriteImage/1028. I own all the files and the directory so I don't understand 'not authorised'. – Peter Flynn Nov 10 '19 at 17:55
  • @PeterFlynn I also received a similar error. This was because of some additional security policy implemented for ImageMagick (`convert` is part of it). Refer to the following to temporarily disable the policy: https://askubuntu.com/questions/1081895/trouble-with-batch-conversion-of-png-to-pdf-using-convert – Ryan Y Jan 03 '20 at 11:28
39

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.

Pascal Rosin
  • 1,408
  • 15
  • 26
13

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.

Michael Schmid
  • 1,420
  • 13
  • 18
  • Anyone here knows how to revert the operation? – Andrei Cioara Oct 18 '18 at 15:43
  • The image files are not deleted, while you may copy them away for peace of mind. I also had the https://stackoverflow.com/questions/42928765/convertnot-authorized-aaaa-error-constitute-c-readimage-453 issue to resolve before it worked. – Audrius Meskauskas Mar 04 '19 at 14:01
  • Anyone finding issue to convert images into pdf and seeing error saying 'convert not allowed' please check this answer: https://askubuntu.com/questions/1127260/imagemagick-convert-not-allowed – Tuhin Jun 26 '20 at 23:03
  • Recently imagemagick denies PDF conversion by default. See this question if you encounter this so you know how to change this policy. https://stackoverflow.com/questions/52998331/imagemagick-security-policy-pdf-blocking-conversion – user2565010 Sep 09 '20 at 21:14
  • How can I define the PDF version? – Nur Muhammad Jan 12 '21 at 02:20
5

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.

Vinicius Placco
  • 1,573
  • 2
  • 9
  • 23
pctech101
  • 51
  • 1
  • 3
  • You can compress the file directly with **convert**, using the parameters `-compress jpeg -quality 50`. See [my answer](https://stackoverflow.com/a/50897456/2136929) – Michael Schmid Jun 17 '18 at 14:13
  • @MichaelSchmid The compression with convert is not as effective. It only reduces jpeg quality whereas the gs solution changes the resolution. If you try it with a high-resolution image, you will see how large the difference in size is, even if you go for extremely low jpg quality. – mak Jun 30 '20 at 09:39
  • Yes, resampling might indeed be more effective than the JPEG compression. And ImageMagick also supports that. I’ve added that option in my answer. – Michael Schmid Jul 09 '20 at 11:24