Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#208 Customizable PDF Metadata -> Added Templates and Callback Function #212

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 160 additions & 4 deletions src/ZugferdDocumentPdfBuilderAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Throwable;
use horstoeko\mimedb\MimeDb;
use horstoeko\stringmanagement\FileUtils;
use horstoeko\stringmanagement\StringUtils;
use horstoeko\zugferd\codelists\ZugferdInvoiceType;
use horstoeko\zugferd\exception\ZugferdFileNotFoundException;
use horstoeko\zugferd\exception\ZugferdFileNotReadableException;
Expand Down Expand Up @@ -82,6 +83,41 @@ abstract class ZugferdDocumentPdfBuilderAbstract
*/
private $additionalFilesToAttach = [];

/**
* User-defined template for the author-metainformation
*
* @var string
*/
private $authorTemplate = "";

/**
* User-defined template for the keyword-metainformation
*
* @var string
*/
private $keywordTemplate = "";

/**
* User-defined template for the title-metainformation
*
* @var string
*/
private $titleTemplate = "";

/**
* User-defined template for the subject-metainformation
*
* @var string
*/
private $subjectTemplate = "";

/**
* User-defined callback function for all metainformation
*
* @var callable|null
*/
private $metaInformationCallback = null;

/**
* Constructor
*
Expand Down Expand Up @@ -354,6 +390,75 @@ public function setDeterministicModeEnabled(bool $deterministicModeEnabled)
return $this;
}

/**
* Set the template for the author meta information
*
* @param string $authorTemplate
* @return static
*/
public function setAuthorTemplate(string $authorTemplate)
{
$this->authorTemplate = $authorTemplate;

return $this;
}

/**
* Set the template for the keyword meta information
*
* @param string $keywordTemplate
* @return static
*/
public function setKeywordTemplate(string $keywordTemplate)
{
$this->keywordTemplate = $keywordTemplate;

return $this;
}

/**
* Set the template for the title meta information
*
* @param string $titleTemplate
* @return static
*/
public function setTitleTemplate(string $titleTemplate)
{
$this->titleTemplate = $titleTemplate;

return $this;
}

/**
* Set the template for the subject meta information
*
* @param string $subjectTemplate
* @return static
*/
public function setSubjectTemplate(string $subjectTemplate)
{
$this->subjectTemplate = $subjectTemplate;

return $this;
}

/**
* Set the user defined callback for generating custom meta information
*
* @param callable|null $callback
* @return static
*/
public function setMetaInformationCallback(?callable $callback = null)
{
if (is_callable($callback)) {
$this->metaInformationCallback = $callback;
} else {
$this->metaInformationCallback = null;
}

return $this;
}

/**
* Get the content of XML to attach
*
Expand Down Expand Up @@ -481,6 +586,11 @@ private function updatePdfMetadata(): void
$xmpNodes->{'CreateDate'} = $pdfMetadataInfos['createdDate'];
$xmpNodes->{'ModifyDate'} = $pdfMetadataInfos['modifiedDate'];
$this->pdfWriter->addMetadataDescriptionNode($descXmp->asXML());

$this->pdfWriter->SetAuthor($pdfMetadataInfos['author'], true);
$this->pdfWriter->SetKeywords($pdfMetadataInfos['keywords'], true);
$this->pdfWriter->SetTitle($pdfMetadataInfos['title'], true);
$this->pdfWriter->SetSubject($pdfMetadataInfos['subject'], true);
}

/**
Expand All @@ -493,14 +603,17 @@ private function preparePdfMetadata(): array
$invoiceInformations = $this->extractInvoiceInformations();

$dateString = date('Y-m-d', strtotime($invoiceInformations['date']));

$author = $invoiceInformations['seller'];
$keywords = sprintf('%s, FacturX/ZUGFeRD', $invoiceInformations['docTypeName']);
$title = sprintf('%s : %s %s', $invoiceInformations['seller'], $invoiceInformations['docTypeName'], $invoiceInformations['invoiceId']);
$subject = sprintf('FacturX/ZUGFeRD %s %s dated %s issued by %s', $invoiceInformations['docTypeName'], $invoiceInformations['invoiceId'], $dateString, $invoiceInformations['seller']);

$pdfMetadata = array(
'author' => $invoiceInformations['seller'],
'keywords' => sprintf('%s, FacturX/ZUGFeRD', $invoiceInformations['docTypeName']),
'title' => $title,
'subject' => $subject,
'author' => $this->buildMetadataField('author', $author, $invoiceInformations),
'keywords' => $this->buildMetadataField('keywords', $keywords, $invoiceInformations),
'title' => $this->buildMetadataField('title', $title, $invoiceInformations),
'subject' => $this->buildMetadataField('subject', $subject, $invoiceInformations),
'createdDate' => $invoiceInformations['date'],
'modifiedDate' => (new DateTime())->format('Y-m-d\TH:i:sP'),
);
Expand Down Expand Up @@ -567,4 +680,47 @@ private function isFile($pdfData): bool
return false;
}
}

/**
* Returns the parsed meta-field content
*
* @param string $which
* @param string $default
* @param array $invoiceInformation
* @return string
*/
private function buildMetadataField(string $which, string $default, array $invoiceInformation): string
{
$xmlContent = $this->getXmlContent();

if (is_callable($this->metaInformationCallback)) {
$callbackResult = call_user_func($this->metaInformationCallback, $which, $xmlContent, $invoiceInformation, $default);
if (!StringUtils::stringIsNullOrEmpty($callbackResult)) {
return $callbackResult;
}
}

$templates = [
'author' => $this->authorTemplate,
'keywords' => $this->keywordTemplate,
'title' => $this->titleTemplate,
'subject' => $this->subjectTemplate,
];

if (!isset($templates[$which])) {
return $default;
}

if (StringUtils::stringIsNullOrEmpty($templates[$which])) {
return $default;
}

return sprintf(
$templates[$which],
$invoiceInformation['invoiceId'],
$invoiceInformation['docTypeName'],
$invoiceInformation['seller'],
$invoiceInformation['date']
);
}
}
Loading
Loading