Embed XML? PDF/A-3 format?
Is it possible to include an XML file when generating a PDF by using the PDF/A-3 format?
This would enable the generation of e-invoices source
you could use the pdfmake document's getStream (https://pdfmake.github.io/docs/0.1/getting-started/client-side/methods/#get-pdfkit-document-object-as-stream) method to access the underlaying pdfkit document. There were some approaches to enable pdf/a-3 compatibility there, see https://github.com/foliojs/pdfkit/issues/369
pdfkit's release https://github.com/foliojs/pdfkit/releases/tag/v0.14.0 introduced official support for PDF/A-3a but it's not yet available in pdfmake. However, I guess the authors would be delighted if you open up a new PR for this. Good Luck 👍
PDF/A support implemented, more here: https://github.com/bpampuch/pdfmake/issues/950#issuecomment-2451925833
i had some success in generating a zugferd pdf with pdfmake.
var pdfDoc = printer.createPdfKitDocument(generateInvoiceTemplate(order,t));
const zugferdXML = generateZugferdXML(order, t); // basically take one of the examples and generate a long string with your order contents
const base64XML = Buffer.from(zugferdXML).toString('base64');
pdfDoc.file(`data:application/xml;base64,${base64XML}`,
{
creationDate: new Date(2020, 3, 1),
description: 'ZUGFeRD Invoice Data',
hidden: false,
modifiedDate: new Date(2020, 3, 1),
name: 'factur-x.xml',
relationship: 'Alternative', //currently not supported see https://github.com/foliojs/pdfkit/issues/1561
type: 'application/xml',
})
i don't think it is yet valid at least because the relationship attribute is missing. but the pdf looks good and the xml can be viewed in an e-invoice reader.
ah and of course you need to set the pdf/a options in your template and pdf version 1.7
@eikaramba do you have a full working example? :)
the code above is basically complete. you just have to put into your pdf template instead of generateInvoiceTemplate and one of the example xmls from here https://www.ferd-net.de/standards/zugferd-versionsarchiv/zugferd-2.3.html as a string for zugferdXML (just put some fake data into the form the download is on the next page then)
then adapt the contents of the xml to your needs. it is quite complicated but the basic example only needs the most basic stuff (total amount, taxes, line price etc.)
Fixed in https://github.com/foliojs/pdfkit/releases/tag/v0.15.2
As it might be helpful, this creates valid ZugFerd:
const pdf = await pdfmake.createPdf(template).getStream()
const base64XML = Buffer.from(xml).toString('base64')
pdf.file(`data:application/xml;base64,${base64XML}`, {
creationDate: new Date(),
description: 'ZUGFeRD Invoice Data',
hidden: false,
modifiedDate: new Date(),
name: 'xrechnung.xml',
relationship: 'Data',
type: 'application/xml',
})
const xrechnungMetadata = `<rdf:Description xmlns:fx="urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0#" rdf:about="">
<fx:ConformanceLevel>XRECHNUNG</fx:ConformanceLevel>
<fx:DocumentType>INVOICE</fx:DocumentType>
<fx:DocumentFileName>xrechnung.xml</fx:DocumentFileName>
<fx:Version>1.0</fx:Version>
</rdf:Description>
<rdf:Description xmlns:pdfaExtension="http://www.aiim.org/pdfa/ns/extension/" xmlns:pdfaProperty="http://www.aiim.org/pdfa/ns/property#" xmlns:pdfaSchema="http://www.aiim.org/pdfa/ns/schema#" rdf:about="">
<pdfaExtension:schemas>
<rdf:Bag>
<rdf:li rdf:parseType="Resource">
<pdfaSchema:schema>ZUGFeRD PDFA Extension Schema</pdfaSchema:schema>
<pdfaSchema:namespaceURI>urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0#</pdfaSchema:namespaceURI>
<pdfaSchema:prefix>fx</pdfaSchema:prefix>
<pdfaSchema:property>
<rdf:Seq>
<rdf:li rdf:parseType="Resource">
<pdfaProperty:name>DocumentFileName</pdfaProperty:name>
<pdfaProperty:valueType>Text</pdfaProperty:valueType>
<pdfaProperty:category>external</pdfaProperty:category>
<pdfaProperty:description>name of the embedded XML invoice file</pdfaProperty:description>
</rdf:li>
<rdf:li rdf:parseType="Resource">
<pdfaProperty:name>DocumentType</pdfaProperty:name>
<pdfaProperty:valueType>Text</pdfaProperty:valueType>
<pdfaProperty:category>external</pdfaProperty:category>
<pdfaProperty:description>INVOICE</pdfaProperty:description>
</rdf:li>
<rdf:li rdf:parseType="Resource">
<pdfaProperty:name>Version</pdfaProperty:name>
<pdfaProperty:valueType>Text</pdfaProperty:valueType>
<pdfaProperty:category>external</pdfaProperty:category>
<pdfaProperty:description>The actual version of the ZUGFeRD XML schema</pdfaProperty:description>
</rdf:li>
<rdf:li rdf:parseType="Resource">
<pdfaProperty:name>ConformanceLevel</pdfaProperty:name>
<pdfaProperty:valueType>Text</pdfaProperty:valueType>
<pdfaProperty:category>external</pdfaProperty:category>
<pdfaProperty:description>The selected ZUGFeRD profile completeness</pdfaProperty:description>
</rdf:li>
</rdf:Seq>
</pdfaSchema:property>
</rdf:li>
</rdf:Bag>
</pdfaExtension:schemas>
</rdf:Description>
`
pdf.appendXML(xrechnungMetadata)
pdf.end()