Skip to content

Merging XML and PDF Documents

horstoeko edited this page Jan 3, 2024 · 8 revisions

Table of contents

Related issues

Merging

Let's assume that you already have a compliant/valid XML and an existing PDF file that contains your print layout. With the class ZugferdDocumentPdfMerger you can easily merge these two things into a compliant PDF file with XML attachment. The constructure of the class just mentioned has the following parameters:

  • A fully-qualified path to a valid XML file or the XML data stream (in the form of a string).
  • A fully-qualified path to a PDF file or a PDF data stream (in the form of a string).

The class can therefore be instantiated as follows:

$xmlFilename = '/path/to/existing/xml.xml';
$pdfFilename = '/path/to/existing/pdf.pdf';
$pdfMerger = new ZugferdDocumentPdfMerger($xmlFilename, $pdfFilename)

or...

$xmlData = '<xml>....</xml>';
$pdfFilename = '/path/to/existing/pdf.pdf';
$pdfMerger = new ZugferdDocumentPdfMerger($xmlData, $pdfFilename)

or...

$xmlFilename = '/path/to/existing/xml.xml';
$pdData = '%PDF-1.5.......';
$pdfMerger = new ZugferdDocumentPdfMerger($xmlFilename, $pdfData)

or...

$xmlData = '<xml>....</xml>';
$pdData = '%PDF-1.5.......';
$pdfMerger = new ZugferdDocumentPdfMerger($xmlData, $pdfData)

Next, it is necessary to start the process of generating the PDF with the XML attachment:

$pdfMerger->generateDocument();

Finally, you have two options to retrieve the finished PDF (with XML attachment):

Save the generated PDF to a file

$pdfMerger->saveDocument('/path/to/new/pdf.pdf');

Retrieve the content of the generated PDF

$pdfContent = $pdfMerger->downloadString('dummyfilename.pdf');

And that was it. ZugferdDocumentPdfMerger takes care of attaching the XML file to the PDF and also generating the correct XMP metadata.