If you like this utility, please give in a star in GutHub! Report the issues to bugtracker.
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:
- http://convert-my-image.com/ – based on iText PDF 5.3.0
- https://www.pdfpro.co/ – uses BCL easyPDF
These services perform recompression:
- Additional information about how "correct" merge should work is given in book "PDF Explained" by John Whitington (2011), p.109, ISBN 1449310028.
- Duplicate TTF font merging and replacement, Fonts and merging documents.
- Unembed a font.
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.
-
java -jar merge2pdf.jar --merge 1.jpg 2.tiff all.pdf– merge images1.jpgand2.tiffintoall.pdfas 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 images2.jpgand3.pngto1.pdfand save the resulting PDF toout.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 imagesview1.jpgandview1.jpgintoall.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.
Please follow Contribution section.
The whole project is licensed under AGPLv3.