PDF-Converter

ImageToPdf
in package

FinalYes

Converts images into a PDF with a page for each, every page exactly the size of its image.

One pixel becomes one point, as in the ReportLab script this was ported from. Any format GD can read works (PNG, JPEG, GIF, WebP, BMP and AVIF). Transparency is kept as a soft mask, so transparent areas show the white page; GD stores alpha in 7 bits, so it is kept to within one step of 255.

Images are decoded one at a time, and only their compressed pixels are kept, so many large images can go into one PDF.

Needs only the gd and zlib extensions, which PHP ships with.

Table of Contents

Methods

convert()  : string
Converts an image file and writes a one-page PDF.
convertAll()  : string
Converts image files and writes a PDF with a page for each, in the order given.
fromFile()  : string
Converts an image file into the bytes of a one-page PDF.
fromFiles()  : string
Converts image files into the bytes of a PDF with a page for each, in the order given.
fromString()  : string
Converts image bytes into the bytes of a one-page PDF.
fromStrings()  : string
Converts images' bytes into the bytes of a PDF with a page for each, in the order given.
document()  : string
Builds a PDF with a page for each image, each image drawn across its whole page.
fromImages()  : string
Decodes and compresses each image in turn, then builds the PDF.
image()  : string
An image XObject holding 8-bit samples.
samples()  : array{0: string, 1: ?string}
Reads an image's pixels, top row first, as 8-bit RGB samples and 8-bit alpha samples, compressed with zlib.
stream()  : string
A stream object: its dictionary, which this closes after adding the length, and its data.

Methods

convert()

Converts an image file and writes a one-page PDF.

public static convert(string $imagePath, string $pdfPath) : string
Parameters
$imagePath : string

The image to convert.

$pdfPath : string

Where to write the PDF; an existing file is replaced.

Tags
throws
RuntimeException

The image could not be read, or the PDF could not be written.

InvalidArgumentException

The file is not an image GD can read.

Return values
string —

The PDF's path.

convertAll()

Converts image files and writes a PDF with a page for each, in the order given.

public static convertAll(array<int, string> $imagePaths, string $pdfPath) : string
Parameters
$imagePaths : array<int, string>

The images to convert, one per page.

$pdfPath : string

Where to write the PDF; an existing file is replaced.

Tags
throws
RuntimeException

An image could not be read, or the PDF could not be written.

InvalidArgumentException

A file is not an image GD can read, or there are no images.

Return values
string —

The PDF's path.

fromFile()

Converts an image file into the bytes of a one-page PDF.

public static fromFile(string $imagePath) : string
Parameters
$imagePath : string

The image to convert.

Tags
throws
RuntimeException

The image could not be read.

InvalidArgumentException

The file is not an image GD can read.

Return values
string

fromFiles()

Converts image files into the bytes of a PDF with a page for each, in the order given.

public static fromFiles(array<int, string> $imagePaths) : string

Each file is read only when its page is made.

Parameters
$imagePaths : array<int, string>

The images to convert, one per page.

Tags
throws
RuntimeException

An image could not be read.

InvalidArgumentException

A file is not an image GD can read, or there are no images.

Return values
string

fromString()

Converts image bytes into the bytes of a one-page PDF.

public static fromString(string $bytes[, string $name = 'The image' ]) : string
Parameters
$bytes : string

The image file's contents.

$name : string = 'The image'

What to call the image in an error message.

Tags
throws
InvalidArgumentException

The bytes are not an image GD can read.

Return values
string

fromStrings()

Converts images' bytes into the bytes of a PDF with a page for each, in the order given.

public static fromStrings(array<int, string> $images[, array<int, string> $names = [] ]) : string
Parameters
$images : array<int, string>

Each image file's contents, one per page.

$names : array<int, string> = []

What to call each image in an error message; by default "Image 1", "Image 2" and so on.

Tags
throws
InvalidArgumentException

An image is not one GD can read, or there are no images.

Return values
string

document()

Builds a PDF with a page for each image, each image drawn across its whole page.

private static document(array<int, array{0: int, 1: int, 2: string, 3: ?string}> $pages) : string

Each page takes the next object numbers in turn: the page, its image, its content and, when the image has transparency, its mask. One image therefore gives objects 3 to 5 or 6, as it always has.

Parameters
$pages : array<int, array{0: int, 1: int, 2: string, 3: ?string}>

Each image's width and height in pixels, which are its page's in points, and its RGB and alpha samples compressed with zlib, top row first; alpha is null for an opaque image.

Return values
string

fromImages()

Decodes and compresses each image in turn, then builds the PDF.

private static fromImages(iterable<string|int, array{0: string, 1: string}> $images) : string
Parameters
$images : iterable<string|int, array{0: string, 1: string}>

Each image's bytes and what to call it in an error message.

Tags
throws
InvalidArgumentException

An image is not one GD can read, or there are no images.

Return values
string

image()

An image XObject holding 8-bit samples.

private static image(int $width, int $height, string $colorSpace, string $samples, string $extra) : string
Parameters
$width : int
$height : int
$colorSpace : string

DeviceRGB or DeviceGray.

$samples : string

The samples, compressed with zlib, which is what /FlateDecode reads.

$extra : string

More dictionary entries, each with a leading space.

Return values
string

samples()

Reads an image's pixels, top row first, as 8-bit RGB samples and 8-bit alpha samples, compressed with zlib.

private static samples(GdImage $image) : array{0: string, 1: ?string}

Each row is compressed as it is read, so the uncompressed pixels are never held in memory all at once.

Parameters
$image : GdImage
Return values
array{0: string, 1: ?string} —

The compressed RGB samples, and the compressed alpha samples, or null when every pixel is opaque.

stream()

A stream object: its dictionary, which this closes after adding the length, and its data.

private static stream(string $dictionary, string $data) : string
Parameters
$dictionary : string

The dictionary, opened but not closed.

$data : string
Return values
string
On this page

Search results