Generates pdf files without any dependencies. Includes a layout engine to improve handling of flowing content (e.g. text spanning more than one page).
composer require famoser/pdf-generator
This is still under active development (contributions welcome!), and the public API is subject to change. If you are looking for a more mature project, see https://github.com/tecnickcom/TCPDF.
Using the printer:
// places "Hello world" at coordinate (15/60) coordinates
$document = new LinearDocument();
$bodyText = new TextStyle(Font::createFromDefault());
$printer = $document->createPrinter(0, 15, 60);
$printer->printText("Hello world", $bodyText);
file_put_contents('example.pdf', $document->save());
Using the layout engine:
// places a 20x40 rectangle, followed by "Hello world.".
// placement is decided by Flow.
$flow = new Flow();
$rectangle = new Rectangle(new DrawingStyle());
$rectangleContent = new ContentBlock($rectangle);
$rectangleContent->setWidth(20);
$rectangleContent->setHeight(40);
$flow->addContent($rectangleContent);
$paragraph = new Paragraph();
$paragraph->add($normalText, "Hello ");
$paragraph->add($normalText, "World.");
$flow->addContent($paragraph);
$document->add($flow);
file_put_contents('example.pdf', $document->save());
Layout engine particulars:
- Layouts can be nested (e.g. you can place a flow in another flow)
width
(height
) includes thepadding
, but not themargin
margin
does not collapse with adjacent margins- drawings are ignored during size calculations (e.g.
borderWidth
has no influence on the calculatedwidth
of an element width
(height
) of a child overrideswidth
(height
) of a parent