PDFlib Cookbook

cookbook

pdfa/zugferd_add_xml_to_pdfa

Download Java Code      Switch to PHP code       Show Output PDF      Show Input Files

/*
 * $Id: zugferd_add_xml_to_pdfa.java,v 1.4 2014/07/04 13:55:41 stm Exp $
 * 
 * ZUGFeRD Add XML Invoice to PDF:
 * Import the pages of a plain PDF/A invoice with PDI and add a
 * corresponding XML invoice to the output file. Note that the application
 * must take care that the imported PDF invoice and the XML invoice match.
 *
 * Required software: PDFlib+PDI/PPS 9
 * Required data: PDF/A input document, XML invoice file, XMP metadata
 */
package com.pdflib.cookbook.pdflib.pdfa;

import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class zugferd_add_xml_to_pdfa {
    public static void main(String argv[]) {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";
        
        String outfile = "zugferd_add_xml_to_pdf.pdf";
        String infile = "ZUGFeRD_invoice_noxml.pdf";
        String title = "ZUGFeRD Add XML Invoice to PDF";
        String xmpfile = "ZUGFeRD_extension_schema.xmp";
        String xmlinvoicefile = "ZUGFeRD-invoice.xml";

        pdflib p = null;
	int exitcode = 0;

        try {
            p = new pdflib();

            p.set_option("searchpath={" + searchpath + "}");

            /* This means we must check return values of load_font() etc. */
            p.set_option("errorpolicy=exception");

            if (p.begin_document(outfile, "pdfa=PDF/A-3b metadata={filename={"
                + xmpfile + "}}") == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Open the input PDF */
            int indoc = p.open_pdi_document(infile, "");
            if (indoc == -1) {
                throw new Exception("Error: " + p.get_errmsg());
            }
            
            /*
             * Clone PDF/A or PDF/X output intent
             */
            if (p.process_pdi(indoc, -1, "action=copyoutputintent") == -1) {
                throw new Exception("Error: " + p.get_errmsg());
            }

            p.set_info("Creator", "zugferd_add_xml_to_pdf.java");
            p.set_info("Title", title + " $Revision: 1.4 $");
            
            int endpage = (int) p.pcos_get_number(indoc, "length:pages");

            /* Loop over all pages of the input document */
            for (int pageno = 1; pageno <= endpage; pageno++) {
                int page = p.open_pdi_page(indoc, pageno, "");

                if (page == -1) {
                    throw new Exception("Error: " + p.get_errmsg());
                }
                
                /* Dummy page size; will be adjusted later */
                p.begin_page_ext(10, 10, "");

                /*
                 * Place the imported page on the output page, and adjust the
                 * page size
                 */
                p.fit_pdi_page(page, 0, 0, "adjustpage");
                p.close_pdi_page(page);

                p.end_page_ext("");
            }
            
            /*
             * Clone XMP metadata of input document if present. In order to
             * be able to merge the XMP from the input document with the
             * XMP needed according to ZUGFeRD for identifying the
             * XML invoice, the XMP from the input document is supplied
             * to the "metadata" option of PDF_end_document(), while the
             * ZUGFeRD XMP was supplied to the "metadata" option of
             * PDF_begin_document().
             */
            String pfv_xmpfile = "/xmp/document.xmp";
            if (!p.pcos_get_string(indoc, "type:/Root/Metadata")
                                                    .equals("stream")) {
                throw new Exception(
                    "Error: XMP metadata missing in input document");
            }
            
            byte[] xmp = p.pcos_get_stream(indoc, "", "/Root/Metadata");
            p.create_pvf(pfv_xmpfile, xmp, "");
            String xmp_optlist = "metadata={filename=" + pfv_xmpfile + "}";
            
            p.close_pdi_document(indoc);

            /*
             * Load the XML file for the invoice, and associate it with the
             * document with relationship "Alternative". Also mark it as an
             * attachment that can be retrieved with Acrobat's normal attachment
             * dialog.
             */
            int xml_invoice = p.load_asset("Attachment", xmlinvoicefile,
                "mimetype=text/xml "
                    + "description={Rechnungsdaten im ZUGFeRD-XML-Format} "
                    + "relationship=Alternative documentattachment=true");

            if (xml_invoice == -1) {
                throw new Exception("Error: " + p.get_errmsg());
            }
            
            p.end_document("associatedfiles={" + xml_invoice + "} "
                            + xmp_optlist);
            
            p.delete_pvf(pfv_xmpfile);
        }
        catch (PDFlibException e) {
            System.err.print("PDFlib exception occurred:\n");
            System.err.print("[" + e.get_errnum() + "] " + e.get_apiname()
                + ": " + e.get_errmsg() + "\n");
	    exitcode = 1;
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
	    exitcode = 1;
        }
        finally {
            if (p != null) {
                p.delete();
            }
	    System.exit(exitcode);
        }
    }
}