02 — Import

Change a PDF you already have.

An imported PDF comes back as the same objects you would have built yourself — pages, fonts, images, text. Which means modifying a document is the same work as making one, using the same API you already read on the last page.

08

Import & modify

Import a whole file, or name the pages you want and leave the rest on disk. Then stamp it, annotate it, add pages, reorder them, and write it back out.

Working from a stream rather than a path — an upload, an S3 object, a queue payload? Pdf::importRawData() takes the bytes.

Importing in the docs →
stamp.php
use Pop\Pdf\Pdf;
use Pop\Pdf\Document;

// the whole file
$doc = Pdf::importFromFile('contract.pdf');

// or only the pages you need
$doc = Pdf::importFromFile(
    'contract.pdf', [2, 4, 6]
);

// register the font you stamp with
$doc->addFont(Document\Font::ARIAL_BOLD);

$doc->getPage(1)->addText(
    new Document\Page\Text('DRAFT', 48),
    Document\Font::ARIAL_BOLD, 180, 400
);

Pdf::writeToFile($doc, 'contract-draft.pdf');
09

Merge PDFs

Combine files in order, into one document object you can keep working on — add a cover page, number the result, stamp it — before you write it.

Same story for streams: Pdf::mergeRawData().

Merging in the docs →
merge.php
use Pop\Pdf\Pdf;

$doc = Pdf::merge([
    'cover.pdf',
    'body.pdf',
    'appendix.pdf',
]);

Pdf::writeToFile($doc, 'report.pdf');
10

Build a PDF from images

Hand it an array of images and each one becomes a page. Useful for turning a batch of scans, screenshots or camera photos into a single document.

This is the exact inverse of extracting pages as images — the same library does the round trip.

Images to PDF in the docs →
from-images.php
use Pop\Pdf\Pdf;

$images = [
    'scans/page-01.jpg',
    'scans/page-02.jpg',
];

// one page per image
$doc = Pdf::importFromImages($images);

Pdf::writeToFile($doc, 'scanned-batch.pdf');