PDF to Text PHP Examples
Complete PHP examples for common PDF to Text tasks. See the PHP SDK guide for installation and result handling.
You can run the examples below with the displayed API credentials. Update the input URLs and filenames as needed. Each PHP example can be copied and run independently.
Basic examples
Choose file, in-memory, or output-stream handling for the text result. Stream examples use a binary file stream as their destination.
The memory variants return a PHP string containing UTF-8 text. You can use it directly without a separate decoding step.
Convert a PDF file
Upload an existing invoice.pdf file.
Save to a file
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $client->convertFileToFile('invoice.pdf', 'invoice.txt');
Keep in memory
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $text_bytes = $client->convertFile('invoice.pdf'); // binary string echo strlen($text_bytes), PHP_EOL;
Write to a stream
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $output_stream = fopen('invoice.txt', 'wb'); if ($output_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $client->convertFileToStream('invoice.pdf', $output_stream); if (!fflush($output_stream)) { throw new \RuntimeException('Cannot flush output stream'); } } finally { fclose($output_stream); }
Convert a URL
Fetch a PDF from its URL.
Save to a file
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $client->convertUrlToFile('https://your-server.com/invoice.pdf', 'invoice.txt');
Keep in memory
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $text_bytes = $client->convertUrl('https://your-server.com/invoice.pdf'); // binary string echo strlen($text_bytes), PHP_EOL;
Write to a stream
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $output_stream = fopen('invoice.txt', 'wb'); if ($output_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $client->convertUrlToStream( 'https://your-server.com/invoice.pdf', $output_stream ); if (!fflush($output_stream)) { throw new \RuntimeException('Cannot flush output stream'); } } finally { fclose($output_stream); }
Convert raw PDF bytes
Read invoice.pdf as binary data. In an application, pass the bytes you already hold in memory instead.
Save to a file
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $pdf_data_1 = file_get_contents('invoice.pdf'); // binary string if ($pdf_data_1 === false) { throw new \RuntimeException('Cannot read binary input'); } $client->convertRawDataToFile($pdf_data_1, 'invoice.txt');
Keep in memory
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $pdf_data_1 = file_get_contents('invoice.pdf'); // binary string if ($pdf_data_1 === false) { throw new \RuntimeException('Cannot read binary input'); } $text_bytes = $client->convertRawData($pdf_data_1); // binary string echo strlen($text_bytes), PHP_EOL;
Write to a stream
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $output_stream = fopen('invoice.txt', 'wb'); if ($output_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $pdf_data_1 = file_get_contents('invoice.pdf'); // binary string if ($pdf_data_1 === false) { throw new \RuntimeException('Cannot read binary input'); } $client->convertRawDataToStream($pdf_data_1, $output_stream); if (!fflush($output_stream)) { throw new \RuntimeException('Cannot flush output stream'); } } finally { fclose($output_stream); }
Read from an input stream
Open invoice.pdf as a binary input stream and save the result to invoice.txt.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $input_stream = fopen('invoice.pdf', 'rb'); if ($input_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $client->convertStreamToFile($input_stream, 'invoice.txt'); } finally { fclose($input_stream); }
Use extracted text
Convert a PDF URL and print the returned UTF-8 string.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $text = $client->convertUrl('https://your-server.com/invoice.pdf'); // UTF-8 string echo $text, PHP_EOL;
Customize text extraction
These examples upload extraction.pdf, a three-page document with a two-column article, wrapped paragraphs, and invoice fields.
Extract selected pages
Extract pages 1 and 3 of extraction.pdf into excerpt.txt.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $client->setPrintPageRange('1,3'); $client->convertFileToFile('extraction.pdf', 'excerpt.txt');
Extract text without layout spacing
Extract the article on page 1 into article.txt without spacing that recreates the PDF's columns. For tables, the default layout mode usually preserves column alignment better.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $client->setNoLayout(true); $client->setPrintPageRange('1'); $client->convertFileToFile('extraction.pdf', 'article.txt');
Reconstruct wrapped paragraphs
Extract page 2 into paragraphs.txt, joining wrapped lines with paragraph detection and removing line-end hyphens. For example, infor- followed by mation becomes information. Paragraph detection depends on the document's layout.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $client->setParagraphMode('bounding-box'); $client->setPrintPageRange('2'); $client->setRemoveHyphenation(true); $client->convertFileToFile('extraction.pdf', 'paragraphs.txt');
Extract text from a page region
Crop away the headers and footers on every page, or extract only the customer, invoice, and reference fields on page 3.
The coordinates below match a 612 × 792 point (US Letter) page. Measure X and Y from the top-left corner; 72 points equal one inch. Adjust the regions for your PDF.
Exclude headers and footers
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $client->setCropAreaHeight(648); $client->setCropAreaWidth(540); $client->setCropAreaX(36); $client->setCropAreaY(72); $client->convertFileToFile('extraction.pdf', 'body.txt');
Extract an invoice field block
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $client->setCropAreaHeight(70); $client->setCropAreaWidth(300); $client->setCropAreaX(36); $client->setCropAreaY(290); $client->setPrintPageRange('3'); $client->convertFileToFile('extraction.pdf', 'fields.txt');
Keep page boundaries
The default output has no page separators. Insert form-feed characters (\f) for processing text by page, or add a visible --- PAGE BREAK --- marker on its own line.
Use form-feed separators
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $client->setPageBreakMode('default'); $client->convertFileToFile('extraction.pdf', 'pages.txt');
Use visible separators
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToTextClient('demo', 'demo'); $client->setCustomPageBreak(' --- PAGE BREAK --- '); $client->setPageBreakMode('custom'); $client->convertFileToFile('extraction.pdf', 'separated.txt');