15

I have many documents scanned in past with a printer that did not have a direct "to PDF" option, so I have various folders called "Document1, Document2." Within each of the folders were many jpg like 001.jpg, 002.jpg, 003.jpg, ..

I want to join them in a single PDF file. Modern printers just do the same incorporating a JPG "stream" encapsulated and shown one for each page.

I tried to do it with a simple image viewer by selecting all of them and printing in PDF. This caused a re-compression of all images again in JPG with several drawbacks:

  • If I select a JPG quality too poor the image look super crappy and compressing something compressed will lead to Generation Loss.
  • If I select a JPG very high quality near ~100% in order to prevent additional generation loss the filesize of the output PDF will be huge.

Here's what I am thinking:

Because the images stored in a PDF file are nothing more than JPG streams, isn't it possible to include original JPG files directly into a PDF container without recompressing them? This would be the best way to achieve NO LOSS and NO RECOMPRESSION!

In case, which software can I use in order to do so? There's something out of there like ffmpeg for PDFs (my head go to ffmpeg -c:v copy option to copy streams into a different container) ? Perhaps a parser of postscript to call directly, rendering a folder of jpg files and many command-line options?

It would be nice to find a command line software available for all operating systems. The software must, however, run under Windows.


EDIT: Aside the main question of a multi-platform software, I was trying to understand why Adobe Acrobat itself isn't able to do it. In the advanced settings dialog it seems be possible with the option: Save original JPEG images in PDF if possible

Adobe PDF Setting: Save original JPEG images in PDF if possible

Probably the problem here is given by my image viewer program that resample and rehandle in some ways the JPGs instead giving them directly inside Adobe Acrobat Distiller.

Should I just drag & drop the files inside Acrobat Application ?

  • Based on what i found on this topic: stackoverflow.com/questions/13618236/… I downloaded imagemagick win64 exe version and tried to use the command convert *.jpg file.pdf in a folder with about 380 files. A nightmare, I have 16GB RAM on this laptop but them got instantly filled by convert.exe! The total filesize of the JPGs is 336 MB, at this rate I doubt that the program is just importing them, instead a full reconversion is applied! Maybe digging into the documentation a "no-resample / no-transcode" is possible but i haven't found yet. – user3450548 Oct 29 '15 at 20:16
11

img2pdf

Website: https://gitlab.mister-muffin.de/josch/img2pdf/blob/master/README.md

img2pdf is a Python library with a command-line interface, which directly (i.e. losslessly and without recompression) embeds JPG images into a PDF.

Private usage

Occasionally, I need to automatically convert high-resolution TIFF files to individual PDF pages. I am leveraging a PowerShell script which downscales the TIFFs to JPEGs sing a .NET API and then calls the img2pdf CLI to produce multiple PDFs. Eventually, the PDFs get combined with some other PDF files using pdftk.
The bottom line is: img2pdf fits quite nicely my workflow and I can definitely recommend it.
Plus, the author responded very quickly to a question I once raised.

Installation

Provided that you have Python already installed, you can just run

pip install img2pdf

Usage

  • Single JPG file:

    python.exe -m img2pdf inputImage.jpg -o output.pdf
    
  • Wildcards (in PowerShell):

    python.exe -m img2pdf $(gci myFolder/*.jpg) -o output.pdf
    

    A quick test revealed that img2pdf even seems to support spaces in file names.

  • Wildcards (in most *nix shells):

    python.exe -m img2pdf myFolder/*.jpg -o output.pdf
    
  • Converting many files at once

    In certain cases you can hit the maximum length of allowed command line strings . The limit varies per OS, e.g. you can use at least 2047 characters depending on your Windows version, on Linux you can run xargs to have a look at your current limits.

    In such cases, you can run img2pdf on individual files and later combine them using any PDF tool capable of doing so, e.g. pdftk:

    1. for /r %i in (*.jpg) do python -m img2pdf -o %i.pdf %i
      (Windows command prompt syntax)
    2. pdftk.exe *.pdf cat output combined.pdf

img2pdf's author josch is open to any pull requests incorporating built-in wildcard support for Windows: https://gitlab.mister-muffin.de/josch/img2pdf/issues/25#note_122

(Thanks to user3450548 for mentioning the potential problem with regard to converting many files at once and sharing his solution in the comments!)

  • Can be used for process multiple files at once ? If i try to use something like img2pdf -o output.pdf folder/*.jpg it throws out some errors! I have to write some batch to process all the files and then join the various PDFs with another utility ? – user3450548 Oct 30 '15 at 17:34
  • 1
    @user3450548 img2pdf does not support wildcards. Fortunately, if you use PowerShell instead of cmd.exe, you can just type ./python -m img2pdf $(gci myFolder/*.jpg) -o out.pdf. – ComFreek Oct 30 '15 at 17:55
  • Thank you. The $(gci myFolder/*.jpg) is supposed to repeat the path automatically for each file found in that directory right? Doing so will not output a extreme long command that could break at some point? If i want to use direct shell what i could do? a for /r %i in (*.jpg) do python -m img2pdf -o %i.pdf %i and then use some tool_for_join_pdf.exe folder/*.pdf ? – user3450548 Oct 30 '15 at 18:16
  • A possible workflow could be join the pdf files with the pdftk tool like pdftk.exe *.pdf cat output combined.pdf – user3450548 Oct 31 '15 at 21:00
  • @user3450548 Indeed you could hit the (minimum) limit of 2047 characters as per this MS article on cmd.exe command-line string limitation). Thanks for your comments, I will gratefully incorporate them! – ComFreek Nov 3 '15 at 21:40
6

PDFJam

PDFJam, part of texlive, is a wrapper script that provides a front-end to the LaTeX pdfpages utility.

This script accepts JPG and PNG images, as well as PDF files, and concatenates them together. The input is NOT recompressed, and is embedded into the PDF stream.

Usage

The following command will create an output file in the current working directory, named XXX-joined.pdf, where XXX is the name of the first file on the command line:

     pdfjam --fitpaper true --rotateoversize true --suffix joined /path/to/*.jpg

Platforms

  1. Windows

    Download and install Cygwin. Be sure to select the texlive-collection-binextra package:Installation of the texlive-collection-binextra package

  2. Arch Linux-based distributions

    Use pacman to install texlive-core.

  3. Debian-based (Ubuntu and others)

    The tool is distributed as part of the texlive-extra-utils package.

  • Thanks, I also found this one: gitlab.mister-muffin.de/josch/img2pdf/tree/master The problem is that I'm on Windows, any chance to find windows command line exe ports of one of them? Without using things like cygwin ? – user3450548 Oct 30 '15 at 0:10
  • Ok, i managed to make it running on windows so i will share my installation details to help other users. However I'm still searching for a more Native windows command line program. This is how: Install Cygwin (i used the x64 ver), during the installation (or after by re-running again the setup doing next next next) at the packages selection pick the following: texlive, texlive-collection-basic, texlive-collection-latex, texlive-collection-latexrecommended. Once did this you can download the PDFJam package and put it into your /home directory and remember to edit your .bashrc file. – user3450548 Oct 30 '15 at 0:12
  • Edit .bashrc by adding export PATH=$PATH:/home/user/pdfjam/bin line at the end. At this point restart the Cygwin terminal and you could use the commands described by baitisj in his answer. – user3450548 Oct 30 '15 at 0:14
  • 1
    In the PDFJam website they talk also about Multivalent a Java based package able to deal with PDFs in some ways (they say is even more complete). As PDFJam on Windows requires Cygwin and some setup, Multivalent require Java, and if not installed into java extensions, at least a command line like java -classpath /path/to/Multivalent.jar tool.XXX [options] files I don't know however if Multivalent includes the images in the way we want like PDFJam without recompressing the images! Maybe I could try to send them a mail and ask :) – user3450548 Oct 30 '15 at 0:48
  • @user3450548 - it might be helpful to add the platform requirement to your original question :-) – baitisj Oct 30 '15 at 3:24
3

Adobe Acrobat

not the best of the 3 solutions, please check the PDFJam below too

I add this answer just for the sake of contributing to the community. But I will not vote it, because this answer offer a solution just to the main topic without taking in account the secondary requirements:

  • free
  • command line
  • multiplatform

Adobe Acrobat itself allows to join multiple JPGs files together apparently without applying a recompression.

The interface can be called in many ways, i will show them with corresponding images.

By selecting many files and hitting right click on them:

Combine Files called by right click

By hitting the create button in Adobe Acrobat Pro:

Combine Files called by Acrobat main window

In any case the Adobe Acrobat will open the Combine Files window allowing you to select files or folder to add:

Combine Files interface

By leaving selected the medium setting file size, and if you are working with default options (you didn't change Adobe PDF Standard settings overriding them with something else) the program will output a single PDF file with the JPG incorporated inside.

On my test of 385 JPG files with 336 MB disk space, the pdf come with 337 MB file adding 1MB overhead.

With PDFJam script the overead was near to zero resulting in a 336 MB PDF file. Also by keeping the standard settings Acrobat will stretch the images over an A4 or Letter size. PDFJam in comparison just created a pdf with each page fitting the JPG size instead.

This obviously by keeping the defaults, probably by tweaking the options in both programs a similar output could be obtained.

2

jpeg2pdf

This one's a tiny command line tool to create a PDF file from JPGs without re-compressing them.

Get it here: http://jpeg2pdf.sourceforge.net/

Features

  • Stores original JPGs in PDFs
  • Various paper size options (including cropping to image size)
  • Various scaling options (including preserving original image scale)

Usage

jpeg2pdf *.jpg -o document.pdf

See the readme and built-in help for more info.

Platforms

It says it's cross-platform, but I've only tested it on Windows. Looks like you need to compile it from the C source code on Unix-like systems.

2

Image to PDF or XPS

(Formerly JPEGtoPDF)

This one's a Windows GUI application written in VB.NET. It doesn't claim that it stores the original JPG data in the PDF output, but when I tested it, it did actually preserve the original JPG data.

Get it here: http://compulsivecode.com/Project_ImageToPDF.aspx

It's probably slow compared to jpeg2pdf. However, there's no installation needed, and it seems straight-forward to use, so it could be useful for people who aren't comfortable with the command line.

Image to PDF or XPS Screenshot

  • question is.. does it re-compress / re-encode? in this case the answer is not acceptable.. – user3450548 Jun 26 '16 at 13:29
  • @user3450548, it doesn't claim that it preserves the original JPEG data, but when I tested it (using the xpdfbin pdfimages -j command to extract the JPGs and the Windows fc command to compare them with the originals), the JPG data in the PDFs was identical to the originals. – Sam Jun 27 '16 at 0:37
  • thank you for your suggestion then ;) – user3450548 Jun 28 '16 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.