How to use the command 'img2pdf' (with examples)
The ‘img2pdf’ command is a versatile tool that allows users to convert raster images into PDF files without any loss in quality. It supports a range of image formats including GIF, JPEG, JPEG2000, PNG, GIF, and TIFF. This command is particularly useful for creating PDF documents from multiple images or handling images that are part of a larger document creation process. Users can customize the output using various options, such as selecting specific frames from multi-frame images, adjusting the orientation, and adding metadata to the resulting PDF file.
Use case 1: Convert one or more images to a single PDF, each image being on its own page
Code:
img2pdf path/to/image1.ext path/to/image2.ext ... --output path/to/file.pdf
Motivation:
This use case is useful when you have multiple images that you wish to compile into a single PDF document for distribution, archiving, or printing. If you have a series of images from an event or a collection of scanned documents, converting them into a single, portable PDF format ensures ease of access and presentation consistency.
Explanation:
path/to/image1.ext path/to/image2.ext ...
: Specifies the paths to the images you want to convert. Each image will appear on a separate page in the PDF.--output path/to/file.pdf
: Designates the path and name of the output PDF file.
Example Output:
A PDF file named file.pdf
containing each specified image on its own 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:
Animated images or multi-frame images like GIFs can contain numerous frames. If your goal is to only capture the first frame for documentation or presentation purposes, this option allows you to convert just that initial frame instead of the entire sequence, resulting in a faster conversion process and a smaller file size.
Explanation:
path/to/file.gif
: Path to the multi-frame image file.--first-frame-only
: This option ensures that only the first frame of the multi-frame image is converted.--output path/to/file.pdf
: Specifies the output file for the PDF.
Example Output:
A PDF file named file.pdf
that contains only the first frame from the GIF image.
Use case 3: Auto orient the image, use a specific page size in landscape mode, and set a border of specified sizes horizontally and vertically
Code:
img2pdf path/to/image.ext --auto-orient --pagesize A4^T --border 2cm:5.1cm --output path/to/file.pdf
Motivation:
Images often come in various orientations, and creating a PDF document with consistent orientation and size can enhance its readability. By setting a specific page size and adding borders, you can ensure that each image fits nicely within the page, with a consistent, professional-looking presentation.
Explanation:
path/to/image.ext
: The image you wish to convert.--auto-orient
: Automatically rotates the image to match the page layout for optimal viewing.--pagesize A4^T
: Sets the page size to A4 in landscape mode (denoted by^T
).--border 2cm:5.1cm
: Adds a border of 2cm on the horizontal sides and 5.1cm on the vertical sides.--output path/to/file.pdf
: Determines the output file name and path.
Example Output:
A landscape-oriented PDF file with the specified image and defined borders, ensuring a clean and professional presentation.
Use case 4: Shrink only larger images to a rectangle of specified dimensions inside a page with a specific size
Code:
img2pdf path/to/image.ext --pagesize 30cmx20cm --imgsize 10cmx15cm --fit shrink --output path/to/file.pdf
Motivation:
When dealing with high-resolution images, it might be necessary to fit them within a specified dimension without cropping. This is particularly helpful for maintaining the entirety of the image content while matching page size constraints for presentations or printed materials.
Explanation:
path/to/image.ext
: Specifies the path to the image for conversion.--pagesize 30cmx20cm
: Defines the overall page size.--imgsize 10cmx15cm
: Sets the target rectangle size for the image.--fit shrink
: Shrinks the image to fit within the designated dimensions if necessary.--output path/to/file.pdf
: Specifies where the resulting PDF will be saved.
Example Output:
A PDF file named file.pdf
where larger images are resized to 10cm by 15cm on pages sized 30cm by 20cm.
Use case 5: Convert an image to PDF, and specify metadata for the resulting file
Code:
img2pdf path/to/image.ext --title title --author author --creationdate 1970-01-31 --keywords keyword1 keyword2 --subject subject --output path/to/file.pdf
Motivation:
Adding metadata to your PDF files can significantly enhance document management. This is particularly important for archiving, searching, and organizing digital libraries where categorization and easy retrieval are necessary. For professional documents, metadata provides valuable contextual information.
Explanation:
path/to/image.ext
: The image you wish to convert.--title title
: Sets the PDF document title.--author author
: Names the author of the document.--creationdate 1970-01-31
: Sets the date of creation for the document.--keywords keyword1 keyword2
: Adds keywords for searchability.--subject subject
: Provides a subject for the PDF.--output path/to/file.pdf
: Specifies the output location for the PDF file.
Example Output:
A PDF document complete with metadata such as title, author, creation date, keywords, and subject, aiding in document categorization and retrieval.
Conclusion:
The ‘img2pdf’ command provides a robust framework for converting images to PDFs with different customization options, such as handling multi-page documents, managing frame selection, ensuring consistent orientation and size, resizing images, and embedding metadata. This functionality supports a wide range of applications from personal document management to professional publishing. Whether you’re converting a batch of images or managing specialized document requirements, ‘img2pdf’ offers a reliable and efficient solution.