Skip to content

Getting Started

Dragon edited this page Jun 3, 2026 · 1 revision

Getting Started

This page walks you through the minimum steps to create a PDF document: instantiating Document, adding pages, choosing a unit system, and writing the file to disk or memory.

Creating a document

use DragonOfMercy\PhpPdf\Document;

$pdf = new Document();
$pdf->addPage();
$pdf->save('out.pdf');

$pdf->output() returns the PDF bytes as a string instead of writing to disk - useful for streaming a response or running in-memory tests.

Pages and formats

addPage() accepts an optional PageFormat case, a custom dimensions array, and/or an Orientation value:

use DragonOfMercy\PhpPdf\{Document, PageFormat, Orientation, Unit};

$pdf = new Document();                              // mm by default
$pdf->addPage();                                    // A4 portrait
$pdf->addPage(PageFormat::A6);                      // A6 portrait
$pdf->addPage();                                    // A6 portrait (remembered)
$pdf->addPage(orientation: Orientation::LANDSCAPE); // last format in landscape
$pdf->addPage([99, 38]);                            // custom 99x38 mm (label)

Available formats: A3, A4, A5, A6, LETTER, LEGAL.

The document remembers the last format and orientation, so a multi-page label sheet only needs addPage([99, 38]) once. Passing a PageFormat clears any custom dimensions; for custom arrays, the orientation argument is ignored - you provide the dimensions in the order you want.

Units

The document works in millimetres by default. To switch the whole document to PDF points:

$pdf = new Document(Unit::PT);
$pdf->addPage(); // 595.28 x 841.89 pt

Two things to keep in mind regardless of the document unit:

  • Font sizes and leading are always in points (typographic convention). setFont() and setFontSize() always take point values.
  • The Y axis is top-down, with the origin at the top-left corner of the page. This differs from native PDF coordinates; the library flips to PDF bottom-up at serialization time.

Default font

Pages created via addPage() start with Helvetica 11 already set, so simple uses can call cell(), text(), and stringWidth() right away. To change the document-wide default before any addPage() call:

use DragonOfMercy\PhpPdf\{Document, Font};

$doc = new Document();
$doc->setDefaultFont(Font::times(), 11);
$doc->addPage(); // starts with Times Roman 11

See also

Clone this wiki locally