Read pdf files with php

Read pdf files with php

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Parsing PDF Text Layers and Coordinates in PHP

Reading a PDF file to extract specific metadata, such as text layers and their geometric coordinates, requires moving beyond standard file handling functions like fread or SplFileObject. The PDF format is inherently object-based; it does not store data as a continuous stream of characters but rather as discrete objects containing font definitions, image streams, and page descriptions. To access the contents and position of text boxes on a floor plan, you must interact with these internal structures.

The most robust approach in PHP involves using a dedicated library that can parse the PDF's internal representation without requiring manual regex parsing of the raw binary stream. The Smalot/PdfParser library is a popular choice because it exposes an API to retrieve text strings and their bounding boxes. However, for complex layouts like floor maps with multiple layers, understanding the underlying mechanism is crucial before committing to a specific tool.

When you load a PDF into memory, the library reconstructs the document object model (DOM). Text in a PDF is not always stored as a single continuous string per line. Instead, it is often broken down by character or glyph, with each instance having its own position relative to the page origin (0, 0). To map seat locations accurately, you need to aggregate these individual character positions into logical blocks or lines that correspond to your text boxes.

Here is how you can approach this in PHP using Smalot/PdfParser. First, ensure the library is installed via Composer:

composer require smalot/pdfparser

Once installed, you instantiate the parser with the path to your floor map file. The core logic involves iterating through the text items returned by the parser and mapping them back to their page coordinates.

use Smalot\PdfParser\PDFParser;

$path = '/path/to/floor_map.pdf';
$parser = new PDFParser($path);
$pages = $parser->getPages();

foreach ($pages as $page) {
    $texts = $page->getTextItems();

    foreach ($texts as $textItem) {
        // Extract the raw text content
        $content = $textItem->getContent();
        
        // Get the bounding box coordinates relative to the page
        // Usually returned as an array or object with x, y, width, height
        $coords = $textItem->getCoordinates(); 
        
        echo "Text: {$content}\n";
        print_r($coords);
    }
}

The getCoordinates() method is the critical piece here. It returns the bounding box of the text string on the page. For a floor map, this allows you to determine exactly where a seat label sits relative to the furniture layers defined in the PDF. You can then perform your own logic to cluster these points if they belong to the same logical "seat location."

A significant trade-off to consider is the distinction between extracting text and extracting content. The Smalot/PdfParser library relies on the font encoding within the PDF. If the floor map was generated with custom fonts or if the text layers are not properly defined as standard text objects (which happens in scanned images or encrypted PDFs), the library might return empty strings for those sections. In such cases, OCR (Optical Character Recognition) becomes necessary, which introduces its own complexity and error rates.

Furthermore, coordinate systems can be tricky. The coordinates returned by the parser are typically relative to the bottom-left corner of the page in user space units. If your PDF uses a specific points-per-inch scale or has non-standard rotation, you must ensure your mapping logic accounts for these transformations before plotting the seat locations on your application's canvas.

If you find that pure PHP becomes a bottleneck due to heavy parsing overhead or complex rendering requirements, shifting the processing to a language with stronger graphics libraries, such as Python with PyMuPDF or Ruby with pdf-reader, might be a viable architectural decision. These languages often offer more mature support for high-fidelity PDF manipulation.

Ultimately, the success of this operation depends on the integrity of the source PDF. If the text layers are embedded cleanly, PHP can handle the extraction efficiently. If the data is obfuscated or image-based, you will need to preprocess the document before attempting to read it programmatically.

Our network