perlpdflibzugferd

How to create virtual XML for ZUGFeRD Invoices


I try to create a PDF/A-3b file which contains an embedded XML-File to be ZUGFeRD conform. I use Perl and PDFLib for this purpose. The PDFLib Documentation out there is just for Java and PHP. Creating the PDF works fine, but the XML part is my problem.

So how can i create a pvf from xml and join this to my pdf?

This is what PDFLib recommends in Java:

// Place XML stream in a virtual PVF file
String pvf_name = "/pvf/ZUGFeRD-invoice.xml";
byte[] xml_bytes = xml_string.getBytes("UTF-8");
p.create_pvf(pvf_name, xml_bytes, "");

// Create file attachment (asset) from PVF file
int xml_asset = p.load_asset("Attachment", pvf_name,
     "mimetype=text/xml description={ZUGFeRD invoice in XML format} "
   + "relationship=Alternative documentattachment=true");

// Associate file attachment with the document
p.end_document("associatedfiles={" + xml_asset + "}");

So I thought, take the example and fit it to perl:

my $xmldata = read_file($xmlfile, binmode => ':utf8'); #I use example xml at the moment

my $pvf_xml = "/pvf/ZUGFeRD-invoice.xml";
PDF_create_pvf($pdf, $pvf_xml, $xmldata, ""); #because no OOP i need to call it this way (works with all other PDF Functions)

my $xml_invoice = PDF_load_asset("Attachment", $pvf_xml, "mimetype=text/xml "
                                                         ."description={Rechnungsdaten im Zugferd-Xml-Format} "
                                                         ."relationship=Alternative documentattachment=true");

PDF_end_document($pdf, "associatedfiles={".$xml_invoice."}");

In PHP examples it's also not needed to convert to ByteArray after reading xml. Further tried it with unpack but don't seem to be the problem.

If I call my script I'm just getting:

Usage: load_asset(type, filename, optlist); at signatur_test.pl line 41.

I think the problem is that pvf_xml isn't created the line before. Anyone did this before and no how to solve this?


Solution

  • Arg, i was just missing the PDF-Handle in the load_asset method:

    my $xml_invoice = PDF_load_asset($pdf, "Attachment", $pvf_xml, "mimetype=text/xml "
                                                             ."description={Rechnungsdaten im Zugferd-Xml-Format} "
                                                             ."relationship=Alternative documentattachment=true");
    

    This way it works.