Skip to content

Class ZugferdDocumentPdfMerger

HorstOeko edited this page Dec 31, 2024 · 25 revisions

Summary

Class representing the facillity adding existing XML data (file or data-string)
to an existing PDF with conversion to PDF/A

Example

Example for merging an existing PDF file with an existing XML file

use horstoeko\zugferd\ZugferdDocumentPdfMerger;

$existingXmlFile = "/tmp/invoice_1.xml";
$existingPdfFile = "/tmp/emptypdf.pdf";
$mergeToPdf = "/tmp/fullpdf.pdf";

if (!file_exists($existingXmlFile) || !file_exists($existingPdfFile)) {
    throw new \Exception("XML and/or PDF does not exist");
}

(new ZugferdDocumentPdfMerger($existingXmlFile, $existingPdfFile))
    ->generateDocument()
    ->saveDocument($mergeToPdf);

Example for merging an existing PDF file with a string which contains the XML

use horstoeko\zugferd\ZugferdDocumentPdfMerger;

$xmlString= "<xml>...</xml>";
$existingPdfFile = "/tmp/emptypdf.pdf";
$mergeToPdf = "/tmp/fullpdf.pdf";

if (!file_exists($existingPdfFile)) {
    throw new \Exception("XML and/or PDF does not exist");
}

(new ZugferdDocumentPdfMerger($xmlString, $existingPdfFile))
    ->generateDocument()
    ->saveDocument($mergeToPdf);

Example for merging a string which contains a PDF with an existing XML file

use horstoeko\zugferd\ZugferdDocumentPdfMerger;

$existingXmlFile = "/tmp/invoice_1.xml";
$pdfString = "%PDF-1.4.....";
$mergeToPdf = "/tmp/fullpdf.pdf";

if (!file_exists($existingXmlFile)) {
    throw new \Exception("XML and/or PDF does not exist");
}

(new ZugferdDocumentPdfMerger($existingXmlFile, $pdfString))
    ->generateDocument()
    ->saveDocument($mergeToPdf);

Example for merging a string which contains a PDF with a string which contains the XML

use horstoeko\zugferd\ZugferdDocumentPdfMerger;

$xmlString= "<xml>...</xml>";
$pdfString = "%PDF-1.4.....";
$mergeToPdf = "/tmp/fullpdf.pdf";

(new ZugferdDocumentPdfMerger($xmlString, $pdfString))
    ->generateDocument()
    ->saveDocument($mergeToPdf);

Methods

__construct

Summary

Constructor

Signature

public function __construct(string $xmlDataOrFilename, string $pdfData): void
{
}

Parameters

Name Type Allows Null Description
xmlDataOrFilename string
The XML data as a string or the full qualified path to an XML-File
containing the XML-data
pdfData string
The full filename or a string containing the binary pdf data. This
is the original PDF (e.g. created by a ERP system)

generateDocument

Summary

Generates the final document

Signature

public function generateDocument(): static
{
}

Returns

Returns a value of type static

saveDocument

Summary

Saves the generated PDF document to a file

Signature

public function saveDocument(string $toFilename): static
{
}

Parameters

Name Type Allows Null Description
toFilename string The full qualified filename to which the generated PDF (with attachment)is stored

Returns

Returns a value of type static

saveDocumentInline

Summary

Starts a HTTP download of the generated PDF document

Signature

public function saveDocumentInline(string $toFilename): string
{
}

Parameters

Name Type Allows Null Description
toFilename string

Returns

Returns a value of type string

downloadString

Summary

Returns the content of the generared PDF as a string

Signature

public function downloadString(): string
{
}

Returns

Returns a value of type string

setAdditionalCreatorTool

Summary

Sets an additional creator tool (e.g. the ERP software that called the PHP library)

Signature

public function setAdditionalCreatorTool(string $additionalCreatorTool): static
{
}

Parameters

Name Type Allows Null Description
additionalCreatorTool string The name of the creator

Returns

Returns a value of type static

setAttachmentRelationshipType

Summary

Set the type of relationship for the XML attachment. Allowed
types are 'Data', 'Alternative' and 'Source'

Signature

public function setAttachmentRelationshipType(string $relationshipType): static
{
}

Parameters

Name Type Allows Null Description
relationshipType string Type of relationship

Returns

Returns a value of type static

setAttachmentRelationshipTypeToData

Summary

Set the type of relationship for the XML attachment to "Data"

Signature

public function setAttachmentRelationshipTypeToData(): static
{
}

Returns

Returns a value of type static

setAttachmentRelationshipTypeToAlternative

Summary

Set the type of relationship for the XML attachment to "Alternative"

Signature

public function setAttachmentRelationshipTypeToAlternative(): static
{
}

Returns

Returns a value of type static

setAttachmentRelationshipTypeToSource

Summary

Set the type of relationship for the XML attachment to "Source"

Signature

public function setAttachmentRelationshipTypeToSource(): static
{
}

Returns

Returns a value of type static

attachAdditionalFileByRealFile

Summary

Attach an additional file to PDF. The file that is specified in $fullFilename
must exists

Signature

public function attachAdditionalFileByRealFile(
  string $fullFilename,
  string $displayName = '',
  string $relationshipType = '',
): static
{
}

Parameters

Name Type Allows Null Description
fullFilename string
displayName string
relationshipType string

Returns

Returns a value of type static

attachAdditionalFileByContent

Summary

Attach an additional file to PDF by a content string

Signature

public function attachAdditionalFileByContent(
  string $content,
  string $filename,
  string $displayName = '',
  string $relationshipType = '',
): static
{
}

Parameters

Name Type Allows Null Description
content string
filename string
displayName string
relationshipType string

Returns

Returns a value of type static

setDeterministicModeEnabled

Summary

Set the the deterministic mode. This mode should only be used
for testing purposes

Signature

public function setDeterministicModeEnabled(bool $deterministicModeEnabled): static
{
}

Parameters

Name Type Allows Null Description
deterministicModeEnabled bool

Returns

Returns a value of type static

setAuthorTemplate

Summary

Set the template for the author meta information

Signature

public function setAuthorTemplate(string $authorTemplate): static
{
}

Parameters

Name Type Allows Null Description
authorTemplate string

Returns

Returns a value of type static

setKeywordTemplate

Summary

Set the template for the keyword meta information

Signature

public function setKeywordTemplate(string $keywordTemplate): static
{
}

Parameters

Name Type Allows Null Description
keywordTemplate string

Returns

Returns a value of type static

setTitleTemplate

Summary

Set the template for the title meta information

Signature

public function setTitleTemplate(string $titleTemplate): static
{
}

Parameters

Name Type Allows Null Description
titleTemplate string

Returns

Returns a value of type static

setSubjectTemplate

Summary

Set the template for the subject meta information

Signature

public function setSubjectTemplate(string $subjectTemplate): static
{
}

Parameters

Name Type Allows Null Description
subjectTemplate string

Returns

Returns a value of type static

setMetaInformationCallback

Summary

Set the user defined callback for generating custom meta information

Signature

public function setMetaInformationCallback(?callable $callback = null): static
{
}

Parameters

Name Type Allows Null Description
callback callable ✔️

Returns

Returns a value of type static

Clone this wiki locally