command line - Convert a directory of JPEG files to a single PDF ...

How to Convert a Directory of JPEG Files to a Single PDF Document Using the Command Line in Ubuntu

Need to combine multiple JPEG images into a single PDF document on your Ubuntu system? The command line offers a fast and efficient way to achieve this. This article explores several methods, focusing on tools readily available in Ubuntu and how to use them effectively.

Why Use the Command Line for JPEG to PDF Conversion?

While graphical tools exist, the command line provides several advantages:

  • Speed: Command-line operations are often faster than their GUI counterparts, especially for batch processing.
  • Automation: You can easily script the conversion process for repetitive tasks.
  • Efficiency: No need to open multiple programs or click through numerous menus.
  • Server Compatibility: Ideal for server environments where a GUI is not available.

Method 1: Using ImageMagick's convert Command

ImageMagick is a powerful image manipulation tool available on most Linux distributions. Its convert command is perfect for this task.

  1. Installation: If you don't have ImageMagick installed, use the following command:

    sudo apt-get install imagemagick
    
  2. Basic Conversion: To convert all JPEG files in the current directory to a single PDF, use:

    convert *.jpg pictures.pdf
    

    This command concatenates all .jpg files into a PDF named pictures.pdf.

  3. Ordering Files: The order of images in the PDF will follow the alphabetical order of the filenames. For correct ordering, especially with numbered files, name them with leading zeros (e.g., image01.jpg, image02.jpg, ..., image10.jpg).

  4. Auto-Orient Images: To automatically rotate images based on their EXIF data, use the -auto-orient option:

    convert *.jpg -auto-orient pictures.pdf
    

Method 2: Using img2pdf for Lossless Conversion

For minimal quality loss during conversion, img2pdf is an excellent choice. This method preserves the original JPEG data within the PDF.

  1. Installation: Install img2pdf using:

    sudo apt-get install img2pdf
    
  2. Conversion: The simplest command is:

    img2pdf *.jpg --output combined.pdf
    

    This creates combined.pdf from all JPEG files in the directory.

  3. OCR (Optional): To add an OCRed text layer for searchable PDFs, use ocrmypdf:

    ocrmypdf combined.pdf combined_ocr.pdf
    

    Install ocrmypdf if you don't have it: sudo apt-get install ocrmypdf.

Method 3: Using pdfjam

pdfjam is another efficient tool, especially when working with LaTeX.

  1. Installation: Install pdfjam using:

    sudo apt install pdfjam
    

    Alternatively, it might come with texlive-extra-utils:

    sudo apt install texlive-extra-utils
    
  2. Conversion: To create an A4-sized PDF from all JPEG files:

    pdfjam --a4paper *.jpg
    

    This creates a PDF named yourfiles-pdfjam.pdf. Use the --outfile option to specify a different output name:

    pdfjam --a4paper *.jpg --outfile combined.pdf
    

Additional Tips and Considerations

  • Handling Filenames with Spaces: If your filenames contain spaces, use quotes around the filename variables in your commands.

  • Memory Issues: If you encounter memory issues with a large number of files, consider converting images in smaller batches or using tools like img2pdf which process files one by one.

  • Image Compression: Be mindful of compression options. Using the +compress option in convert disables compression, leading to significantly larger PDF files. Omit compression options to maintain the original JPEG compression.

  • Alternative Tools: LibreOffice Writer can also be used by opening the images and exporting them as a PDF. You can also find various online converter websites that offer this functionality.

By utilizing these command-line methods, you can efficiently convert directories of JPEG files into single PDF documents on your Ubuntu system. Choose the method that best suits your needs, whether it's speed, quality preservation, or advanced options like OCR.