Created
July 9, 2024 22:28
-
-
Save danielmarschall/8f4788ef4ff3b41ff00ba2057acc3234 to your computer and use it in GitHub Desktop.
Convert plain PDF to PDF/A-3b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use horstoeko\zugferd\ZugferdPdfWriter; | |
/** | |
* Converts a plain PDF to PDF/A-3b (without attachments). | |
* @param string $sourcePdf Source PDF file name | |
* @param string $destPdf Destination PDF file name (can be the same as the source) | |
* @param string $title Title metadata | |
* @param string $author Author metadata | |
* @param string $creatorTool Creator tool metadata | |
* @return void | |
*/ | |
function convert_pdf_to_pdfa(string $sourcePdf, string $destPdf, string $title, string $author, string $creatorTool=''): void | |
{ | |
$pdfWriter = new ZugferdPdfWriter(); | |
// Copy pages from the original PDF | |
$pageCount = $pdfWriter->setSourceFile($sourcePdf); | |
for ($pageNumber = 1; $pageNumber <= $pageCount; ++$pageNumber) { | |
$pageContent = $pdfWriter->importPage($pageNumber, '/MediaBox'); | |
$pdfWriter->AddPage(); | |
$pdfWriter->useTemplate($pageContent, 0, 0, null, null, true); | |
} | |
// Set PDF version 1.7 according to PDF/A-3 ISO 32000-1 | |
$pdfWriter->setPdfVersion('1.7', true); | |
// Update meta data (e.g. such as author, producer, title) | |
$pdfMetadata = array( | |
'author' => $author, | |
'keywords' => '', | |
'title' => $title, | |
'subject' => '', | |
'createdDate' => date('Y-m-d\TH:i:s') . '+00:00', | |
'modifiedDate' => date('Y-m-d\TH:i:s') . '+00:00', | |
); | |
$pdfWriter->setPdfMetadataInfos($pdfMetadata); | |
$xmp = simplexml_load_file(ZugferdSettings::getFullXmpMetaDataFilename()); | |
$descriptionNodes = $xmp->xpath('rdf:Description'); | |
// rdf:Description urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0# | |
// $descriptionNodes[0] not applicable | |
// Factur-X PDFA Extension Schema http://www.aiim.org/pdfa/ns/extension/ | |
// $descriptionNodes[1] not applicable | |
// rdf:Description http://www.aiim.org/pdfa/ns/id/ | |
// PDF/A-3b declaration | |
$descPdfAid = $descriptionNodes[2]; | |
$pdfWriter->addMetadataDescriptionNode($descPdfAid->asXML()); | |
// rdf:Description http://purl.org/dc/elements/1.1/ | |
$descDc = $descriptionNodes[3]; | |
$descNodes = $descDc->children('dc', true); | |
$descNodes->title->children('rdf', true)->Alt->li = $pdfMetadata['title']; | |
$descNodes->creator->children('rdf', true)->Seq->li = $pdfMetadata['author']; | |
$descNodes->description->children('rdf', true)->Alt->li = $pdfMetadata['subject']; | |
$pdfWriter->addMetadataDescriptionNode($descDc->asXML()); | |
// rdf:Description http://ns.adobe.com/pdf/1.3/ | |
$descAdobe = $descriptionNodes[4]; | |
$descAdobe->children('pdf', true)->{'Producer'} = 'FPDF'; | |
$pdfWriter->addMetadataDescriptionNode($descAdobe->asXML()); | |
// rdf:Description http://ns.adobe.com/xap/1.0/ | |
$descXmp = $descriptionNodes[5]; | |
$xmpNodes = $descXmp->children('xmp', true); | |
$xmpNodes->{'CreatorTool'} = $creatorTool; | |
$xmpNodes->{'CreateDate'} = $pdfMetadata['createdDate']; | |
$xmpNodes->{'ModifyDate'} = $pdfMetadata['modifiedDate']; | |
$pdfWriter->addMetadataDescriptionNode($descXmp->asXML()); | |
// Save file | |
$pdfWriter->Output($destPdf, 'F'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires this library: https://github.com/horstoeko/zugferd/