smalot / smalot/pdfparser

Embedded png image cannot be parsed

Open
#749 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

  • PHP Version: 7.4.11
  • PDFParser Version: 2.11
Description:

Hello, I am trying to parse a pdf that contains png images but I can't do it. The content of the image seems corrupted which triggers an error with zlib_decode
Thanks for your help

PDF input

https://telechargements.soludedia.fr/divers/LF6451.pdf

Expected output & actual output
Code
if ($f = file_get_contents('file.pdf')) {
	include "./inc/pdfparser-2024/alt_autoload.php";
	
	$parser = new \Smalot\PdfParser\Parser();

	$document = $parser->parseContent($f);
	$pages    = $document->getPages();
	$page     = $pages[0];
	$content  = $page->getText();
	
	$images = $document->getObjectsByType('XObject', 'Image');
	foreach($images as $index=>$image) {
		$img_content = $image->getContent();
		$details = $image->getHeader()->getDetails();

		if ('FlateDecode' === $image->getHeader()->getElements()['Filter']->getContent()) {
			$img_content = zlib_decode($img_content);
			echo '<img src="data:image/png;base64,'. base64_encode($img_content) .'" />';
		}
	}
}

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 reproducing the issue with the linked LF6451.pdf and the provided parseContent, getObjectsByType, getContent, and getHeader calls. Inspect how FlateDecode image XObjects are exposed before the caller invokes zlib_decode. Done means the embedded PNG content can be parsed without the reported zlib error, with a regression test for the supplied PDF if the project supports one.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.