Quickstart
Five scripts, each complete on its own — paste any one of them into a file and run it with nothing else on the page.
A One-Page Document#
Create a document, add a font, create a page and put text on it:
require __DIR__ . '/vendor/autoload.php';
use Pop\Pdf\Pdf;
use Pop\Pdf\Document;
use Pop\Pdf\Document\Font;
use Pop\Pdf\Document\Page;
$document = new Document();
$document->addFont(Font::ARIAL);
$page = $document->createPage(Page::LETTER);
$page->addText('Hello, Pop PDF.', Font::ARIAL, x: 50, y: 700);
Pdf::writeToFile($document, filename: __DIR__ . '/hello.pdf');
Font::ARIAL is one of the available standard fonts that Font::standardFonts() returns. All PDF readers
support a predefined set of standard fonts that do not require a font file. The method addFont() on the
document object is what makes the name available to addText() calls on any of its pages. The createPage()
method takes a page size constant and returns a page object. The addText() method can take a plain
text string and places its baseline at 50, 700, measured in points from the bottom left corner of the page
(the default origin of a PDF page); a bare string like this one renders at 12 points, the default size of a
Text object.
Building a Page from HTML#
Hand a complete HTML document to importFromHtml() and write the Document it returns the same
way you write one built by hand:
require __DIR__ . '/vendor/autoload.php';
use Pop\Pdf\Pdf;
$html = <<<HTML
<html>
<head>
<style>
h1 { color: #cc0000; }
.total { font-size: 18px; }
</style>
</head>
<body>
<h1>Invoice #1042</h1>
<p>Thanks for your business. Payment is due within 30 days.</p>
<p class="total">Total due: 420.00</p>
</body>
</html>
HTML;
Pdf::writeToFile(Pdf::importFromHtml($html), filename: __DIR__ . '/invoice.pdf');
importFromHtml() parses the markup and the <style> block together and lays out a Document object
from them, autowiring any page, font or text objects needed. The <html> wrapper matters here: a
<head>/<body> fragment without it parses to a page with nothing on it, so a sample carrying a
<style> block keeps the wrapper (a bare fragment with no <head> at all renders fine without
one).
Adding an Image#
Like addText(), the addImage() method can also take a bare string — a path to an image file:
require __DIR__ . '/vendor/autoload.php';
use Pop\Pdf\Pdf;
use Pop\Pdf\Document;
use Pop\Pdf\Document\Font;
use Pop\Pdf\Document\Page;
$document = new Document();
$document->addFont(Font::ARIAL);
$page = $document->createPage(Page::LETTER);
$page->addText('An image on the page:', Font::ARIAL, x: 50, y: 700);
$page->addImage(__DIR__ . '/logo.jpg', x: 50, y: 550);
Pdf::writeToFile($document, filename: __DIR__ . '/with-image.pdf');
The x, y coordinates of the addImage() behave the same as with addText() and place the image's
bottom left corner in relation to the bottom left corner of the page.
Importing and Stamping a File#
Open an existing PDF, add a font to the imported document, and write onto one of its pages:
require __DIR__ . '/vendor/autoload.php';
use Pop\Pdf\Pdf;
use Pop\Pdf\Document\Font;
use Pop\Pdf\Document\Page\Text;
$document = Pdf::importFromFile(__DIR__ . '/deed.pdf');
$document->addFont(Font::ARIAL);
$page = $document->getPage(1);
$page->addText(new Text('DRAFT', size: 18), Font::ARIAL, x: 50, y: 40);
Pdf::writeToFile($document, filename: __DIR__ . '/stamped.pdf');
The importFromFile() method reads deed.pdf and hands back a Document built from its
pages, exactly like one you would have constructed yourself. The getPage(1) method returns a
page object with which to work. Adding anything to the page, like text or an image, is the same
as if you used the createPage() method. This example builds the Text object explicitly rather
than passing a bare string so the size can be controlled directly, setting it at 18.
Extracting Text Back Out#
Pull the text layer out of any PDF as a plain string:
require __DIR__ . '/vendor/autoload.php';
use Pop\Pdf\Pdf;
echo Pdf::extractTextFromFile(__DIR__ . '/essay.pdf');
Most PDF documents contain text strings throughout their structure. Pulling that text out to be processed by another service is a common need. While it's a complex process under the hood, decoding fonts, glyphs and the actual text itself, this method makes it a simple one-liner. It's essentially the same as if you opened the PDF in a reader and dragged your mouse across the entire text within to select and copy it.
See Also#
- Creating Documents — the full
DocumentandPageAPI the object-model scripts on this page sample - HTML to PDF — supported tags, CSS styling from a string, file or URI, and page size, margins and tables
- Importing — page selection, raw data streams and the objects
importFromFile()returns - Extracting Text — page ranges, limits and what comes back for a page with no text