Converting images to PDF format is a common task, especially when dealing with multiple files. While graphical tools are available, the command line offers a powerful and efficient way to batch convert JPG files to PDFs in Ubuntu. This article explores several methods to achieve this, providing you with the flexibility to choose the best approach for your specific needs.
The command line interface (CLI) offers several advantages for batch image conversion:
convert
Command (ImageMagick)The convert
command, part of the ImageMagick suite, is a versatile tool for image manipulation. It can easily convert multiple JPG files into individual PDFs or a single PDF.
To convert all JPG files in a directory to a single PDF, use the following command:
convert *.jpg myPdf1.pdf
This command concatenates all JPG images in the current directory into a single PDF file named myPdf1.pdf
.
If you need to convert each JPG file to a separate PDF, you can use a loop:
for file in *.jpg; do
convert "$file" "${file%.jpg}.pdf"
done
This script iterates through all JPG files in the current directory and converts each one to a PDF with the same name. For example, myJPG1.jpg
will be converted to myJPG1.pdf
.
In some cases, the above command might create files with a double extension (e.g., myJPG1.jpg.pdf
). To fix this, you can use the rename
command:
rename 's/\.jpg\.pdf$/.pdf/' *.jpg.pdf
This command removes the extra .jpg
extension from the generated PDF files.
find
and convert
The find
command is useful for locating files, especially when dealing with subdirectories. You can combine it with convert
to process JPG files recursively:
find -name '*.jpg' -exec convert "{}" "{}.pdf" \;
This command finds all JPG files in the current directory and its subdirectories and converts them to PDFs. You might still need to use the rename
command to clean up the filenames. You can limit the search depth using -maxdepth 1
to only search the current directory.
img2pdf
for Lossless ConversionThe convert
command might introduce slight quality loss during the conversion process. If preserving the original image quality is crucial, img2pdf
is a better option.
Install img2pdf
using the following command:
sudo apt install img2pdf
To convert multiple JPG files to individual PDFs with minimal quality loss, use:
ls -1 ./*jpg | xargs -L1 -I {} img2pdf {} -o {}.pdf
This command lists all JPG files, and then uses xargs
to pass each filename to img2pdf
, creating a PDF for each JPG. As before, you may need to rename the files to remove duplicate extensions.
A more concise way to convert all JPG images to a single PDF is:
img2pdf *.jp* --output combined.pdf
This command combines all JPG images into a single PDF file named combined.pdf
.
Once you have your PDFs, you might want to make them searchable by adding Optical Character Recognition (OCR). You can use the ocrmypdf
tool for this:
Install ocrmypdf
using:
sudo apt install ocrmypdf
To apply OCR to your PDF, use the following command:
ocrmypdf combined.pdf combined_ocr.pdf
This command creates a new PDF file (combined_ocr.pdf
) with OCR applied, making the text searchable.
Converting JPG files to PDFs using the command line in Ubuntu offers a range of options to suit different needs. Whether you prioritize simplicity, quality, or automation, the methods outlined in this article provide a solid foundation for efficient image conversion. By leveraging tools like convert
, find
, and img2pdf
, you can streamline your workflow and manage your image collections with ease.