Pop PDF
Import

Importing

Pdf::importFromFile() reads an existing PDF from disk and hands back a Document object - the same document object that you would have built from scratch. You can inspect it, add to it and write it out again. Reading a file in does not touch it — every change stays in the returned Document until you write it out under a different name, so the source file is safe to import repeatedly.

Importing a File#

importFromFile($file, $pages = null, $password = null) parses $file and returns a fully independent Document:

PHP
use Pop\Pdf\Pdf;

$imported = Pdf::importFromFile(__DIR__ . '/report.pdf');
$imported->getNumberOfPages(); // 3

A path that does not exist raises Pop\Pdf\Build\Exception:

PHP
use Pop\Pdf\Pdf;
use Pop\Pdf\Build\Exception;

try {
    Pdf::importFromFile(__DIR__ . '/does-not-exist.pdf');
} catch (Exception $exception) {
    echo $exception->getMessage();
    // Error: That PDF file does not exist.
}

Selecting Pages#

The $pages parameter targets specific pages to import. It takes a single 1-based page number or an array of them, pulling only those pages out of the source document:

PHP
use Pop\Pdf\Pdf;

$imported = Pdf::importFromFile(__DIR__ . '/report.pdf', pages: [1, 3]);
$imported->getNumberOfPages(); // 2

Adding to an Imported Document#

Because the result is an ordinary Document, createPage() puts a new page alongside the imported ones with nothing special done to make room for it:

PHP
use Pop\Pdf\Pdf;
use Pop\Pdf\Document\Page;
use Pop\Pdf\Document\Page\Text;
use Pop\Pdf\Document\Font;

$document = Pdf::importFromFile(__DIR__ . '/report.pdf');
$document->addFont(Font::HELVETICA);

$page = $document->createPage(Page::LETTER);
$page->addText(new Text('Summary', size: 20), 'Helvetica', x: 72, y: 700);

$document->getNumberOfPages(); // 4 — the three imported pages, plus the new one

Pdf::writeToFile($document, filename: __DIR__ . '/report-plus-summary.pdf');

Importing Raw Bytes#

importRawData($data, $pages = null) is importFromFile() for a PDF you already have in memory rather than on disk — a string of raw PDF bytes in place of a path, with the same $pages argument behind it:

PHP
use Pop\Pdf\Pdf;

$imported = Pdf::importRawData(
    file_get_contents(__DIR__ . '/report.pdf')
);

$imported->getNumberOfPages(); // 3

Page selection works identically to importFromFile():

PHP
use Pop\Pdf\Pdf;

$bytes = file_get_contents(__DIR__ . '/report.pdf');
$selected = Pdf::importRawData($bytes, pages: [1, 3]);

$selected->getNumberOfPages(); // 2

Reach for importRawData() wherever the PDF arrives as bytes rather than a filesystem path — the body of an uploaded file, a stream pulled from storage, or a document generated somewhere else in the request and never written to disk at all. Bytes that are not a parseable PDF at all raise Pop\Pdf\Build\Exception, the same exception type a missing file raises on importFromFile():

PHP
use Pop\Pdf\Pdf;
use Pop\Pdf\Build\Exception;

try {
    Pdf::importRawData('not a pdf at all');
} catch (Exception $exception) {
    echo $exception->getMessage();
    // Error: Could not resolve the source PDF document catalog (Root).
}

Building a Document From Images#

importFromImages($images, $quality = 70) builds a Document from one or more raster images rather than an existing PDF, one image per page, each page sized to that image's own pixel dimensions:

PHP
use Pop\Pdf\Pdf;

$gallery = Pdf::importFromImages([__DIR__ . '/photo1.jpg', __DIR__ . '/photo2.jpg']);
$gallery->getNumberOfPages(); // 2

$page = $gallery->getPage(1);
$page->getWidth();  // 400
$page->getHeight(); // 300

$images also takes a single path on its own rather than an array, for a one-page result. Every image is converted to JPEG before it is placed, whatever format it started in, and $quality is that conversion's JPEG quality, from 1 to 100. It defaults to 70, and it is the one argument on this method worth tuning. The lower the quality, the smaller the PDF file size, but the more the page images will show degradation.

Every page takes the pixel dimensions of its own source image, so images of different sizes produce a document with mismatched page sizes rather than one uniform size chosen for the set.

A missing image path raises Pop\Pdf\Document\Exception rather than skipping the file silently:

PHP
use Pop\Pdf\Pdf;
use Pop\Pdf\Document\Exception;

try {
    Pdf::importFromImages(__DIR__ . '/does-not-exist.jpg');
} catch (Exception $exception) {
    echo $exception->getMessage();
    // Error: That image file does not exist.
}

See Also#