smalot / smalot/pdfparser

how can I get image and text from pdf file ?

Open
#487 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
PHP
Stars
2.7k
Forks
579
Avg merge
2m
Merged PRs (30d)
1

Description

I get all image or all text from pdf with no typesetting
now,
I want to get the image and text in the whole pdf according to the layout
How do I do it?

get text code

<?php
require './vendor/autoload.php';

use Smalot\PdfParser\XObject\Form;
use Smalot\PdfParser\XObject\Image;

$fileSavePath = "./doc/img/";
$filename = "./doc/test.pdf";

toRun($filename,$fileSavePath);

 function toRun($filename,$fileSavePath){
    $parser = new \Smalot\PdfParser\Parser();
    $pdf    = $parser->parseFile($filename);
    $pages  = $pdf->getPages();
    foreach ($pages as $page) {
        echo $page->getText();
    }

}

get img text

<?php
require './vendor/autoload.php';
use Smalot\PdfParser\XObject\Form;
use Smalot\PdfParser\XObject\Image;

$fileSavePath = "./doc/img/";
$filename = "./doc/test.pdf";

toRun($filename,$fileSavePath);

/**
 * @throws \Exception
 */
 function toRun($filename,$fileSavePath){
     $parser = new \Smalot\PdfParser\Parser();
     $pdf    = $parser->parseFile($filename);
     $i = 0;
     $xobjects  = $pdf->getObjectsByType('XObject');
     foreach( $xobjects  as $obj ) {
         if ($obj instanceof Image) {
             $content = $obj->getContent();
             file_put_contents($fileSavePath. ++$i .".png", $content);
         }
     }
}

?>

Look forward to your reply

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the Parser usage shown in the issue, especially getPages(), getText(), getObjectsByType('XObject'), and the Image handling example. Clarify the expected layout-preserving output and behavior for the supplied PDF before determining what implementation and tests would be needed; done means extracting text and images in the requested document order.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.