Parsing more than one HTML files to a single PDF

Tags: XMLWorkerXHTMLiText 5

These examples were written in answer to the question How to parse multiple HTML files into a single PDF?

Files: 
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/27814701/itextsharp-how-to-use-htmlworker-to-covert-html-to-pdf-with-pagination
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class ParseMultipleHtmlFiles1 {
    public static final String[] HTML = {
        "resources/xml/page1.html",
        "resources/xml/page2.html",
        "resources/xml/page3.html"
    };
    public static final String DEST = "results/xmlworker/multiple_html_pages1.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseMultipleHtmlFiles1().createPdf(DEST);
    }
 
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(file));
        document.open();
        PdfReader reader;
        for (String html : HTML) {
            reader = new PdfReader(parseHtml(html));
            copy.addDocument(reader);
            reader.close();
        }
        document.close();
    }
 
    public byte[] parseHtml(String html) throws DocumentException, IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(html));
        // step 5
        document.close();
        // return the bytes of the PDF
        return baos.toByteArray();
    }
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/27814701/itextsharp-how-to-use-htmlworker-to-covert-html-to-pdf-with-pagination
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Utilities;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.ElementList;
import com.itextpdf.tool.xml.XMLWorkerHelper;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import sandbox.WrapToTest;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class ParseMultipleHtmlFiles2 {
    public static final String[] HTML = {
        "resources/xml/page1.html",
        "resources/xml/page2.html",
        "resources/xml/page3.html"
    };
    public static final String DEST = "results/xmlworker/multiple_html_pages2.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseMultipleHtmlFiles2().createPdf(DEST);
    }
 
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        String css = readCSS();
        for (String htmlfile : HTML) {
            String html = Utilities.readFileToString(htmlfile);
            ElementList list = XMLWorkerHelper.parseToElementList(html, css);
            for (Element e : list) {
                document.add(e);
            }
            document.newPage();
        }
        document.close();
    }
 
    private String readCSS() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        InputStream is = XMLWorkerHelper.class.getResourceAsStream("/default.css");
        while ((length = is.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }
        return new String(baos.toByteArray());
    }
}
Resources: 
<h1>Information about California</h1>
<table style="border: solid 1pt;" cellspacing="0">
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Name:</td>
<td style="width:240.5pt;border: solid 0.5pt;" colspan="2">CALIFORNIA</td>
<td style="width:114pt;border: solid 0.5pt;">Abbreviation:</td>
<td style="width:37pt;border: solid 0.5pt;">CA</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Capital:</td>
<td style="width:128pt;border: solid 0.5pt;">Sacramento</td>
<td style="width:113pt;border: solid 0.5pt;">Most populous city:</td>
<td style="width:150.5pt;border: solid 0.5pt;" colspan="2">Los Angeles</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Population:</td>
<td style="width:128pt;border: solid 0.5pt;">36,961,664</td>
<td style="width:113pt;border: solid 0.5pt;">Square miles:</td>
<td style="width:150.5pt;border: solid 0.5pt;" colspan="2">163,707</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Timezones:</td>
<td style="width:128pt;border: solid 0.5pt;">PT (UTC-8)</td>
<td style="width:113pt;border: solid 0.5pt;">&nbsp;</td>
<td style="width:114pt;border: solid 0.5pt;">Daylight Savings:</td>
<td style="width:37pt;border: solid 0.5pt;">YES</td>
</tr>
</table>
<h1>Information about New York</h1>
<table style="border: solid 1pt;" cellspacing="0">
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Name:</td>
<td style="width:240.5pt;border: solid 0.5pt;" colspan="2">NEW YORK</td>
<td style="width:114pt;border: solid 0.5pt;">Abbreviation:</td>
<td style="width:37pt;border: solid 0.5pt;">NY</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Capital:</td>
<td style="width:128pt;border: solid 0.5pt;">Albany</td>
<td style="width:113pt;border: solid 0.5pt;">Most populous city:</td>
<td style="width:150.5pt;border: solid 0.5pt;" colspan="2">New York</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Population:</td>
<td style="width:128pt;border: solid 0.5pt;">19,541,453</td>
<td style="width:113pt;border: solid 0.5pt;">Square miles:</td>
<td style="width:150.5pt;border: solid 0.5pt;" colspan="2">54,475</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Timezones:</td>
<td style="width:128pt;border: solid 0.5pt;">EST (UTC-5)</td>
<td style="width:113pt;border: solid 0.5pt;">&nbsp;</td>
<td style="width:114pt;border: solid 0.5pt;">Daylight Savings:</td>
<td style="width:37pt;border: solid 0.5pt;">YES</td>
</tr>
</table>
<h1>Information about Massachusetts</h1>
<table style="border: solid 1pt;" cellspacing="0">
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Name:</td>
<td style="width:240.5pt;border: solid 0.5pt;" colspan="2">MASSACHUSETTS</td>
<td style="width:114pt;border: solid 0.5pt;">Abbreviation:</td>
<td style="width:37pt;border: solid 0.5pt;">MA</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Capital:</td>
<td style="width:128pt;border: solid 0.5pt;">Boston</td>
<td style="width:113pt;border: solid 0.5pt;">Most populous city:</td>
<td style="width:150.5pt;border: solid 0.5pt;" colspan="2">Boston</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Population:</td>
<td style="width:128pt;border: solid 0.5pt;">6,593,587</td>
<td style="width:113pt;border: solid 0.5pt;">Square miles:</td>
<td style="width:150.5pt;border: solid 0.5pt;" colspan="2">10,555</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Timezones:</td>
<td style="width:128pt;border: solid 0.5pt;">EST (UTC-5)</td>
<td style="width:113pt;border: solid 0.5pt;">&nbsp;</td>
<td style="width:114pt;border: solid 0.5pt;">Daylight Savings:</td>
<td style="width:37pt;border: solid 0.5pt;">YES</td>
</tr>
</table>
File nameRaw URLUpdated
page1.htmlpage1.html2015-11-05 2:22 pm
page2.htmlpage2.html2015-11-05 2:22 pm
page3.htmlpage3.html2015-11-05 2:22 pm