smalot / smalot/pdfparser

Missing encoding data for "" error - fix suggestion

Open
#462 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I had been getting this error for a while and the fix had been to export the PDF with embedded fonts. However I think I have found the real issue.

As 'BaseEncoding' in a PDF is optional so some PDFs may have it null, this will cause the exception.

A suggested fix would be to set 'StandardEncoding' as default if base encoding is null. This may cause the PDF to look different as the user intended, however it will make it parseable instead of chucking the exception.

Code change to the getEncodingClass() function on Encoding.php

    protected function getEncodingClass()
    {
        // Load reference table charset.
        $baseEncoding = preg_replace('/[^A-Z0-9]/is', '', $this->get('BaseEncoding')->getContent());

        //fix for null BaseEncoding set in PDF, will default to standard encoding
	if(!$baseEncoding){
		$baseEncoding = 'StandardEncoding';
	}

        $className = '\\Smalot\\PdfParser\\Encoding\\'.$baseEncoding;

        if (!class_exists($className)) {
            throw new Exception('Missing encoding data for: "'.$baseEncoding.'".');
        }

        return $className;
    }

I am sorry if this has already been solved or not an applicable fix, I just wanted to let you know something that worked and hopefully give some insight into the error that had me head scratching for a while.

If this isn't submitted to the official GIT at least the solution will be here incase anyone else runs into this issue. Thanks.

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 in Encoding.php at getEncodingClass() and inspect how a missing BaseEncoding value is handled. Confirm that an affected PDF can be parsed without the missing-encoding exception, while preserving the existing behavior for PDFs that provide BaseEncoding.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.