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

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

Introduction

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

Prototype

void removeProcessor(String tag);

Source Link

Document

Removes a TagProcessor for a specific tag.

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();/*from w ww . ja  v a 2 s. com*/
        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();
    }
}