PHP Requires Chilkat v11.7.0+
Chilkat2 PHP
Extract PDF Text to a StringBuilder and Save to a File
See more PDF Text Extraction Examples
Demonstrates ExtractTextSb, which writes the extracted text to a StringBuilder instead of returning it as a JSON string. Use it for large documents. The JSON output still receives the quality report.Chilkat PHP Downloads
<?php
use Chilkat2\JsonObject;
use Chilkat2\Pdf;
use Chilkat2\StringBuilder;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example is available at long_report.pdf
$pdf = new Pdf();
try {
$pdf->LoadFile('qa_data/pdf/long_report.pdf');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'Number of pages: ', $pdf->NumPages, "\n";
// ExtractTextSb takes the same operation and options as ExtractText, but the text
// goes to a StringBuilder. This avoids building a very large JSON string for
// documents with many pages.
$json = new JsonObject();
$sbText = new StringBuilder();
$jsonOut = new JsonObject();
try {
$pdf->ExtractTextSb('text', $json, $sbText, $jsonOut);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'Extracted ', $sbText->Length, " characters.\n";
// Save the text to a file.
try {
$sbText->WriteFile('qa_output/long_report.txt', 'utf-8', false);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
// The JSON output does not contain the text, but it still has the quality report.
echo 'Quality: ', $jsonOut->StringOf('quality.rating'), "\n";
echo 'Pages with no text (need OCR): [', $jsonOut->StringOf('quality.pagesNeedingOcr'), "]\n";
// Sample output:
// Number of pages: 3
// Extracted 4522 characters.
// Quality: good
// Pages with no text (need OCR): []