Creating HTML invoices

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

31st August 2015
admin-marketing

Switch code for this example

HtmlInvoicesComfort.java
  1. /*
  2.  * This example was written by Bruno Lowagie.
  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.ComfortProfile;
  10.  
  11. import java.io.ByteArrayInputStream;
  12. import java.io.File;
  13. import java.io.FileInputStream;
  14. import java.io.FileOutputStream;
  15. import java.io.FileWriter;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.io.Writer;
  20. import java.sql.SQLException;
  21. import java.util.List;
  22.  
  23. import javax.xml.parsers.ParserConfigurationException;
  24. import javax.xml.transform.Transformer;
  25. import javax.xml.transform.TransformerException;
  26. import javax.xml.transform.TransformerFactory;
  27. import javax.xml.transform.stream.StreamResult;
  28. import javax.xml.transform.stream.StreamSource;
  29.  
  30. import org.xml.sax.SAXException;
  31.  
  32. import zugferd.data.InvoiceData;
  33. import zugferd.pojo.Invoice;
  34. import zugferd.pojo.PojoFactory;
  35.  
  36. /**
  37.  * @author Bruno Lowagie
  38.  */
  39. public class HtmlInvoicesComfort {
  40.     public static final String DEST = "results/zugferd/html/comfort%05d.html";
  41.     public static final String XSL = "resources/zugferd/invoice.xsl";
  42.     public static final String CSS = "resources/zugferd/invoice.css";
  43.     public static final String LOGO = "resources/zugferd/logo.png";
  44.    
  45.     public static void main(String[] args) throws SQLException, IOException, ParserConfigurationException, SAXException, DataIncompleteException, InvalidCodeException, TransformerException {
  46.         File file = new File(DEST);
  47.         file.getParentFile().mkdirs();
  48.         File css = new File(CSS);
  49.         copyFile(css, new File(file.getParentFile(), css.getName()));
  50.         File logo = new File(LOGO);
  51.         copyFile(logo, new File(file.getParentFile(), logo.getName()));
  52.         HtmlInvoicesComfort app = new HtmlInvoicesComfort();
  53.         PojoFactory factory = PojoFactory.getInstance();
  54.         List invoices = factory.getInvoices();
  55.         for (Invoice invoice : invoices) {
  56.             app.createHtml(invoice, new FileWriter(String.format(DEST, invoice.getId())));
  57.         }
  58.         factory.close();
  59.     }
  60.    
  61.     public void createHtml(Invoice invoice, Writer writer) throws IOException, ParserConfigurationException, SAXException, DataIncompleteException, InvalidCodeException, TransformerException {
  62.         ComfortProfile comfort = new InvoiceData().createComfortProfileData(invoice);
  63.         InvoiceDOM dom = new InvoiceDOM(comfort);
  64.         StreamSource xml = new StreamSource(new ByteArrayInputStream(dom.toXML()));
  65.         StreamSource xsl = new StreamSource(new File(XSL));
  66.         TransformerFactory factory = TransformerFactory.newInstance();
  67.         Transformer transformer = factory.newTransformer(xsl);
  68.         transformer.transform(xml, new StreamResult(writer));
  69.         writer.flush();
  70.         writer.close();
  71.     }
  72.    
  73.     private static void copyFile(File source, File dest) throws IOException {
  74.         InputStream input = new FileInputStream(source);
  75.         OutputStream output = new FileOutputStream(dest);
  76.         byte[] buf = new byte[1024];
  77.         int bytesRead;
  78.         while ((bytesRead = input.read(buf)) > 0) {
  79.             output.write(buf, 0, bytesRead);
  80.         }
  81.     }
  82. }
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