Skip to content

Commit

Permalink
#208 Customizable PDF Metadata -> Added Templates and Callback Function
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed Dec 11, 2024
1 parent d1f0782 commit fa686e9
Show file tree
Hide file tree
Showing 2 changed files with 493 additions and 4 deletions.
159 changes: 155 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 @@ -493,14 +598,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 +675,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

0 comments on commit fa686e9

Please sign in to comment.