Skip to content

dmak/merge2pdf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Merge2PDF

Build Status Coverage Status

If you like this utility, please give in a star in GutHub! Report the issues to bugtracker.

Description

This utility allows one to "convert" JPEG to PDF without recompression. More generally, it combines a sequence of PDF, JPEG, TIFF, PNG images into one PDF.

The code was originally posted here, and later evolved into a more powerful utility.

The question about JPEG-to-PDF conversion was asked several times (see here), however it is known that GraphicsMagick uses GhostScript under the hood, which performs image decompression and then compression (check this answer comments) hence gm convert image.jpg image.pdf does not fit.

Further, pdftk and qpdf only take PDF as input, not images. Hence PDF should be created from images upfront and later taken by one of these utilities.

The only utility which does combining of images efficiently is img2pdf, written in Python.

I have also done small research and examined PDFs generated by several online services.

These do not perform recompression, but they are online services or commercial products:

These services perform recompression:

Extras

Usage

java -jar merge2pdf.jar -m [-d] [-s dim] [-Anum] [-g pos] [-b num] one.pdf [two.jpg three.png ...] out.pdf

java -jar merge2pdf.jar -e [-p directory/file_prefix] in.pdf

java -jar merge2pdf.jar [-v|-h]

Option Comment
-m|--merge Merge PDF/image files that follow into one PDF.
-d|--dpi If image metainformation provides DPI, then scale the image down, namely to 72 / image_DPI × 100%, where 72 DPI is a standard PDF DPI. This is necessary in case one needs to merge images with different DPI so that they are represented on the same scale in resulting PDF.
-s|--scale Scale down (if necessary) image so that it fits provided box given as a page (A4) or dimension in pixels (180x20).
-An, e.g. -A3, -A4, -A5, ... Scale the image down to fit the given page size (A3, A4, ...). Auto-rotation is applied, i.e. if image width is bigger than height and the image does not fit the portrait-oriented page, page orientation is set to landscape and then the image is scaled down (if necessary). Images are anchored to the top-left corner.
-g|--gravity Place an image to the page at given corner. Requires that page size is given (e.g. -A4, see above). Possible values (case insensitive): center / centre, top / north, topright / northeast, right / east, bottomright / southeast, bottom / south, bottomleft / southwest, left / west, topleft / northwest.
-b|--border When adding images, pad them with given border.
-r|--remove-font When reading input PDFs, remove the specified embedded font(s) thus reducing the output PDF size. Font name is given as regular expression where empty argument (-r "") means ".*" i.e. remove all embedded fonts.
-e|--extract Extract images from given PDF. Images get automatic suffixes like _1, _2, etc and appropriate extension (.jpg, .png). Note that this function is still experimental as it does not fully support extraction of TIFF files, which are saved in PNG format.
-p|--prefix Use given directory or directory + file as a prefix. If omitted, the current directory is used to save extracted files.
-v|--version Print version and exit.
-h|--help Print help and exit.

Note that output PDF is overwritten without any warnings.

Examples

  • java -jar merge2pdf.jar --merge 1.jpg 2.tiff all.pdf – merge images 1.jpg and 2.tiff into all.pdf as is, i.e. each image forms a separate page with the size equal to image

  • java -jar merge2pdf.jar --merge -A3 -b20 -rArial 1.pdf 2.png 3.png out.pdf – append images 2.jpg and 3.png to 1.pdf and save the resulting PDF to out.pdf; the images are placed to page size A3 padded with border 10 pixels, resized (if necessary) to fit the given page and centered; the embedded font Arial is removed (if found)

  • java -jar merge2pdf.jar --merge --dpi -scale 200x100 view1.jpg view2.jpg all.pdf – merge images view1.jpg and view1.jpg into all.pdf; the images are first scaled according to DPI (that means for example that image with 300dpi is twice smaller than image with 150dpi) and then scaled down (if don't fit) the given box with width 200px and height 100px.

Development / Contribution

Please follow Contribution section.

License

The whole project is licensed under AGPLv3.

About

Utility that creates PDF from the sequence of images (JPEG, PNG, ...) or PDFs

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages