Example usage for com.itextpdf.tool.xml.html TagProcessorFactory addProcessor

List of usage examples for com.itextpdf.tool.xml.html TagProcessorFactory addProcessor

Introduction

In this page you can find the example usage for com.itextpdf.tool.xml.html TagProcessorFactory addProcessor.

Prototype

void addProcessor(final TagProcessor processor, final String... tags);

Source Link

Document

Add a tag processor and the tags it maps to.

Usage

From source file:org.freelancertech.poc.itext.ParseHtmlTable1.java

public static byte[] convertHtmlToPdf(String content) throws IOException, DocumentException {
    final ClassLoader classLoader = ParseHtmlTable1.class.getClassLoader();
    try (ByteArrayOutputStream file = new ByteArrayOutputStream();
            InputStream is = new ByteArrayInputStream(content.getBytes());) {
        final Document document = new Document();
        final PdfWriter writer = PdfWriter.getInstance(document, file);
        document.open();// w  w w.  j av  a2  s .  c o m
        final TagProcessorFactory tagProcessorFactory = Tags.getHtmlTagProcessorFactory();
        tagProcessorFactory.removeProcessor(HTML.Tag.IMG);
        tagProcessorFactory.addProcessor(new ImageTagProcessor(), HTML.Tag.IMG);
        final CssFile cssFile = XMLWorkerHelper.getCSS(classLoader.getResourceAsStream(STYLE_FILE_NAME));
        final CssFilesImpl cssFiles = new CssFilesImpl(cssFile);
        //cssFiles.add(XMLWorkerHelper.getInstance().getDefaultCSS());
        final StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
        final HtmlPipelineContext hpc = new HtmlPipelineContext(
                new CssAppliersImpl(new XMLWorkerFontProvider()));
        hpc.setAcceptUnknown(true).autoBookmark(true).setTagFactory(tagProcessorFactory);
        final HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(document, writer));
        final Pipeline<?> pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
        final XMLWorker worker = new XMLWorker(pipeline, true);
        final Charset charset = Charset.forName("UTF-8");
        final XMLParser xmlParser = new XMLParser(true, worker, charset);

        xmlParser.parse(is, charset);
        document.close();
        return file.toByteArray();
    }
}