diff --git a/src/ZugferdDocumentPdfBuilderAbstract.php b/src/ZugferdDocumentPdfBuilderAbstract.php index ce555884..a1db160b 100644 --- a/src/ZugferdDocumentPdfBuilderAbstract.php +++ b/src/ZugferdDocumentPdfBuilderAbstract.php @@ -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; @@ -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 * @@ -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 * @@ -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); } /** @@ -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'), ); @@ -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'] + ); + } } diff --git a/tests/testcases/PdfBuilderEn16931Test.php b/tests/testcases/PdfBuilderEn16931Test.php index 5904b806..384616f5 100644 --- a/tests/testcases/PdfBuilderEn16931Test.php +++ b/tests/testcases/PdfBuilderEn16931Test.php @@ -168,6 +168,10 @@ public function testPdfMetaData(): void $this->assertIsArray($pdfDetails); $this->assertArrayHasKey("Producer", $pdfDetails); //"FPDF 1.84" $this->assertArrayHasKey("CreationDate", $pdfDetails); //"2020-12-09T05:19:39+00:00" + $this->assertArrayHasKey("Title", $pdfDetails); + $this->assertArrayHasKey("Subject", $pdfDetails); + $this->assertArrayHasKey("Author", $pdfDetails); + $this->assertArrayHasKey("Keywords", $pdfDetails); $this->assertArrayHasKey("Pages", $pdfDetails); //"1" $this->assertArrayHasKey("fx:documenttype", $pdfDetails); $this->assertArrayHasKey("fx:documentfilename", $pdfDetails); @@ -183,6 +187,9 @@ public function testPdfMetaData(): void $this->assertArrayHasKey("xmp:modifydate", $pdfDetails); $this->assertStringContainsString('FPDF', $pdfDetails["Producer"]); $this->assertStringContainsString(date("Y-m-d"), $pdfDetails["CreationDate"]); + $this->assertEquals("Lieferant GmbH : Invoice 471102", $pdfDetails["Title"]); + $this->assertEquals("FacturX/ZUGFeRD Invoice 471102 dated 2018-03-05 issued by Lieferant GmbH", $pdfDetails["Subject"]); + $this->assertEquals("Lieferant GmbH", $pdfDetails["Author"]); $this->assertEquals("1", $pdfDetails["Pages"]); $this->assertEquals("INVOICE", $pdfDetails["fx:documenttype"]); $this->assertEquals("factur-x.xml", $pdfDetails["fx:documentfilename"]); @@ -542,6 +549,10 @@ public function testDeterministicMode(): void $this->assertIsArray($pdfDetails); $this->assertArrayHasKey("Producer", $pdfDetails); $this->assertArrayHasKey("CreationDate", $pdfDetails); + $this->assertArrayHasKey("Title", $pdfDetails); + $this->assertArrayHasKey("Subject", $pdfDetails); + $this->assertArrayHasKey("Author", $pdfDetails); + $this->assertArrayHasKey("Keywords", $pdfDetails); $this->assertArrayHasKey("Pages", $pdfDetails); $this->assertArrayHasKey("fx:documenttype", $pdfDetails); $this->assertArrayHasKey("fx:documentfilename", $pdfDetails); @@ -557,6 +568,9 @@ public function testDeterministicMode(): void $this->assertArrayHasKey("xmp:modifydate", $pdfDetails); $this->assertStringContainsString('FPDF', $pdfDetails["Producer"]); $this->assertStringContainsString('2000-01-01', $pdfDetails["CreationDate"]); + $this->assertEquals("Lieferant GmbH : Invoice 471102", $pdfDetails["Title"]); + $this->assertEquals("FacturX/ZUGFeRD Invoice 471102 dated 2018-03-05 issued by Lieferant GmbH", $pdfDetails["Subject"]); + $this->assertEquals("Lieferant GmbH", $pdfDetails["Author"]); $this->assertEquals("1", $pdfDetails["Pages"]); $this->assertEquals("INVOICE", $pdfDetails["fx:documenttype"]); $this->assertEquals("factur-x.xml", $pdfDetails["fx:documentfilename"]); @@ -571,4 +585,391 @@ public function testDeterministicMode(): void $this->assertStringContainsString('2000-01-01', $pdfDetails["xmp:createdate"]); $this->assertStringContainsString('2000-01-01', $pdfDetails["xmp:modifydate"]); } + + public function testCustomMetaInformation(): void + { + $pdfBuilder = new ZugferdDocumentPdfBuilder(self::$document, self::$sourcePdfFilename); + $pdfBuilder->setTitleTemplate('%3$s : %2$s %1$s'); + $pdfBuilder->generateDocument(); + $pdfBuilder->saveDocument(self::$destPdfFilename); + + $pdfParser = new PdfParser(); + $pdfParsed = $pdfParser->parseFile(self::$destPdfFilename); + $pdfDetails = $pdfParsed->getDetails(); + + $this->assertIsArray($pdfDetails); + $this->assertArrayHasKey("Producer", $pdfDetails); + $this->assertArrayHasKey("CreationDate", $pdfDetails); + $this->assertArrayHasKey("Title", $pdfDetails); + $this->assertArrayHasKey("Subject", $pdfDetails); + $this->assertArrayHasKey("Author", $pdfDetails); + $this->assertArrayHasKey("Keywords", $pdfDetails); + $this->assertArrayHasKey("Pages", $pdfDetails); + $this->assertArrayHasKey("fx:documenttype", $pdfDetails); + $this->assertArrayHasKey("fx:documentfilename", $pdfDetails); + $this->assertArrayHasKey("fx:version", $pdfDetails); + $this->assertArrayHasKey("fx:conformancelevel", $pdfDetails); + $this->assertArrayHasKey("pdfaid:part", $pdfDetails); + $this->assertArrayHasKey("pdfaid:conformance", $pdfDetails); + $this->assertArrayHasKey("dc:title", $pdfDetails); + $this->assertArrayHasKey("dc:creator", $pdfDetails); + $this->assertArrayHasKey("dc:description", $pdfDetails); + $this->assertArrayHasKey("xmp:creatortool", $pdfDetails); + $this->assertArrayHasKey("xmp:createdate", $pdfDetails); + $this->assertArrayHasKey("xmp:modifydate", $pdfDetails); + $this->assertStringContainsString('FPDF', $pdfDetails["Producer"]); + $this->assertStringContainsString(date("Y-m-d"), $pdfDetails["CreationDate"]); + $this->assertEquals("Lieferant GmbH : Invoice 471102", $pdfDetails["Title"]); + $this->assertEquals("FacturX/ZUGFeRD Invoice 471102 dated 2018-03-05 issued by Lieferant GmbH", $pdfDetails["Subject"]); + $this->assertEquals("Lieferant GmbH", $pdfDetails["Author"]); + $this->assertEquals("1", $pdfDetails["Pages"]); + $this->assertEquals("INVOICE", $pdfDetails["fx:documenttype"]); + $this->assertEquals("factur-x.xml", $pdfDetails["fx:documentfilename"]); + $this->assertEquals("1.0", $pdfDetails["fx:version"]); + $this->assertEquals("EN 16931", $pdfDetails["fx:conformancelevel"]); + $this->assertEquals("3", $pdfDetails["pdfaid:part"]); + $this->assertEquals("B", $pdfDetails["pdfaid:conformance"]); + $this->assertEquals("Lieferant GmbH : Invoice 471102", $pdfDetails["dc:title"]); + $this->assertEquals("Lieferant GmbH", $pdfDetails["dc:creator"]); + $this->assertEquals("FacturX/ZUGFeRD Invoice 471102 dated 2018-03-05 issued by Lieferant GmbH", $pdfDetails["dc:description"]); + $this->assertStringContainsString('Factur-X PHP library', $pdfDetails["xmp:creatortool"]); + $this->assertStringContainsString("2018-03-05", $pdfDetails["xmp:createdate"]); + $this->assertStringContainsString(date("Y-m-d"), $pdfDetails["xmp:modifydate"]); + + $pdfBuilder = new ZugferdDocumentPdfBuilder(self::$document, self::$sourcePdfFilename); + $pdfBuilder->setTitleTemplate('%3$s : %2$s %1$s'); + $pdfBuilder->setKeywordTemplate('%1$s, %2$s, %3$s, %4$s'); + $pdfBuilder->generateDocument(); + $pdfBuilder->saveDocument(self::$destPdfFilename); + + $pdfParser = new PdfParser(); + $pdfParsed = $pdfParser->parseFile(self::$destPdfFilename); + $pdfDetails = $pdfParsed->getDetails(); + + $this->assertIsArray($pdfDetails); + $this->assertArrayHasKey("Producer", $pdfDetails); + $this->assertArrayHasKey("CreationDate", $pdfDetails); + $this->assertArrayHasKey("Title", $pdfDetails); + $this->assertArrayHasKey("Subject", $pdfDetails); + $this->assertArrayHasKey("Author", $pdfDetails); + $this->assertArrayHasKey("Keywords", $pdfDetails); + $this->assertArrayHasKey("Pages", $pdfDetails); + $this->assertArrayHasKey("fx:documenttype", $pdfDetails); + $this->assertArrayHasKey("fx:documentfilename", $pdfDetails); + $this->assertArrayHasKey("fx:version", $pdfDetails); + $this->assertArrayHasKey("fx:conformancelevel", $pdfDetails); + $this->assertArrayHasKey("pdfaid:part", $pdfDetails); + $this->assertArrayHasKey("pdfaid:conformance", $pdfDetails); + $this->assertArrayHasKey("dc:title", $pdfDetails); + $this->assertArrayHasKey("dc:creator", $pdfDetails); + $this->assertArrayHasKey("dc:description", $pdfDetails); + $this->assertArrayHasKey("xmp:creatortool", $pdfDetails); + $this->assertArrayHasKey("xmp:createdate", $pdfDetails); + $this->assertArrayHasKey("xmp:modifydate", $pdfDetails); + $this->assertStringContainsString('FPDF', $pdfDetails["Producer"]); + $this->assertStringContainsString(date("Y-m-d"), $pdfDetails["CreationDate"]); + $this->assertEquals("Lieferant GmbH : Invoice 471102", $pdfDetails["Title"]); + $this->assertEquals("FacturX/ZUGFeRD Invoice 471102 dated 2018-03-05 issued by Lieferant GmbH", $pdfDetails["Subject"]); + $this->assertEquals("Lieferant GmbH", $pdfDetails["Author"]); + $this->assertEquals("1", $pdfDetails["Pages"]); + $this->assertEquals("INVOICE", $pdfDetails["fx:documenttype"]); + $this->assertEquals("factur-x.xml", $pdfDetails["fx:documentfilename"]); + $this->assertEquals("1.0", $pdfDetails["fx:version"]); + $this->assertEquals("EN 16931", $pdfDetails["fx:conformancelevel"]); + $this->assertEquals("3", $pdfDetails["pdfaid:part"]); + $this->assertEquals("B", $pdfDetails["pdfaid:conformance"]); + $this->assertEquals("Lieferant GmbH : Invoice 471102", $pdfDetails["dc:title"]); + $this->assertEquals("Lieferant GmbH", $pdfDetails["dc:creator"]); + $this->assertEquals("FacturX/ZUGFeRD Invoice 471102 dated 2018-03-05 issued by Lieferant GmbH", $pdfDetails["dc:description"]); + $this->assertStringContainsString('Factur-X PHP library', $pdfDetails["xmp:creatortool"]); + $this->assertStringContainsString("2018-03-05", $pdfDetails["xmp:createdate"]); + $this->assertStringContainsString(date("Y-m-d"), $pdfDetails["xmp:modifydate"]); + + $pdfBuilder = new ZugferdDocumentPdfBuilder(self::$document, self::$sourcePdfFilename); + $pdfBuilder->setTitleTemplate('%3$s : %2$s %1$s'); + $pdfBuilder->setKeywordTemplate('%1$s, %2$s, %3$s, %4$s'); + $pdfBuilder->setAuthorTemplate('Issued by seller with name %3$s'); + $pdfBuilder->generateDocument(); + $pdfBuilder->saveDocument(self::$destPdfFilename); + + $pdfParser = new PdfParser(); + $pdfParsed = $pdfParser->parseFile(self::$destPdfFilename); + $pdfDetails = $pdfParsed->getDetails(); + + $this->assertIsArray($pdfDetails); + $this->assertArrayHasKey("Producer", $pdfDetails); + $this->assertArrayHasKey("CreationDate", $pdfDetails); + $this->assertArrayHasKey("Title", $pdfDetails); + $this->assertArrayHasKey("Subject", $pdfDetails); + $this->assertArrayHasKey("Author", $pdfDetails); + $this->assertArrayHasKey("Keywords", $pdfDetails); + $this->assertArrayHasKey("Pages", $pdfDetails); + $this->assertArrayHasKey("fx:documenttype", $pdfDetails); + $this->assertArrayHasKey("fx:documentfilename", $pdfDetails); + $this->assertArrayHasKey("fx:version", $pdfDetails); + $this->assertArrayHasKey("fx:conformancelevel", $pdfDetails); + $this->assertArrayHasKey("pdfaid:part", $pdfDetails); + $this->assertArrayHasKey("pdfaid:conformance", $pdfDetails); + $this->assertArrayHasKey("dc:title", $pdfDetails); + $this->assertArrayHasKey("dc:creator", $pdfDetails); + $this->assertArrayHasKey("dc:description", $pdfDetails); + $this->assertArrayHasKey("xmp:creatortool", $pdfDetails); + $this->assertArrayHasKey("xmp:createdate", $pdfDetails); + $this->assertArrayHasKey("xmp:modifydate", $pdfDetails); + $this->assertStringContainsString('FPDF', $pdfDetails["Producer"]); + $this->assertStringContainsString(date("Y-m-d"), $pdfDetails["CreationDate"]); + $this->assertEquals("Lieferant GmbH : Invoice 471102", $pdfDetails["Title"]); + $this->assertEquals("FacturX/ZUGFeRD Invoice 471102 dated 2018-03-05 issued by Lieferant GmbH", $pdfDetails["Subject"]); + $this->assertEquals("Issued by seller with name Lieferant GmbH", $pdfDetails["Author"]); + $this->assertEquals("1", $pdfDetails["Pages"]); + $this->assertEquals("INVOICE", $pdfDetails["fx:documenttype"]); + $this->assertEquals("factur-x.xml", $pdfDetails["fx:documentfilename"]); + $this->assertEquals("1.0", $pdfDetails["fx:version"]); + $this->assertEquals("EN 16931", $pdfDetails["fx:conformancelevel"]); + $this->assertEquals("3", $pdfDetails["pdfaid:part"]); + $this->assertEquals("B", $pdfDetails["pdfaid:conformance"]); + $this->assertEquals("Lieferant GmbH : Invoice 471102", $pdfDetails["dc:title"]); + $this->assertEquals("Issued by seller with name Lieferant GmbH", $pdfDetails["dc:creator"]); + $this->assertEquals("FacturX/ZUGFeRD Invoice 471102 dated 2018-03-05 issued by Lieferant GmbH", $pdfDetails["dc:description"]); + $this->assertStringContainsString('Factur-X PHP library', $pdfDetails["xmp:creatortool"]); + $this->assertStringContainsString("2018-03-05", $pdfDetails["xmp:createdate"]); + $this->assertStringContainsString(date("Y-m-d"), $pdfDetails["xmp:modifydate"]); + + $pdfBuilder = new ZugferdDocumentPdfBuilder(self::$document, self::$sourcePdfFilename); + $pdfBuilder->setTitleTemplate('%3$s : %2$s %1$s'); + $pdfBuilder->setKeywordTemplate('%1$s, %2$s, %3$s, %4$s'); + $pdfBuilder->setAuthorTemplate('Issued by seller with name %3$s'); + $pdfBuilder->setSubjectTemplate('%2$s-Document, Issued by %3$s'); + $pdfBuilder->generateDocument(); + $pdfBuilder->saveDocument(self::$destPdfFilename); + + $pdfParser = new PdfParser(); + $pdfParsed = $pdfParser->parseFile(self::$destPdfFilename); + $pdfDetails = $pdfParsed->getDetails(); + + $this->assertIsArray($pdfDetails); + $this->assertArrayHasKey("Producer", $pdfDetails); + $this->assertArrayHasKey("CreationDate", $pdfDetails); + $this->assertArrayHasKey("Title", $pdfDetails); + $this->assertArrayHasKey("Subject", $pdfDetails); + $this->assertArrayHasKey("Author", $pdfDetails); + $this->assertArrayHasKey("Keywords", $pdfDetails); + $this->assertArrayHasKey("Pages", $pdfDetails); + $this->assertArrayHasKey("fx:documenttype", $pdfDetails); + $this->assertArrayHasKey("fx:documentfilename", $pdfDetails); + $this->assertArrayHasKey("fx:version", $pdfDetails); + $this->assertArrayHasKey("fx:conformancelevel", $pdfDetails); + $this->assertArrayHasKey("pdfaid:part", $pdfDetails); + $this->assertArrayHasKey("pdfaid:conformance", $pdfDetails); + $this->assertArrayHasKey("dc:title", $pdfDetails); + $this->assertArrayHasKey("dc:creator", $pdfDetails); + $this->assertArrayHasKey("dc:description", $pdfDetails); + $this->assertArrayHasKey("xmp:creatortool", $pdfDetails); + $this->assertArrayHasKey("xmp:createdate", $pdfDetails); + $this->assertArrayHasKey("xmp:modifydate", $pdfDetails); + $this->assertStringContainsString('FPDF', $pdfDetails["Producer"]); + $this->assertStringContainsString(date("Y-m-d"), $pdfDetails["CreationDate"]); + $this->assertEquals("Lieferant GmbH : Invoice 471102", $pdfDetails["Title"]); + $this->assertEquals("Invoice-Document, Issued by Lieferant GmbH", $pdfDetails["Subject"]); + $this->assertEquals("Issued by seller with name Lieferant GmbH", $pdfDetails["Author"]); + $this->assertEquals("1", $pdfDetails["Pages"]); + $this->assertEquals("INVOICE", $pdfDetails["fx:documenttype"]); + $this->assertEquals("factur-x.xml", $pdfDetails["fx:documentfilename"]); + $this->assertEquals("1.0", $pdfDetails["fx:version"]); + $this->assertEquals("EN 16931", $pdfDetails["fx:conformancelevel"]); + $this->assertEquals("3", $pdfDetails["pdfaid:part"]); + $this->assertEquals("B", $pdfDetails["pdfaid:conformance"]); + $this->assertEquals("Lieferant GmbH : Invoice 471102", $pdfDetails["dc:title"]); + $this->assertEquals("Issued by seller with name Lieferant GmbH", $pdfDetails["dc:creator"]); + $this->assertEquals("Invoice-Document, Issued by Lieferant GmbH", $pdfDetails["dc:description"]); + $this->assertStringContainsString('Factur-X PHP library', $pdfDetails["xmp:creatortool"]); + $this->assertStringContainsString("2018-03-05", $pdfDetails["xmp:createdate"]); + $this->assertStringContainsString(date("Y-m-d"), $pdfDetails["xmp:modifydate"]); + + $whichArray = []; + + $pdfBuilder = new ZugferdDocumentPdfBuilder(self::$document, self::$sourcePdfFilename); + $pdfBuilder->setMetaInformationCallback(function ($which) use (&$whichArray) { + $whichArray[] = $which; + + $this->assertIsString($which); + $this->assertTrue(in_array($which, ['author', 'keywords', 'title', 'subject'])); + + if ($which === 'title') { + return "DummyTitle"; + } + if ($which === 'author') { + return "DummyAuthor"; + } + if ($which === 'subject') { + return "DummySubject"; + } + }); + $pdfBuilder->generateDocument(); + $pdfBuilder->saveDocument(self::$destPdfFilename); + + $this->assertNotEmpty($whichArray); + $this->assertCount(4, $whichArray); + $this->assertTrue(in_array('author', $whichArray)); + $this->assertTrue(in_array('keywords', $whichArray)); + $this->assertTrue(in_array('title', $whichArray)); + $this->assertTrue(in_array('subject', $whichArray)); + + $pdfParser = new PdfParser(); + $pdfParsed = $pdfParser->parseFile(self::$destPdfFilename); + $pdfDetails = $pdfParsed->getDetails(); + + $this->assertIsArray($pdfDetails); + $this->assertArrayHasKey("Producer", $pdfDetails); + $this->assertArrayHasKey("CreationDate", $pdfDetails); + $this->assertArrayHasKey("Title", $pdfDetails); + $this->assertArrayHasKey("Subject", $pdfDetails); + $this->assertArrayHasKey("Author", $pdfDetails); + $this->assertArrayHasKey("Keywords", $pdfDetails); + $this->assertArrayHasKey("Pages", $pdfDetails); + $this->assertArrayHasKey("fx:documenttype", $pdfDetails); + $this->assertArrayHasKey("fx:documentfilename", $pdfDetails); + $this->assertArrayHasKey("fx:version", $pdfDetails); + $this->assertArrayHasKey("fx:conformancelevel", $pdfDetails); + $this->assertArrayHasKey("pdfaid:part", $pdfDetails); + $this->assertArrayHasKey("pdfaid:conformance", $pdfDetails); + $this->assertArrayHasKey("dc:title", $pdfDetails); + $this->assertArrayHasKey("dc:creator", $pdfDetails); + $this->assertArrayHasKey("dc:description", $pdfDetails); + $this->assertArrayHasKey("xmp:creatortool", $pdfDetails); + $this->assertArrayHasKey("xmp:createdate", $pdfDetails); + $this->assertArrayHasKey("xmp:modifydate", $pdfDetails); + $this->assertStringContainsString('FPDF', $pdfDetails["Producer"]); + $this->assertStringContainsString(date("Y-m-d"), $pdfDetails["CreationDate"]); + $this->assertEquals("DummyTitle", $pdfDetails["Title"]); + $this->assertEquals("DummySubject", $pdfDetails["Subject"]); + $this->assertEquals("DummyAuthor", $pdfDetails["Author"]); + $this->assertEquals("1", $pdfDetails["Pages"]); + $this->assertEquals("INVOICE", $pdfDetails["fx:documenttype"]); + $this->assertEquals("factur-x.xml", $pdfDetails["fx:documentfilename"]); + $this->assertEquals("1.0", $pdfDetails["fx:version"]); + $this->assertEquals("EN 16931", $pdfDetails["fx:conformancelevel"]); + $this->assertEquals("3", $pdfDetails["pdfaid:part"]); + $this->assertEquals("B", $pdfDetails["pdfaid:conformance"]); + $this->assertEquals("DummyTitle", $pdfDetails["dc:title"]); + $this->assertEquals("DummyAuthor", $pdfDetails["dc:creator"]); + $this->assertEquals("DummySubject", $pdfDetails["dc:description"]); + $this->assertStringContainsString('Factur-X PHP library', $pdfDetails["xmp:creatortool"]); + $this->assertStringContainsString("2018-03-05", $pdfDetails["xmp:createdate"]); + $this->assertStringContainsString(date("Y-m-d"), $pdfDetails["xmp:modifydate"]); + + $pdfBuilder = new ZugferdDocumentPdfBuilder(self::$document, self::$sourcePdfFilename); + $pdfBuilder->setMetaInformationCallback(function ($which) { + if ($which === 'title') { + return ""; + } + if ($which === 'author') { + return ""; + } + if ($which === 'subject') { + return ""; + } + }); + $pdfBuilder->setTitleTemplate('%3$s : %2$s %1$s'); + $pdfBuilder->setKeywordTemplate('%1$s, %2$s, %3$s, %4$s'); + $pdfBuilder->setAuthorTemplate('Issued by seller with name %3$s'); + $pdfBuilder->setSubjectTemplate('%2$s-Document, Issued by %3$s'); + $pdfBuilder->generateDocument(); + $pdfBuilder->saveDocument(self::$destPdfFilename); + + $pdfParser = new PdfParser(); + $pdfParsed = $pdfParser->parseFile(self::$destPdfFilename); + $pdfDetails = $pdfParsed->getDetails(); + + $this->assertIsArray($pdfDetails); + $this->assertArrayHasKey("Producer", $pdfDetails); + $this->assertArrayHasKey("CreationDate", $pdfDetails); + $this->assertArrayHasKey("Title", $pdfDetails); + $this->assertArrayHasKey("Subject", $pdfDetails); + $this->assertArrayHasKey("Author", $pdfDetails); + $this->assertArrayHasKey("Keywords", $pdfDetails); + $this->assertArrayHasKey("Pages", $pdfDetails); + $this->assertArrayHasKey("fx:documenttype", $pdfDetails); + $this->assertArrayHasKey("fx:documentfilename", $pdfDetails); + $this->assertArrayHasKey("fx:version", $pdfDetails); + $this->assertArrayHasKey("fx:conformancelevel", $pdfDetails); + $this->assertArrayHasKey("pdfaid:part", $pdfDetails); + $this->assertArrayHasKey("pdfaid:conformance", $pdfDetails); + $this->assertArrayHasKey("dc:title", $pdfDetails); + $this->assertArrayHasKey("dc:creator", $pdfDetails); + $this->assertArrayHasKey("dc:description", $pdfDetails); + $this->assertArrayHasKey("xmp:creatortool", $pdfDetails); + $this->assertArrayHasKey("xmp:createdate", $pdfDetails); + $this->assertArrayHasKey("xmp:modifydate", $pdfDetails); + $this->assertStringContainsString('FPDF', $pdfDetails["Producer"]); + $this->assertStringContainsString(date("Y-m-d"), $pdfDetails["CreationDate"]); + $this->assertEquals("Lieferant GmbH : Invoice 471102", $pdfDetails["Title"]); + $this->assertEquals("Invoice-Document, Issued by Lieferant GmbH", $pdfDetails["Subject"]); + $this->assertEquals("Issued by seller with name Lieferant GmbH", $pdfDetails["Author"]); + $this->assertEquals("1", $pdfDetails["Pages"]); + $this->assertEquals("INVOICE", $pdfDetails["fx:documenttype"]); + $this->assertEquals("factur-x.xml", $pdfDetails["fx:documentfilename"]); + $this->assertEquals("1.0", $pdfDetails["fx:version"]); + $this->assertEquals("EN 16931", $pdfDetails["fx:conformancelevel"]); + $this->assertEquals("3", $pdfDetails["pdfaid:part"]); + $this->assertEquals("B", $pdfDetails["pdfaid:conformance"]); + $this->assertEquals("Lieferant GmbH : Invoice 471102", $pdfDetails["dc:title"]); + $this->assertEquals("Issued by seller with name Lieferant GmbH", $pdfDetails["dc:creator"]); + $this->assertEquals("Invoice-Document, Issued by Lieferant GmbH", $pdfDetails["dc:description"]); + $this->assertStringContainsString('Factur-X PHP library', $pdfDetails["xmp:creatortool"]); + $this->assertStringContainsString("2018-03-05", $pdfDetails["xmp:createdate"]); + $this->assertStringContainsString(date("Y-m-d"), $pdfDetails["xmp:modifydate"]); + + $pdfBuilder = new ZugferdDocumentPdfBuilder(self::$document, self::$sourcePdfFilename); + $pdfBuilder->setMetaInformationCallback(null); + $pdfBuilder->setTitleTemplate('%3$s : %2$s %1$s'); + $pdfBuilder->setKeywordTemplate('%1$s, %2$s, %3$s, %4$s'); + $pdfBuilder->setAuthorTemplate('Issued by seller with name %3$s'); + $pdfBuilder->setSubjectTemplate('%2$s-Document, Issued by %3$s'); + $pdfBuilder->generateDocument(); + $pdfBuilder->saveDocument(self::$destPdfFilename); + + $pdfParser = new PdfParser(); + $pdfParsed = $pdfParser->parseFile(self::$destPdfFilename); + $pdfDetails = $pdfParsed->getDetails(); + + $this->assertIsArray($pdfDetails); + $this->assertArrayHasKey("Producer", $pdfDetails); + $this->assertArrayHasKey("CreationDate", $pdfDetails); + $this->assertArrayHasKey("Title", $pdfDetails); + $this->assertArrayHasKey("Subject", $pdfDetails); + $this->assertArrayHasKey("Author", $pdfDetails); + $this->assertArrayHasKey("Keywords", $pdfDetails); + $this->assertArrayHasKey("Pages", $pdfDetails); + $this->assertArrayHasKey("fx:documenttype", $pdfDetails); + $this->assertArrayHasKey("fx:documentfilename", $pdfDetails); + $this->assertArrayHasKey("fx:version", $pdfDetails); + $this->assertArrayHasKey("fx:conformancelevel", $pdfDetails); + $this->assertArrayHasKey("pdfaid:part", $pdfDetails); + $this->assertArrayHasKey("pdfaid:conformance", $pdfDetails); + $this->assertArrayHasKey("dc:title", $pdfDetails); + $this->assertArrayHasKey("dc:creator", $pdfDetails); + $this->assertArrayHasKey("dc:description", $pdfDetails); + $this->assertArrayHasKey("xmp:creatortool", $pdfDetails); + $this->assertArrayHasKey("xmp:createdate", $pdfDetails); + $this->assertArrayHasKey("xmp:modifydate", $pdfDetails); + $this->assertStringContainsString('FPDF', $pdfDetails["Producer"]); + $this->assertStringContainsString(date("Y-m-d"), $pdfDetails["CreationDate"]); + $this->assertEquals("Lieferant GmbH : Invoice 471102", $pdfDetails["Title"]); + $this->assertEquals("Invoice-Document, Issued by Lieferant GmbH", $pdfDetails["Subject"]); + $this->assertEquals("Issued by seller with name Lieferant GmbH", $pdfDetails["Author"]); + $this->assertEquals("1", $pdfDetails["Pages"]); + $this->assertEquals("INVOICE", $pdfDetails["fx:documenttype"]); + $this->assertEquals("factur-x.xml", $pdfDetails["fx:documentfilename"]); + $this->assertEquals("1.0", $pdfDetails["fx:version"]); + $this->assertEquals("EN 16931", $pdfDetails["fx:conformancelevel"]); + $this->assertEquals("3", $pdfDetails["pdfaid:part"]); + $this->assertEquals("B", $pdfDetails["pdfaid:conformance"]); + $this->assertEquals("Lieferant GmbH : Invoice 471102", $pdfDetails["dc:title"]); + $this->assertEquals("Issued by seller with name Lieferant GmbH", $pdfDetails["dc:creator"]); + $this->assertEquals("Invoice-Document, Issued by Lieferant GmbH", $pdfDetails["dc:description"]); + $this->assertStringContainsString('Factur-X PHP library', $pdfDetails["xmp:creatortool"]); + $this->assertStringContainsString("2018-03-05", $pdfDetails["xmp:createdate"]); + $this->assertStringContainsString(date("Y-m-d"), $pdfDetails["xmp:modifydate"]); + } }