Pop PDF
Introduction

Overview

Pop PDF is a PHP library for working with PDF documents in a number of different ways. You can build PDFs from the ground up - or hand it HTML to have it do the work for you. You can import existing PDFs and build on them. Or, you extract only the parts you need, such as text or images for further processing.

What It Does#

A Document holds pages, and a page holds the things you put on it — text, fonts, images and vector paths and shapes. Build that structure by hand, one call per element, when a layout needs exact placement. Or skip the object calls entirely and pass a string of HTML to Pdf::importFromHtml(), which parses it, applies any CSS it carries, and returns the same kind of Document laid out for you. Either route ends at the same object: write it to a file, stream it to a browser, or read it back with (string) $document.

The same library reads PDFs it did not create. Pdf::importFromFile() opens an existing document and turns its pages, fonts and images into the same objects a document you built holds, so you can add a page, stamp existing ones, or merge several files into one and save the result. Reach the other direction and Pdf::extractTextFromFile() pulls the text layer back out as a string, while Pdf::extractAsImages() rasterizes each page to a JPEG, PNG, WebP or TIFF file.

Building, importing and extracting are all native PHP — the object model that represents a document, the code that writes PDF's own binary format, and the parser that reads it back in are not a wrapper around a system binary or a third-party PDF engine. The one exception is rasterizing pages to image files: that path hands the actual pixel rendering to the imagick extension. Every other capability on this site runs with nothing installed beyond Pop PDF itself and the PHP extensions most installs already carry.

Build, Import, Extract#

Multiple features, one component, instead of having to install multiple components to manage all things PDF:

  • Build
    • Assemble a document from pages, text, fonts, images and drawn paths, style it with color, compression and metadata, and save it to a file, an HTTP response or a string. Start at Creating Documents.
    • Pass valid HTML and CSS to the HTML parser and have it create the object model of the document and everything contained within. Start at HTML to PDF.
  • Import — open an existing PDF as editable objects, add pages to it, stamp text or images onto pages it already has, or merge several files into one. Start at Importing.
  • Extract — pull the text layer out of a PDF, tell scanned pages apart from ones with real text, and rasterize pages to standalone image files. Start at Extracting Text.

Each of these features stands on its own — a script that only extracts text never touches the build side — and all three share the same Document and Page objects, so a document you built and one you imported look identical to any code that works on them afterward.

Where to Start#

Installation covers the Composer require line and a one-file check that everything is wired up. Quickstart walks through five short scripts — text placed by hand, a page built from HTML, an image, importing and stamping a file, and extracting text back out — each one complete on its own. If you want control over where every element lands, start with Creating Documents and work through Build page by page; if you want to hand over a string of HTML and get a PDF back, start at HTML to PDF and skip the rest of Build entirely.

From there, the sidebar splits into Build, Import and Extract, matching the three things above. Build is organized by what you are adding to a page — text, fonts, images, paths — with HTML to PDF as the parser-driven alternative to all of it, and Import and Extract are short enough to read end to end.