PHP code example of vuthaihoc / pdfparser

1. Go to this page and download the library: Download vuthaihoc/pdfparser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vuthaihoc / pdfparser example snippets


use PrinsFrank\PdfParser\PdfParser;
use PrinsFrank\PdfParser\Render\PageRenderer;
use PrinsFrank\PdfParser\Render\RenderOptions;

$document = (new PdfParser())->parseFile('document.pdf');
$renderer = new PageRenderer($document, RenderOptions::preset('balanced'));
$result = $renderer->render($document->getPage(1), 300); // 300 px wide

$result->image->writeImage('page-1.png'); // Imagick
$result->score;   // 0..1: how close the image probably is to a real renderer, from what was skipped or approximated
$result->issues;  // what was skipped or approximated, with the share of the page it covers
$result->timings; // seconds per stage: parse, interpret, text, fonts, image_decode, image_place, shading, rasterize, ...
$result->memory;  // PHP and ImageMagick peaks

use PrinsFrank\PdfParser\Render\Crop;

$renderer->render($document->getPage(3), 600, Crop::fractions(0.1, 0.2, 0.9, 0.5)); // shares of the displayed page
$renderer->render($document->getPage(3), 600, Crop::pixels(141, 162, 921, 426, 1000)); // pixels on a 1000 px wide rendering

use PrinsFrank\PdfParser\Stream\HttpRangeStream;

$document = (new PdfParser())->parse(HttpRangeStream::open('https://example.com/document.pdf'));
$document->getPage(1)?->getText(); // reads the trailer, cross reference and objects of that page only

use PrinsFrank\PdfParser\PdfParser;

$document = (new PdfParser())
    ->parseFile($path);

$document->getText();

use PrinsFrank\PdfParser\PdfParser;

$document = (new PdfParser())
    ->parseFile($path);

$document->getMarkdown();

use PrinsFrank\PdfParser;

$document = (new PdfParser())
    ->parseFile(dirname(__DIR__, 3) . '/path/to/file.pdf');

use PrinsFrank\PdfParser;

$pdfAsString = file_get_contents(dirname(__DIR__, 3) . '/path/to/file.pdf');

$document = (new PdfParser())
    ->parseString($pdfAsString);

$page = $document->getObject(42, Page::class);

/** @var \PrinsFrank\PdfParser\Document\Document $document */
$title = $document->getInformationDictionary()?->getTitle(); 
$producer = $document->getInformationDictionary()?->getProducer(); 
$author = $document->getInformationDictionary()?->getAuthor(); 
$creator = $document->getInformationDictionary()?->getCreator(); 
$creationDate = $document->getInformationDictionary()?->getCreationDate(); 
$modificationDate = $document->getInformationDictionary()?->getModificationDate(); 

/** @var \PrinsFrank\PdfParser\Document\Document $document */
$dictionary = $document->getInformationDictionary()?->getDictionary();

use PrinsFrank\PdfParser\PdfParser;

$document = (new PdfParser())->parseFile('/path/to/file.pdf');

foreach ($document->getPages() as $index => $page) {
    echo 'Text on page ' . $index . ' : ' . $page->getText();
}

/** @var \PrinsFrank\PdfParser\Document\Document $document */
foreach ($document->getImages() as $index => $image) {
    if (($imageFileExtension = $image->getImageType()?->getFileExtension()) === null) {
        continue; // You could still save the file with a default file extension like 'jpg', but it is not clear what kind of image this is.
    }

    file_put_contents(sprintf('%s/image_%d.%s', __DIR__, $index, $imageFileExtension), $image->getContent());
}