Example usage for com.itextpdf.tool.xml.pipeline.html HtmlPipelineContext setAcceptUnknown

List of usage examples for com.itextpdf.tool.xml.pipeline.html HtmlPipelineContext setAcceptUnknown

Introduction

In this page you can find the example usage for com.itextpdf.tool.xml.pipeline.html HtmlPipelineContext setAcceptUnknown.

Prototype

public HtmlPipelineContext setAcceptUnknown(final boolean acceptUnknown) 

Source Link

Document

Set to true to allow the HtmlPipeline to accept tags it does not find in the given TagProcessorFactory

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 a2s.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();
    }
}