LinuxCommandLibrary

img2pdf

Convert images to PDF

TLDR

Convert one or more images to a single PDF, each image being on its own page

$ img2pdf [path/to/image1.ext path/to/image2.ext ...] --output [path/to/file.pdf]
copy

Convert only the first frame of a multi-frame image to PDF
$ img2pdf [path/to/file.gif] --first-frame-only --output [path/to/file.pdf]
copy

Auto orient the image, use a specific page size in landscape mode, and set a border of specific sizes horizontally and vertically
$ img2pdf [path/to/image.ext] --auto-orient --pagesize [A4^T] --border [2cm]:[5.1cm] --output [path/to/file.pdf]
copy

Shrink only larger images to a rectangle of specified dimensions inside a page with a specific size
$ img2pdf [path/to/image.ext] --pagesize [30cm]x[20cm] --imgsize [10cm]x[15cm] --fit [shrink] --output [path/to/file.pdf]
copy

Convert an image to PDF, and specify metadata for the resulting file
$ img2pdf [path/to/image.ext] --title [title] --author [author] --creationdate [1970-01-31] --keywords [keyword1 keyword2] --subject [subject] --output [path/to/file.pdf]
copy

SYNOPSIS

img2pdf [OPTIONS] IMAGE [IMAGE ...] [-o OUTPUT.pdf]
img2pdf [OPTIONS] -o OUTPUT.pdf [IMAGE ...]

PARAMETERS

-o, --output FILE
    Specify the output PDF filename. If not given, output goes to standard output.

-S, --pagesize SIZE
    Set the PDF page size (e.g., A4, Letter, 210mmx297mm, or 'fit' to fit the largest image).

--fit-width
    Scale images to fit the page width, maintaining aspect ratio. Requires --pagesize to be set.

--fit-height
    Scale images to fit the page height, maintaining aspect ratio. Requires --pagesize to be set.

--rotation DEG
    Rotate images by a specified angle (90, 180, 270 degrees). Applied before fitting.

--border SIZE
    Add a border around images. Can be a single value for all sides or four values (left, top, right, bottom).

--dpi DPI
    Set the DPI for rasterizing vector graphics or scaling images without explicit DPI information.

--img-dpi DPI
    Override the DPI information for images that have it embedded, used for scaling.

--jpeg-quality QUALITY
    Set JPEG quality for non-JPEG images that need re-encoding (0-100). Ignored for JPEG input.

--viewer-page-layout LAYOUT
    Set the initial page layout for the PDF viewer (e.g., OneColumn, TwoPageLeft, TwoPageRight, SinglePage).

--viewer-non-full-screen-page-mode MODE
    Set the initial document outline/thumbnail panel mode for the PDF viewer (e.g., UseNone, UseOutlines, UseThumbs, UseOC).

--title TEXT
    Set the PDF document title metadata.

--author TEXT
    Set the PDF document author metadata.

--creationdate YYYYMMDDHHMMSS[+|-HH'MM']
    Set the creation date for the PDF document.

-v, --version
    Show program's version number and exit.

-h, --help
    Show a help message and exit.

DESCRIPTION

img2pdf is a powerful and efficient command-line tool designed to convert one or multiple image files into a single PDF document. Unlike many other converters, img2pdf aims for lossless conversion by directly embedding JPEG and JPEG2000 images into the PDF without re-encoding, preserving the original image quality and metadata. For other image formats (like PNG, TIFF, GIF, BMP), it converts them to uncompressed raw pixel data within the PDF. It supports various image formats and offers extensive options for controlling page size, image fitting, rotation, metadata, and PDF viewer settings, making it highly flexible for professional and archival use.

CAVEATS

img2pdf requires a Python 3 environment. While it excels at lossless conversion for JPEG/JPEG2000, other image formats (PNG, TIFF, GIF, BMP) are converted to uncompressed pixel data, which can result in significantly larger PDF file sizes. Processing very large image files or many images simultaneously might consume considerable memory.

PIPING INPUT

img2pdf can accept image data from standard input, making it suitable for piping with other commands (e.g., curl example.com/image.jpg | img2pdf -o output.pdf). When using standard input, only one image can be processed.

MULTIPLE IMAGES

You can specify multiple image files as arguments, and img2pdf will concatenate them into a single multi-page PDF document. Each image will occupy a new page in the order they are provided.

HISTORY

img2pdf was created with the primary goal of providing a simple, fast, and lossless way to convert images to PDF, specifically addressing the common issue where other converters re-encode JPEG images, leading to quality degradation and increased file sizes. Its development focused on leveraging the PDF standard's ability to embed JPEG data directly, ensuring maximum quality preservation and efficiency. It is written in Python and gained popularity for its straightforward approach and reliable results.

SEE ALSO

convert(1), gs(1), pdftk(1), lpr(1)

Copied to clipboard