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
  1. /*
  2.  * Example that shows how to create XML invoices.
  3.  */
  4. package zugferd;
  5.  
  6. import com.itextpdf.text.zugferd.InvoiceDOM;
  7. import com.itextpdf.text.zugferd.exceptions.DataIncompleteException;
  8. import com.itextpdf.text.zugferd.exceptions.InvalidCodeException;
  9. import com.itextpdf.text.zugferd.profiles.BasicProfile;
  10.  
  11. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.sql.SQLException;
  15. import java.util.List;
  16.  
  17. import javax.xml.parsers.ParserConfigurationException;
  18. import javax.xml.transform.TransformerException;
  19.  
  20. import org.xml.sax.SAXException;
  21.  
  22. import zugferd.data.InvoiceData;
  23. import zugferd.pojo.Invoice;
  24. import zugferd.pojo.PojoFactory;
  25.  
  26. /**
  27.  * @author  Bruno Lowagie
  28.  */
  29. public class XmlInvoicesComfort {
  30.     public static final String DEST = "results/zugferd/xml/comfort%05d.xml";
  31.    
  32.     public static void main(String[] args) throws SQLException, ParserConfigurationException, SAXException, IOException, TransformerException, DataIncompleteException, InvalidCodeException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         PojoFactory factory = PojoFactory.getInstance();
  36.         List invoices = factory.getInvoices();
  37.         InvoiceData invoiceData = new InvoiceData();
  38.         BasicProfile comfort;
  39.         InvoiceDOM dom;
  40.         for (Invoice invoice : invoices) {
  41.             comfort = invoiceData.createComfortProfileData(invoice);
  42.             dom = new InvoiceDOM(comfort);
  43.             byte[] xml = dom.toXML();
  44.             FileOutputStream fos = new FileOutputStream(String.format(DEST, invoice.getId()));
  45.             fos.write(xml);
  46.             fos.flush();
  47.             fos.close();
  48.         }
  49.         factory.close();
  50.     }
  51.  
  52. }
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