Page Transformer
PageTransformer modifies page geometry — rotation, scaling, and page box dimensions — using incremental updates so the original content is preserved intact.
Opening a PDF
Section titled “Opening a PDF”use Phpdftk\Pdf\Toolkit\PageTransformer;
// From file$transformer = PageTransformer::open('input.pdf');
// From string$transformer = PageTransformer::openString($pdfBytes);
// Encrypted PDF$transformer = PageTransformer::open('secured.pdf', password: 'secret');Rotating pages
Section titled “Rotating pages”Rotation must be a multiple of 90 degrees. Rotations accumulate with any existing page rotation.
// Rotate all pages$transformer->rotate(90);
// Rotate specific pagesuse Phpdftk\Pdf\Toolkit\PageSelector;
$transformer->rotate(180, PageSelector::pages(1, 3));$transformer->rotate(270, PageSelector::even());Scaling pages
Section titled “Scaling pages”Uniform scale factor
Section titled “Uniform scale factor”Multiplies all page box dimensions (MediaBox, CropBox, TrimBox, BleedBox, ArtBox) by the given factor:
// Scale all pages to 50%$transformer->scale(0.5);
// Scale specific pages to 150%$transformer->scale(1.5, PageSelector::pages(1));Scale to fit dimensions
Section titled “Scale to fit dimensions”Computes a uniform scale factor to fit pages within the target width and height (in points):
// Scale all pages to fit within A5$transformer->scaleTo(420, 595);
// Scale specific pages$transformer->scaleTo(300, 400, PageSelector::range(2, 5));Setting page boxes
Section titled “Setting page boxes”Set any of the standard page boxes. Values are in points: x and y specify the lower-left corner, w and h specify width and height.
CropBox
Section titled “CropBox”$transformer->setCropBox(36, 36, 540, 720);
// On specific pages$transformer->setCropBox(0, 0, 300, 400, PageSelector::pages(1));MediaBox
Section titled “MediaBox”$transformer->setMediaBox(0, 0, 612, 792); // Letter sizeTrimBox
Section titled “TrimBox”$transformer->setTrimBox(18, 18, 576, 756);BleedBox
Section titled “BleedBox”$transformer->setBleedBox(9, 9, 594, 774);Chaining operations
Section titled “Chaining operations”All operations are fluent and can be combined:
PageTransformer::open('input.pdf') ->rotate(90) ->setCropBox(0, 0, 500, 700, PageSelector::pages(1)) ->scale(0.8, PageSelector::range(2, 10)) ->save('output.pdf');Saving
Section titled “Saving”// To file$transformer->save('output.pdf');
// To string$bytes = $transformer->toBytes();Document info
Section titled “Document info”$transformer->getPageCount(); // intEscape hatch
Section titled “Escape hatch”$reader = $transformer->getReader(); // PdfReader