Creating ZUGFeRD XML files

This is a code example of iText PDF, discover more.

30th August 2015
admin-marketing

Switch code for this example

XmlInvoicesComfort.java
/*
 * Example that shows how to create XML invoices.
 */
package zugferd;

import com.itextpdf.text.zugferd.InvoiceDOM;
import com.itextpdf.text.zugferd.exceptions.DataIncompleteException;
import com.itextpdf.text.zugferd.exceptions.InvalidCodeException;
import com.itextpdf.text.zugferd.profiles.BasicProfile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.xml.sax.SAXException;

import zugferd.data.InvoiceData;
import zugferd.pojo.Invoice;
import zugferd.pojo.PojoFactory;

/**
 * @author  Bruno Lowagie
 */
public class XmlInvoicesComfort {
    public static final String DEST = "results/zugferd/xml/comfort%05d.xml";
    
    public static void main(String[] args) throws SQLException, ParserConfigurationException, SAXException, IOException, TransformerException, DataIncompleteException, InvalidCodeException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        PojoFactory factory = PojoFactory.getInstance();
        List invoices = factory.getInvoices();
        InvoiceData invoiceData = new InvoiceData();
        BasicProfile comfort;
        InvoiceDOM dom;
        for (Invoice invoice : invoices) {
            comfort = invoiceData.createComfortProfileData(invoice);
            dom = new InvoiceDOM(comfort);
            byte[] xml = dom.toXML();
            FileOutputStream fos = new FileOutputStream(String.format(DEST, invoice.getId()));
            fos.write(xml);
            fos.flush();
            fos.close();
        }
        factory.close();
    }

}
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now