HTML Tables

Tags: XML WorkertablesrowspanCSSXHTMLiText 5
Files: 
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/24530852/how-to-get-particular-html-table-contents-to-write-it-in-pdf-using-itext
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.css.CssFile;
import com.itextpdf.tool.xml.css.StyleAttrCSSResolver;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
 
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class ParseHtmlTable1 {
    public static final String DEST = "results/xmlworker/html_table_1.pdf";
    public static final String CSS = "tr { text-align: center; } th { background-color: lightgreen; padding: 3px; } td {background-color: lightblue;  padding: 3px; }";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtmlTable1().createPdf(DEST);
    }
 
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        StringBuilder sb = new StringBuilder();
        sb.append("<table border=\"2\">");
        sb.append("<tr>");
        sb.append("<th>Sr. No.</th>");
        sb.append("<th>Text Data</th>");
        sb.append("<th>Number Data</th>");
        sb.append("</tr>");
        for (int i = 0; i < 10; ) {
            i++;
            sb.append("<tr>");
            sb.append("<td>");
            sb.append(i);
            sb.append("</td>");
            sb.append("<td>This is text data ");
            sb.append(i);
            sb.append("</td>");
            sb.append("<td>");
            sb.append(i);
            sb.append("</td>");
            sb.append("</tr>");
        }
        sb.append("</table>");
 
        CSSResolver cssResolver = new StyleAttrCSSResolver();
        CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
        cssResolver.addCss(cssFile);
 
        // HTML
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
 
        // Pipelines
        PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
 
        // XML Worker
        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);
        p.parse(new ByteArrayInputStream(sb.toString().getBytes()));
 
        // step 5
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/24530852/how-to-get-particular-html-table-contents-to-write-it-in-pdf-using-itext
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.ElementList;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.css.CssFile;
import com.itextpdf.tool.xml.css.StyleAttrCSSResolver;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.ElementHandlerPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
 
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class ParseHtmlTable2 {
    public static final String DEST = "results/xmlworker/html_table_2.pdf";
    public static final String CSS = "tr { text-align: center; } th { background-color: lightgreen; padding: 3px; } td {background-color: lightblue;  padding: 3px; }";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtmlTable2().createPdf(DEST);
    }
 
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("This is my HTML table:"));
        document.add(Chunk.NEWLINE);
        PdfPTable table = getTable();
        document.add(table);
        // step 5
        document.close();
    }
 
    public PdfPTable getTable() throws IOException {
 
        StringBuilder sb = new StringBuilder();
        sb.append("<table border=\"2\">");
        sb.append("<tr>");
        sb.append("<th>Sr. No.</th>");
        sb.append("<th>Text Data</th>");
        sb.append("<th>Number Data</th>");
        sb.append("</tr>");
        for (int i = 0; i < 10; ) {
            i++;
            sb.append("<tr>");
            sb.append("<td>");
            sb.append(i);
            sb.append("</td>");
            sb.append("<td>This is text data ");
            sb.append(i);
            sb.append("</td>");
            sb.append("<td>");
            sb.append(i);
            sb.append("</td>");
            sb.append("</tr>");
        }
        sb.append("</table>");
 
        CSSResolver cssResolver = new StyleAttrCSSResolver();
        CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
        cssResolver.addCss(cssFile);
 
        // HTML
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
 
        // Pipelines
        ElementList elements = new ElementList();
        ElementHandlerPipeline pdf = new ElementHandlerPipeline(elements, null);
        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
 
        // XML Worker
        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);
        p.parse(new ByteArrayInputStream(sb.toString().getBytes()));
 
        return (PdfPTable)elements.get(0);
    }
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/24660890/element-inside-class-css-definition-not-applied-using-itextsharp-xhtml-to-pdf
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class ParseHtmlTable3 {
    public static final String DEST = "results/xmlworker/html_table_3.pdf";
    public static final String HTML = "resources/xml/table_css.html";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtmlTable3().createPdf(DEST);
    }
 
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(HTML));
        // step 5
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/26679003/rowspan-does-not-work-in-itextsharp
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class ParseHtmlTable4 {
    public static final String DEST = "results/xmlworker/html_table_4.pdf";
    public static final String HTML = "resources/xml/table2_css.html";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtmlTable4().createPdf(DEST);
    }
 
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(HTML));
        // step 5
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/26698532/how-can-i-add-cellspacing-to-pdftable-when-parsing-html-using-xmlworker-and-itex
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class ParseHtmlTable5 {
    public static final String DEST = "results/xmlworker/html_table_5.pdf";
    public static final String HTML = "resources/xml/table3_css.html";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtmlTable5().createPdf(DEST);
    }
 
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(HTML));
        // step 5
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/28233592/html-to-pdf-using-itext-or-flying-saucer
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import sandbox.WrapToTest;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
 
/**
 *
 * @author iText
 */
//without @WrapToTest annotation, because there are no Windows fonts on the linux server (Calibri)
public class ParseHtmlTable6 {
    public static final String DEST = "results/xmlworker/html_table_6.pdf";
    public static final String HTML = "resources/xml/paragj.html";
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtmlTable6().createPdf(DEST);
    }
 
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Rectangle pagesize = new Rectangle(PageSize.A4);
        pagesize.setBackgroundColor(new BaseColor(0xFF, 0xFF, 0xEE));
        Document document = new Document(pagesize);
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(HTML),Charset.forName("cp1252"),new XMLWorkerFontProvider("resources/fonts/"));
        // step 5
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/28332971/xmlworker-cells-vertical-align-not-working
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class ParseHtmlTable7 {
    public static final String DEST = "results/xmlworker/html_table_7.pdf";
    public static final String HTML = "resources/xml/table4_css.html";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtmlTable7().createPdf(DEST);
    }
 
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(HTML));
        // step 5
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/29247484/working-with-itextsharp-with-htmls-table
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.css.StyleAttrCSSResolver;
import com.itextpdf.tool.xml.html.CssAppliers;
import com.itextpdf.tool.xml.html.CssAppliersImpl;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import sandbox.WrapToTest;
 
@WrapToTest
public class ParseHtmlTable8 {
    public static final String DEST = "results/xmlworker/html_table_8.pdf";
    public static final String HTML = "resources/xml/table5_css.html";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtmlTable8().createPdf(DEST);
    }
 
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        // CSS
        CSSResolver cssResolver = new StyleAttrCSSResolver();
 
        // HTML
        XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        fontProvider.register("resources/fonts/NotoSansCJKsc-Regular.otf");
        CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
 
        // Pipelines
        PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
 
        // XML Worker
        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);
        p.parse(new FileInputStream(HTML), Charset.forName("UTF-8"));;
        // step 5
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/29575252/table-inside-the-li-convert-to-pdf-using-itext-not-working
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.css.StyleAttrCSSResolver;
import com.itextpdf.tool.xml.html.CssAppliers;
import com.itextpdf.tool.xml.html.CssAppliersImpl;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import sandbox.WrapToTest;
 
@WrapToTest
public class ParseHtmlTable9 {
    public static final String DEST = "results/xmlworker/html_table_9.pdf";
    public static final String HTML = "resources/xml/table9.html";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtmlTable9().createPdf(DEST);
    }
 
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        // CSS
        CSSResolver cssResolver = new StyleAttrCSSResolver();
 
        // HTML
        XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
 
        // Pipelines
        PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
 
        // XML Worker
        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);
        p.parse(new FileInputStream(HTML), Charset.forName("UTF-8"));;
        // step 5
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/30276437/why-is-my-pdf-being-generated-as-blank
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class ParseHtmlTable10 {
    public static final String DEST = "results/xmlworker/html_table_10.pdf";
    public static final String HTML = "resources/xml/table10.html";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtmlTable10().createPdf(DEST);
    }
 
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(HTML));
        // step 5
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/31020576/how-to-customize-fontstyles-while-creating-pdf-from-html-using-itext
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import sandbox.WrapToTest;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
 
//without @WrapToTest annotation, because there are no Windows fonts on the linux server (Calibri)
public class ParseHtmlTable11 {
    public static final String DEST = "results/xmlworker/html_table_11.pdf";
    public static final String HTML = "resources/xml/table11.html";
 
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtmlTable11().createPdf(DEST);
    }
 
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(HTML),Charset.forName("cp1252"), new XMLWorkerFontProvider("resources/fonts/"));
        // step 5
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/32765453/table-width-not-set-in-itextsharp-when-converting-html-to-pdf
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import sandbox.WrapToTest;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
 
//without @WrapToTest annotation, because there are no Windows fonts on the linux server (Calibri)
public class ParseHtmlTable12 {
    public static final String DEST = "results/xmlworker/html_table_12.pdf";
    public static final String HTML = "resources/xml/table12.html";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtmlTable12().createPdf(DEST);
    }
 
    /**
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(HTML), Charset.forName("cp1252"),new XMLWorkerFontProvider("resources/fonts/"));
        // step 5
        document.close();
    }
}
Resources: 
<html>
<head>
<style>
    .test td {
        border: 1px solid green;
    }
</style>
</head>
<body>
<table class='test'>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>Peter</td>
    <td>Griffin</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>Lois</td>
    <td>Griffin</td>
    <td>$150</td>
  </tr>
  <tr>
    <td>Joe</td>
    <td>Swanson</td>
    <td>$300</td>
  </tr>
  <tr>
    <td>Cleveland</td>
    <td>Brown</td>
    <td>$250</td>
</tr>
</table>
</body>
</html>
<table border="1" style="width: 100%">
<tr>
  <td colspan="5" align="center" style="background-color: lightblue">INVOICE</td>
</tr>
<tr>
  <td colspan="2" rowspan="4" style="background-color: white"><b>AIRNET NETWORKS</b><br />asdadadadaada asd asd a ads adsadsadsadasd</td>
  <td>INVOICE</td>
  <td>DATE</td>
  <td>aDATEsd</td>
</tr>
<tr>
    <td>Order</td>
    <td>XXXX</td>
    <td>Ref XXXXXX</td>
</tr>
<tr>
  <td>Delivery</td>
  <td>XXXX</td>
  <td>Ref XXXXXX</td>
</tr>
<tr>
  <td>Due Date</td>
  <td>XXXX</td>
  <td>Ref XXXXXX</td>
</tr>
<tr>
  <td colspan="2" rowspan="4" style="background-color: white"><p><b >CUSTOMER NAME</b></p> asd asd adadaadadadada  adadaadsasdad ada asd adad</td>
  <td>Customer Care No:</td>
  <td colspan="2">544646454,88877978975</td>
</tr>
<tr>
  <td>Email Id</td>
  <td colspan="2">airnet@gmail.com</td>
</tr>
<tr>
  <td>Account Details</td>
  <td colspan="2">5522245125545455 IFSC 323hasd<br /> SBI India</td>
</tr>
</table>
<div>&nbsp;</div>
<table border="1" style="width: 100%">
<tr>
  <td  style="background-color: lightblue" height="15" >Srno</td>
  <td  style="background-color: lightblue">Particulars</td>
  <td  style="background-color: lightblue">Quantity</td>
  <td  style="background-color: lightblue">Rate/Month</td>
  <td  style="background-color: lightblue">Total Rupees</td>
</tr>
<tr>
  <td valign="top">1</td>
  <td valign="top">1 MBPS Plan</td>
  <td valign="top">1</td>
  <td valign="top">600</td>
  <td valign="top">692</td>
</tr>
<tr>
  <td height="300" valign="top" >1</td>
  <td valign="top">1 MBPS Plan</td>
  <td valign="top">1</td>
  <td valign="top">600</td>
  <td valign="top">692</td>
</tr>
<tr>
  <td colspan="3" rowspan="3" valign="top">asdasdasd</td>
  <td colspan="1">Total</td>
  <td colspan="1">692</td>
</tr>
<tr>
  <td>Service Tax</td>
  <td>692</td>
</tr>
<tr>
  <td>Grand Total</td>
  <td>692</td>
</tr>
</table>
<html>
<head>
<style>
    table, td {
        border: 1px solid green;
        border-spacing: 0px;
        padding: 0px;
    }
</style>
</head>
<body>
<table class='test'>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>Peter</td>
    <td>Griffin</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>Lois</td>
    <td>Griffin</td>
    <td>$150</td>
  </tr>
  <tr>
    <td>Joe</td>
    <td>Swanson</td>
    <td>$300</td>
  </tr>
  <tr>
    <td>Cleveland</td>
    <td>Brown</td>
    <td>$250</td>
</tr>
</table>
</body>
</html>
<html>
<head>
<title>Summary log</title>
<style type="text/css">
body, html { border: 0px }
 
h1, h2, h3, h4, h5, h6 {font-family: Arial, Helvetica, sans-serif }
body, p { font-family: "verdana", tahoma, arial, sans-serif }
code { font-family: "Courier New", Courier, monospace }
 
body            { background-color: #FFFFEE; font-family: "verdana", tahoma, arial, sans-serif;}
p           { font-size: 11px; font-family: "verdana", tahoma, arial, sans-serif; color: #040404; background-color: #FFFFEE; margin-top: 5px; margin-bottom: 8px; margin-left: 10px; }
h1          { font-size: 17px; color: #FFA500; margin-bottom: 10px; background-color: #0000EE; padding-top:6px; padding-bottom:7px; padding-left:10px; }    
h2          { font-size: 15px; color: #0055AA; margin-bottom: 10px; margin-top: 16px;  background-color:#FFCC66; padding-top:0px; padding-bottom:2px; padding-left:10px; }
h3          { font-size: 15px; color: #660000; margin-bottom: 8px;  margin-top: 15px; }
h4          { font-size: 12px; color: black; margin-bottom: 8px;  margin-top: 15px; margin-left: 8px; font-style: normal; }
code        { font-size: 12px; margin-left: 16px; padding-left:16px} 
hr          { height: 0px; noshade: true; border: 1; width: 100%; }
 
/* FFCC66 FFEE99 */
 
.pError {
 font-size: 11px;
 font-family: "verdana", tahoma, arial, sans-serif;
 color: red;
 background-color: #FFFFEE;
 margin-top: 5px;
 margin-bottom: 8px;
 margin-left: 10px;
}
.pSkipped_PrevTaskFailed {/* Process Manager: Task skipped since previous task failed*/
 font-size: 11px;
 font-family: "verdana", tahoma, arial, sans-serif;
 color: #FF9D8B; /*Light red shade color*/
 background-color: #FFFFEE;
 margin-top: 5px;
 margin-bottom: 8px;
 margin-left: 10px;
}
.pSkipped_DateNotInRange {/* Process Manager: Task skipped since run date not in range*/
 font-size: 11px;
 font-family: "verdana", tahoma, arial, sans-serif;
 color:  #00BFFF; /*Light blue shade color*/
 background-color: #FFFFEE;
 margin-top: 5px;
 margin-bottom: 8px;
 margin-left: 10px;
}
.pAbort {/* Process Manager: Task is aborted*/
 font-size: 11px;
 font-family: "verdana", tahoma, arial, sans-serif;
 color: #FFA500; /*Orange color*/
 background-color: #FFFFEE;
 margin-top: 5px;
 margin-bottom: 8px;
 margin-left: 10px;
}
.pNeutral {
 font-size: 11px;
 font-family: "verdana", tahoma, arial, sans-serif;
 color: silver;
 background-color: #FFFFEE;
 margin-top: 5px;
 margin-bottom: 8px;
 margin-left: 10px;
}
.pSummary {
 font-size: 11px;
 font-family: "verdana", tahoma, arial, sans-serif;
 color: blue;
 background-color: #FFFFEE;
 margin-top: 0px;
 margin-bottom: 2px;
 margin-left: 10px;
}
.tableHead {
 font-size: 15px;
 font-weight: bold;
 color: #0055AA;
 margin-bottom: 10px;
 margin-top: 16px;
 background-color:#FFCC66;
 padding-top:0px;
 padding-bottom:2px;
 /*padding-left:10px;*/
}
.tdPadding {
padding-top:0px;
padding-bottom:0px;
padding-left:7px;
padding-right:7px;
}
.h2RowHead {
 background-color: #999999;
 color: #eeeeee;
 font-weight: bold
}
 
.tableContent {
 font-size: 11px;
 font-weight: normal;
 color: black;
 margin-bottom: 10px;
 margin-top: 16px;
 background-color:#FFCC66;
 padding-top:0px;
 padding-bottom:2px;
 /* padding-left:10px; */
}
</style>
</head>
<body>
<table class="tableHead" width="100%">
<tr>
<td align="left" class="tdPadding">Summary log</td></tr>
<tr class="tableContent"> 
<td align="left" class="tdPadding"><b>User's account name: </b>First.Last</td>
<td align="center" class="tdPadding"><b>Server: </b>someServer</td>
<td align="right" class="tdPadding"><b>Execution date: </b>22.01.2015 04:49:37:918</td>
</tr>
</table>
 
<table width="100%">
<tr><td class="h2RowHead">Running Process for [22.01.2015] </td></tr>
</table>
<p><b><h4>MAIN</h4></b></p>
<hr />
<p class="pError">(1) Task: <i>StopOnFailure1</i> invoked at 22.01.2015 04:49:37:934 >>  <b>FAILURE!</b> Task finished with return code: 13. Component: StopOnFailure1 timed out.</p>
<br />
<p>Record Statistics: Processed: 68680 | Filtered: 0 | Failed: 0 | Running time: 00:00:05</p>
<hr />
<p class="pError">(2) Task: <i>DataFlow1</i> invoked at 22.01.2015 04:49:43:254 >>  <b>FAILURE!</b> Task finished with return code: 4. Failed to load Component from file "C:\doc\API\API.txt". Reason: Component now found.</p>
<br />
<p class="pError">(3) Task: <i>DataFlow2</i> invoked at 22.01.2015 04:49:43:300 >> <b>FAILURE!</b> Reason: Current object state [Ready] doesn't allow to stop this component.</p>
<br />
<table width="100%">
<tr><td class="h2RowHead">Process execution summary:</td></tr>
</table>
<p>Active Tasks: 3</p>
<br />
Executed Tasks: 3<br />
Successful Tasks:  0<br />
Failed Tasks:  3<br />
Skipped Tasks based on calendar: 0<br />
Skipped Tasks based on result of previous Task: 0<br />
<b>Process completed successfully. Elapsed time: 5 seconds.</b>
</body>
</html>
<table>
    <tr>
        <td>
            <table border="1">
                <tr><td>Blah</td></tr>
                <tr><td>Blah</td></tr>
                <tr><td>Blah</td></tr>
            </table>
        </td>
        <td valign="top">
            <table border="1">
                <tr><td>Blah</td></tr>
                <tr><td>Blah</td></tr>
            </table>
        </td>
    </tr>
</table>
<html><body style="font-family: NotoSansCJKsc-Regular">
<table border="1" width="100%" cellspacing="0">
<tr>
	<td>a</td>
	<td colspan="2" align="center" >bbb</td>
	<td></td>
</tr>
<tr>
	<td>a</td>
	<td colspan="2" align="center" >bbb</td>
	<td>c</td>
</tr>
<tr>
	<td>Tel:</td>
	<td>123123</td>
	<td>Fax:</td>
	<td>32321</td>
</tr>
</table>
 
<table border="1" width="100%" cellspacing="0">
<tr>
	<td>致 :</td>
	<td>ABCD</td>
	<td>EFGH</td>
	<td>日期 :</td>
	<td>April 1</td>
</tr>
<tr>
	<td>地址 :</td>
	<td colspan="2">XYZ 123</td>
	<td>發票號碼 :</td>
	<td>May 1</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td align="center" colspan="3">INVOICE</td>
	<td>&nbsp;</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td align="center" colspan="3">發票</td>
	<td>&nbsp;</td>
</tr>
</table>
<table border="1" width="100%" cellspacing="0">
<tr>
	<td colspan="6">==============================================================================</td>
</tr>
<tr>
	<td width="5%">單號</td>
	<td width="5%">日期</td>
	<td width="5%">貨品</td>
	<td width="5%">數量</td>
	<td width="10%">單價(HK$)</td>
	<td width="70%">金額(HK$)</td>
</tr>
<tr>
	<td colspan="6">==============================================================================</td>
</tr>
<tr>
	<td colspan="6" height="70">1</td>
</tr>
</table>
</body></html>
<ul>
<li>Please find the table below:
    <table border="1">
    <tr><td>Blah</td><td>Blah</td><td>Blah</td></tr>
    <tr><td>Blah</td><td>Blah</td><td>Blah</td></tr>
    <tr><td>Blah</td><td>Blah</td><td>Blah</td></tr>
    </table>
</li>
<li>This is a list item with only text</li>
<li>Below you will find a list item with a table</li>
<li><table border="1" width="100%"><tr><td>Blah</td><td>Blah</td><td>Blah</td></tr><tr><td>Blah</td><td>Blah</td><td>Blah</td></tr><tr><td>Blah</td><td>Blah</td><td>Blah</td></tr></table></li>
</ul>
    <table style="font-size:10pt">
        <tr>
            <td rowspan="4"> Address<br/> </td>
            <td>Phone: phone</td>
        </tr>
        <tr>
            <td>Fax: fax</td>
        </tr>
        <tr>
            <td>Email: email@example.com</td>
        </tr>
        <tr>
            <td>Website: example.com</td>
        </tr>
    </table>
 
    <table style="font-size:10pt; width:100%">
        <tr style="width:50%">
            <td>Settlement: 30 days from invoice</td>
        </tr>
        <tr>
            <td>Delivery Charge Notes: N/A</td>
        </tr>
    </table>
    <p style="width:100%; font-size:10pt">
    I love notes</p>
 
    <table>
    <tr style="font-weight:bold">
        <td>Item</td>
        <td>Item Code</td>
        <td>Description</td>
        <td>Unit Size</td>
        <td>Units Per Outer</td>
        <td>Units Per Pallet</td>
        <td>Invoice Price Volume Breaks</td>
        <td>Branding</td>
        <td>Notes</td>
    </tr>
 
    </table>
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></meta>
<meta name="Generator" content="Microsoft Word 14 (filtered medium)"></meta>
<style>
p.MsoNormal, li.MsoNormal, div.MsoNormal
    {margin:0cm;
    margin-bottom:.0001pt;
    font-size:11.0pt;
    font-family: Calibri, sans-serif;
    mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
    {color:blue;
    text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
    {color:purple;
    text-decoration:underline;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
    {
    margin:0cm;
    margin-bottom:.0001pt;
    font-size:8.0pt;
    font-family:Tahoma, sans-serif;}
span.EmailStyle17
    {font-family: Calibri, sans-serif;
    color:windowtext;}
span.BalloonTextChar
    {font-family:Tahoma, sans-serif;}
.MsoChpDefault
    {font-family: Calibri, sans-serif;}
</style>
</head>
<body lang="EN-IE" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">FYI</p>
<p class="MsoNormal"></p>
<table class="MsoNormalTable" border="0" cellspacing="0" cellpadding="0" width="224" style="width:168.0pt;margin-left:-.75pt;border-collapse:collapse">
<tr style="height:15.0pt">
<td width="77" nowrap="" valign="bottom" style="width:58.0pt;background:gray;padding:0cm 5.4pt 0cm 5.4pt;height:15.0pt">
<p class="MsoNormal"><b><span style="color:white;">Number</span></b></p>
</td>
<td width="63" nowrap="" valign="bottom" style="width:47.0pt;background:gray;padding:0cm 5.4pt 0cm 5.4pt;height:15.0pt">
<p class="MsoNormal"><b><span style="color:white">Currency</span></b></p>
</td>
<td width="84" nowrap="" valign="bottom" style="width:63.0pt;background:gray;padding:0cm 5.4pt 0cm 5.4pt;height:15.0pt">
<p class="MsoNormal"><b><span style="color:white">Balance Due</span></b></p>
</td>
</tr>
<tr style="height:15.0pt">
<td width="77" nowrap="" valign="bottom" style="width:58.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:15.0pt">
<p class="MsoNormal" align="right" style="text-align:right"><span style="color:black">3140552178</span></p>
</td>
<td width="63" nowrap="" valign="bottom" style="width:47.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:15.0pt">
<p class="MsoNormal"><span style="color:black">EUR</span></p>
</td>
<td width="84" nowrap="" valign="bottom" style="width:63.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:15.0pt">
<p class="MsoNormal" align="right" style="text-align:right"><span style="color:red">-1,786.38
</span><span style="color:black"></span></p>
</td>
</tr>
<tr style="height:15.0pt">
<td width="77" nowrap="" valign="bottom" style="width:58.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:15.0pt">
<p class="MsoNormal" align="right" style="text-align:right"><span style="color:black">3700081975</span></p>
</td>
<td width="63" nowrap="" valign="bottom" style="width:47.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:15.0pt">
<p class="MsoNormal"><span style="color:black">EUR</span></p>
</td>
<td width="84" nowrap="" valign="bottom" style="width:63.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:15.0pt">
<p class="MsoNormal" align="right" style="text-align:right"><span style="color:red">-1,005.00
</span><span style="color:black"></span></p>
</td>
</tr>
<tr style="height:15.0pt">
<td width="77" nowrap="" valign="bottom" style="width:58.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:15.0pt">
<p class="MsoNormal" align="right" style="text-align:right"><span style="color:black">3700081976</span></p>
</td>
<td width="63" nowrap="" valign="bottom" style="width:47.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:15.0pt">
<p class="MsoNormal"><span style="color:black">EUR</span></p>
</td>
<td width="84" nowrap="" valign="bottom" style="width:63.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:15.0pt">
<p class="MsoNormal" align="right" style="text-align:right"><span style="color:red">-1,005.00
</span><span style="color:black"></span></p>
</td>
</tr>
</table>
</div>
</body>
</html>
<table cellpadding='4' cellspacing='4' border='1' width='100%' style='width:100%'>
    <tr style='background-color:#000000'>
        <td colspan='2' align='center' valign='middle'>
            <font face='Calibri' size='6' color='#FFFFFF'>XXXX XXXXX XXXXX</font>
        </td>
    </tr>
    <tr>
        <td colspan='2'>&nbsp;</td>
    </tr>
    <tr>
        <td width='90%' style='width:90%'>
            <table cellpadding='0' cellspacing='0' border='1' width='100%'>
                <tr>
                    <td width='42%'>
                        <font face='Calibri' size='4'>
                            <b>Deal Number</b>
                        </font>
                    </td>
                    <td width='1%'>&nbsp;</td>
                    <td width='57%'>
                        <font face='Calibri' size='4'>
                            <b>XXXXXXXXXX</b>
                        </font>
                    </td>
                </tr>
                <tr>
                    <td colspan='3' width='100%'>&nbsp;</td>
                </tr>
                <tr>
                    <td width='42%'>
                        <font face='Calibri' size='2'>
                            <b>Trade Date</b>
                        </font>
                    </td>
                    <td width='1%'>&nbsp;</td>
                    <td width='57%'>
                        <font face='Calibri' size='2'>February 09, 2015</font>
                    </td>
                </tr>
                <tr>
                    <td width='42%'>
                        <font face='Calibri' size='2'>
                            <b>Price Date</b>
                        </font>
                    </td>
                    <td width='1%'>&nbsp;</td>
                    <td width='57%'>
                        <font face='Calibri' size='2'>February 09, 2015</font>
                    </td>
                </tr>
                <tr>
                    <td width='42%'>
                        <font face='Calibri' size='2'>
                            <b>Authorize Date</b>
                        </font>
                    </td>
                    <td width='1%'>&nbsp;</td>
                    <td width='57%'>
                        <font face='Calibri' size='2'>February 09, 2015</font>
                    </td>
                </tr>
                <tr>
                    <td colspan='3' width='100%'>&nbsp;</td>
                </tr>
            </table>
        </td>
        <td width='10%' style='width:10%' valign='top'>
            <table cellpadding='0' cellspacing='0' border='1' width='100%'>
                <tr>
                    <td colspan='2' align='center' width='100%'>
                        <font face='Calibri' size='2'>
                            <b>Xxxxxxx (XXXXXXX)</b>
                        </font>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
File nameRaw URLUpdated
table_css.htmltable_css.html2015-11-05 1:05 pm
table2_css.htmltable2_css.html2015-11-05 1:05 pm
table3_css.htmltable3_css.html2015-11-05 1:05 pm
paragj.htmlparagj.html2015-11-05 1:05 pm
table4_css.htmltable4_css.html2015-11-05 1:05 pm
table5_css.htmltable5_css.html2015-11-05 1:05 pm
table9.htmltable9.html2015-11-05 1:05 pm
table10.htmltable10.html2015-11-05 1:05 pm
table11.htmltable11.html2015-11-05 1:05 pm
table12.htmltable12.html2015-11-05 1:05 pm