Using iText, how can I apply css to my PDF document using CssResolver and CssFile?

前端 未结 4 2497
滥情空心
滥情空心 2021-02-20 14:00

Thanks for your help...

Problem

While converting an html table tag/snippet (which I have converted to string) into a PDF document...

I am able to

相关标签:
4条回答
  • 2021-02-20 14:25

    For cssResolver you need to specify the absoulte path for the .css as shown below.

    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
    
    CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(false);
    
    cssResolver.addCssFile("src\\template\\style.css",true);
    Pipeline<?> pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
    
    XMLWorker worker = new XMLWorker(pipeline, true);
    XMLParser xmlParser = new XMLParser(worker);
    
    0 讨论(0)
  • 2021-02-20 14:28

    I think a better and more powerful (and higher level of abstraction) tool for this purpose (generating PDF files from XHTML) would be Flying Saucer, aka. xhmlrenderer.

    It gives complete CSS2 support and a few CSS3 properties for print media, and you can extend it to render any PDF's (see the ReplacementElementFactory API).

    I've used it to render dynamic PDF reports (with freemarker), with custom tags, like . Very simple tool and easy to understand.

    See: http://code.google.com/p/flying-saucer/

    The user guide can be found here: http://flyingsaucerproject.github.com/flyingsaucer/r8/guide/users-guide-R8.html

    0 讨论(0)
  • 2021-02-20 14:32

    nope flying saucer is out of lifetime not supported anymore. we having lots of trouble with it. for private use its ok, but not in a business case.

    0 讨论(0)
  • 2021-02-20 14:40

    Created a project starting from your code, tested and it works very well to create a PDF file from a html file with external CSS.

    Here is the public repository: https://github.com/valentin-nasta/itext-html-css-pdf-jsf-template .

    0 讨论(0)
提交回复
热议问题