Merge Images and PDFs into PDF

When working with PDF documents or scanned images, it’s a pretty common task to merge them to PDF. We have been doing these things using imagemagick and ghostscript for years. But we had to compromise with a few serious drawbacks of that process including converting PDF pages into image snapshot and increasing file size. So, eventually, I have written merge2pdf a tiny cli tool for this job (and a few extra).

This post is to show some example usages (commands) of this tool for different scenarios.

Highlights

Before diving into commands, let’s go through a list of highlights of this tool.

  • Lossless quality by default. Option to adjust JPEG quality
  • Can be merged all pages or only specific pages of PDF
  • Supports a wide range of image formats
  • Options to adjust size, margin, and scaling of images
  • Supports directory as input (merges all Images/PDFs of a directory)
  • Free and open source
  • Very simple and easy to use

Example commands of merge2pdf

Merging Images into PDF

# Merge multiple Images
merge2pdf output.pdf image1.jpg image2.jpg path/to/other.png
 
# Merge all Images/PDFs of one or multiple directory
merge2pdf output.pdf path/to/a/dir path/to/another-dir
 
# Any number of directory of images and single images can be merged
merge2pdf output.pdf path/to/a/dir path/to/an-img.png

Fine tuning Image pages

When adding Images, by default the size for an image containing pages will be the same as image size with 1 inch margin on each side. But you may set custom margins and resize to standard Print size.

# Set image page size to A4. Other Options are A3, Legal and Letter
merge2pdf -s A4 output.pdf image1.jpg image2.jpg
 
# Set image page size and margin (left, right, top, bottom).
merge2pdf -s A3 -m .5,.5,1,1 output.pdf image1.jpg image2.jpg
# margin can be set without size
 
# Scale images to page width, with aspect ratio
# To scale to height, use --scale-height
merge2pdf -s A3 -m .5,.5,1,1 --scale-width output.pdf image1.jpg image2.jpg
 
# For JPEG, you can set optimized quality
merge2pdf -m 0,0,0,0 --jpeg-quality=80 output.pdf image1.jpg image2.jpg

Joining/Merging multiple PDFs

# Merge multiple PDFs
merge2pdf output.pdf input1.pdf input2.pdf path/to/other.pdf
 
# Merge all PDFs of a directory
merge2pdf output.pdf path/to/pdfs/dir
merge2pdf output.pdf first/pdfs/dir second/pdfs/dir ...

Joining specific pages of PDF files

# Merge 1st page of input1.pdf, full input2.pdf and 2nd, 3rd, 4th page of input3.pdf  
merge2pdf output.pdf input1.pdf~1 input2.pdf input3.pdf~2,3,4

Merging multiple PDFs, PDF pages and Images together

# Mixing up PDF, PDF Pages, Images and directories
merge2pdf output.pdf doc1.pdf~1,2 image1.jpg path/to/dir path/to/other.pdf ...

If the examples above don’t cover your special scenario, please let me know. As long as it can be done with this tool, I may help you to make the right command. Also, you should check the repository for installation instructions, cautions etc.

Thanks for using and sharing 😀

Leave a Comment

Your email address will not be published. Required fields are marked *