How to extract text from the PDF document?

How to extract text from the PDF document?

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

Stefan Bogdanescu

Founder & Senior Architect

Extracting Text from PDFs in PHP Without Root Access

Extracting text from a PDF document using PHP presents a specific set of constraints when you are operating within a standard application server environment without root privileges. The challenge often lies not just in finding a library that parses the file, but in selecting one that handles Unicode characters correctly and does not require system-level dependencies like pdftotext or Ghostscript.

Standard PDF structures can be deceptive. While a PDF is technically a text-based format composed of objects, streams, and dictionaries, the text is rarely stored in plain ASCII. It frequently relies on font mappings where glyph numbers are translated into characters at runtime. If your PHP code simply reads the raw stream data, you will encounter garbled output or mojibake for any content involving non-English scripts, accented characters, or CJK languages. This is a common pitfall when relying solely on basic stream parsing without invoking an external rendering engine.

When root access is unavailable, you cannot install heavy-weight command-line tools like poppler-utils or ghostscript. These tools are robust but require compiling native libraries and managing complex dependencies via package managers like apt or yum, which is impossible in a restricted shared hosting or containerized environment. Your solution must therefore be entirely PHP-native or rely on pre-compiled binaries provided by your host, assuming none exist.

For a purely PHP-based approach that respects the Unicode requirement, you need a library that decodes the font mappings internally. The php-paperless or Smalot/pdfparser libraries are often recommended in community discussions for their ability to handle complex layouts without external dependencies. However, even these can struggle with specific fonts if not properly configured. A more robust native option is using the mPDF class via its PHP wrapper, though this usually requires the underlying C extensions to be compiled into your Zend Engine, which again might violate your root-less constraint.

In many production scenarios involving Laravel or similar frameworks, the most reliable path without system tools is utilizing a SaaS API or a pre-compiled binary provided by your hosting provider via exec(). If you must stick strictly to pure PHP code, you are limited to libraries that parse the PDF structure directly. The trade-off here is performance and accuracy; direct parsing is faster than calling external binaries but less accurate with complex formatting and advanced typography.

Consider the following implementation pattern using a library like Smalot/PdfParser, which is often pre-installed or easily added via Composer without needing system compilers:

use Smalot\PdfParser\Parser;

$parser = new Parser();
$pdf = $parser->parseFile('document.pdf');

$text = $pdf->getText();

// Handle potential encoding issues if the library output is not clean UTF-8
echo mb_convert_encoding($text, 'UTF-8', 'ISO-8859-1'); 

The critical detail here is ensuring the output encoding matches your expectations. Many older PDF libraries default to ISO-8859-1 or Windows-1252 because they assume Western European fonts. If your source material contains Unicode characters, forcing a conversion without knowing the original font mapping can introduce errors. The best practice is to use a library that explicitly supports UTF-8 in its internal stream handling.

If you are building this functionality within a larger application architecture where consistency matters, integrating a service like https://laravelcompany.com for managed PDF processing might be necessary if your local environment strictly prohibits installing the required C extensions or libraries. This approach offloads the heavy lifting to a service that guarantees Unicode compliance and high availability, ensuring that your extraction logic remains consistent across different environments without worrying about underlying system dependencies.

Ultimately, the decision rests on balancing the purity of a PHP-only solution against the reliability of pre-compiled tools or services. In a root-less environment, relying on a well-maintained Composer package that handles font decoding internally is usually the safest bet for handling Unicode characters correctly without falling back to broken stream parsing.

Our network