How to use the command img2pdf (with examples)
The img2pdf
command is a command-line tool for converting raster images to PDF format. It is a lossless converter that can be used to convert multiple images into a single PDF file, convert specific frames of multi-frame images, auto orient images, set page size and borders, shrink images, and specify metadata for resulting PDF files.
Use case 1: Convert multiple images to a single PDF, each image being on its own page
Code:
img2pdf path/to/image1.jpg path/to/image2.jpg --output path/to/file.pdf
Motivation:
This use case is helpful when you want to combine multiple images into a single PDF file, where each image is placed on its own page. It can be useful for creating photo albums or presentations.
Explanation:
path/to/image1.jpg path/to/image2.jpg
: Specifies the paths of the input images that will be converted to PDF.--output path/to/file.pdf
: Specifies the output path and filename for the resulting PDF file.
Example output:
The command will convert image1.jpg
and image2.jpg
into a single PDF file named file.pdf
, with image1.jpg
on the first page and image2.jpg
on the second page.
Use case 2: Convert only the first frame of a multi-frame image to PDF
Code:
img2pdf path/to/file.gif --first-frame-only --output path/to/file.pdf
Motivation:
When dealing with multi-frame images, such as animated GIFs, you may only want to convert the first frame to a PDF. This can be useful when you want to extract a specific image from an animated GIF file.
Explanation:
path/to/file.gif
: Specifies the path of the input GIF file.--first-frame-only
: Instructs the command to only convert the first frame of the GIF file.--output path/to/file.pdf
: Specifies the output path and filename for the resulting PDF file.
Example output:
The command will convert the first frame of file.gif
into a PDF file named file.pdf
.
Use case 3: Auto orient the image, use a page size of A4 in landscape mode, and set a border of 2cm horizontally and 5.1cm vertically
Code:
img2pdf path/to/file.jpg --auto-orient --pagesize A4^T --border 2cm:5.1cm --output path/to/file.pdf
Motivation:
Sometimes images need to be auto oriented or properly adjusted to fit a specific page size and have borders. This use case allows you to achieve that for easy printing or display.
Explanation:
path/to/file.jpg
: Specifies the path of the input image file.--auto-orient
: Automatically orients the image to the correct orientation if it contains EXIF information.--pagesize A4^T
: Sets the page size to A4 (29.7cm x 21cm) in landscape mode (^T).--border 2cm:5.1cm
: Sets a border of 2cm horizontally and 5.1cm vertically.--output path/to/file.pdf
: Specifies the output path and filename for the resulting PDF file.
Example output:
The command will auto orient the image, set the page size to A4 in landscape mode, and add a border of 2cm horizontally and 5.1cm vertically. The resulting PDF file named file.pdf
will have the adjusted image.
Use case 4: Shrink only larger images to a 10cm by 15cm rectangle inside a 30x20cm page
Code:
img2pdf path/to/file.tiff --pagesize 30cmx20cm --imgsize 10cmx15cm --fit shrink --output path/to/file.pdf
Motivation:
When working with images of different sizes, you may want to resize larger images to fit a specific rectangle inside a page. This use case helps with resizing and fitting images for uniformity.
Explanation:
path/to/file.tiff
: Specifies the path of the input TIFF file.--pagesize 30cmx20cm
: Sets the page size to 30cm x 20cm.--imgsize 10cmx15cm
: Sets the size of the image within the page to 10cm x 15cm.--fit shrink
: Shrinks the larger images to fit the specified image size.--output path/to/file.pdf
: Specifies the output path and filename for the resulting PDF file.
Example output:
The command will shrink larger images to fit a 10cm by 15cm rectangle inside a 30cm by 20cm page. The resulting PDF file named file.pdf
will have the resized and fitted image.
Use case 5: Convert an image to PDF, and specify metadata for the resulting file
Code:
img2pdf path/to/file.png --title title --author author --creationdate 1970-01-31 --keywords keyword1 keyword2 --subject subject --output path/to/file.pdf
Motivation:
When converting an image to a PDF, it can be beneficial to include relevant metadata for easy organization and searchability, such as title, author, creation date, keywords, and subject.
Explanation:
path/to/file.png
: Specifies the path of the input PNG file.--title title
: Sets the title metadata for the resulting PDF file.--author author
: Sets the author metadata for the resulting PDF file.--creationdate 1970-01-31
: Sets the creation date metadata for the resulting PDF file.--keywords keyword1 keyword2
: Sets the keywords metadata for the resulting PDF file.--subject subject
: Sets the subject metadata for the resulting PDF file.--output path/to/file.pdf
: Specifies the output path and filename for the resulting PDF file.
Example output:
The command will convert file.png
into a PDF file named file.pdf
and include the specified metadata (title, author, creation date, keywords, subject) in the resulting PDF file.
Conclusion:
The img2pdf
command is a versatile tool for converting raster images to PDF format. It enables users to combine multiple images into a single PDF, extract specific frames from multi-frame images, adjust image orientation and size, add borders, and include metadata. By understanding the different use cases and their arguments, users can effectively utilize img2pdf
for various image-to-PDF conversion needs.