How to Convert Image to PDF in Ubuntu 22.04
Converting an image file to a PDF could be intimidating for Linux newbies. To convert images to PDF, you’ll need a command-line program that either keeps the original resolution or reduces it by a negligible amount. As a result, this post will give command-line utilities to batch-convert images to PDF in Ubuntu 22.04.
Use ImageMagick for Conversion
ImageMagick is a free, open-source, and compatible with all platforms image processing software. It’s a binary distribution that may be used as-is or tweaked, duplicated, and redistributed for use in any open-source or closed-source program.
Another great feature is that this program may utilize several threads to speed up the processing time required to read, write, or analyze images of varying sizes.
The capabilities of this program go much beyond those required for converting images. It also can produce and edit photos in a wide range of image formats. In contrast to other image processing suites, it is at the center of many web servers, CMSes, and social media networks, where it conducts bulk or repeated imaging procedures.
Get the most current version by updating the repository to the latest version.
$ sudo apt update
$ sudo apt-get install imagemagick -y
In addition to converting images between formats, the convert command in ImageMagick can perform a wide variety of other image manipulation tasks, such as resizing, blurring, drawing, cropping, and resampling.
The “convert” command of the tool provides the following methods for transforming photos into PDF documents:
Single Image Conversion to PDF
The following command should be used to convert a single PNG file to a PDF:
$ sudo convert image1.png image1.pdf
The error seen above might occur if the ImageMagick global policy file has been modified (policy.xml). To mitigate an ImageTragick vulnerability in versions prior to 7.0.1-2, these changes were proposed.
To change the policy.xml file, go to the /etc/ImageMagick-6/ directory.
$ cd /etc/ImageMagick-6/
$ sudo vim policy.xml
A modification was made in the following policy from “none” to “read|write” permissions. Afterwards, you may use the “convert” command again to list the directory and find a new PDF file.
Multiple Images Conversion to PDF
Using the “convert” option, you can save a subset or the entire collection of images in the folder as a PDF. Use the following command to convert many images quickly and easily to PDF format:
$ sudo convert image1.png image2.png Image.pdf
Similarly, ImageMagick extends the Linux globing concept of filenames. This function applies to all of the picture files in the folder where the conversion will take place.
$ sudo *.png Images.pdf
Using the PNG images in the current folder, the command generates a PDF document.
However, ImageMagick does not provide lossless conversion for large-size pictures that demand little in the way of storage space or processing power. This restriction means we may switch to a different command-line tool without having to re-enable security in ImageMagick.
Img2pdf is a program that can transform bitmap images into PDF documents. Fast PDF conversion of photos with precise pixel-by-pixel color; no CPU required to re-encode tiny pixel data; little size difference between input and output files due to the overhead of the PDF container.
The raw pixel data is not kept in RAM, which is another advantage of img2pdf. The high-quality characteristic of the utility allows it to process input of very large sizes with ease. By just upgrading the Ubuntu repository, you can quickly install img2pdf.
$ sudo apt-get install img2pdf
Conversion of Images
The process used by this app to transform images into PDFs is quite like that of ImageMagick. cd to the location where the images were stored to
Single-image PDF creation
$ sudo img2pdf image1.png -o output_img1.pdf
Similarly, you can convert multiple images to pdf as well by typing the below command:
$ sudo img2pdf image1.png image2.png -o output_img.pdf
$ sudo img2pdf *.png -o output_imgs.pdf
The article provides a list of command-line Linux program that may be used to do this conversion. We presented ImageMagick and img2pdf and assessed their advantages and disadvantages to provide readers a better understanding and assist them pick their favorite tool.