Skip to content

Reading PDF Documents

HorstOeko edited this page Jan 19, 2025 · 12 revisions

Table of contents

Related issues

  • Nothing found

Additional documentation

Examples

Read a pdf file with attached xml file

Reading invoice data from a PDF is similar: you just need to use the ZugferdDocumentPdfReader class instead of ZugferdDocumentReader:

use horstoeko\zugferd\ZugferdDocumentPdfReader;

$document = ZugferdDocumentPdfReader::readAndGuessFromFile('/path/to/some.pdf');

If the content of a PDF is already in a variable, the following procedure can also be selected:

use horstoeko\zugferd\ZugferdDocumentPdfReader;

$pdfContent = file_get_contents('/path/to/some.pdf');
$document = ZugferdDocumentPdfReader::readAndGuessFromContent($pdfContent);

All variants return an instance of ZugferdDocumentReader. See also Reading XML documents

Internally, the class ZugferdDocumentPdfReader uses the class ZugferdDocumentPdfReaderExt described below. This was done for reasons of downward compatibility.

Read additional PDF attachments

Additional attachments may be attached to the PDF to be read. These can be read in quite easily with the class ZugferdDocumentPdfReaderExt:

$additionalDocuments = ZugferdDocumentPdfReaderExt::getAdditionalDocumentContentsFromFile(
    '/path/to/some.pdf'
);

$additionalDocuments is an array which you can use in the following way

$firstAttachment = $additionalDocuments[0];
$secondAttachment = $additionalDocuments[1];

// Resolve information from first attachment

$firstAttachmentContent = $firstAttachment[ZugferdDocumentPdfReaderExt::ATTACHMENT_KEY_CONTENT];
$firstAttachmentFilename = $firstAttachment[ZugferdDocumentPdfReaderExt::ATTACHMENT_KEY_FILENAME];
$firstAttachmentMimeType = $firstAttachment[ZugferdDocumentPdfReaderExt::ATTACHMENT_KEY_MIMETYPE];

The variables can contain the following values for example:

Variable Example value
$firstAttachmentContent %PDF-1.4 ......
$firstAttachmentFilename timetable.pdf
$firstAttachmentMimeType application/pdf
Clone this wiki locally