Example usage for com.itextpdf.tool.xml.css StyleAttrCSSResolver StyleAttrCSSResolver

List of usage examples for com.itextpdf.tool.xml.css StyleAttrCSSResolver StyleAttrCSSResolver

Introduction

In this page you can find the example usage for com.itextpdf.tool.xml.css StyleAttrCSSResolver StyleAttrCSSResolver.

Prototype

public StyleAttrCSSResolver(final CssFiles cssFiles) 

Source Link

Document

Construct a new StyleAttrCSSResolver with the given CssFiles and the DefaultCssInheritanceRules .

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