How to add external CSS while generating PDF?

2019-02-27 03:42发布

问题:

Currently i am using following code to generate PDF in a JSP file:

response.setContentType("application/force-download");                                                          
response.setHeader("Content-Disposition", "attachment;filename=reports.pdf");                                                     
Document document = new Document();                                      
document.setPageSize(PageSize.A1);

PdfWriter writer = null;                                                      

writer = PdfWriter.getInstance(document, response.getOutputStream());                                         

document.open();                                                                                   

ByteArrayInputStream bis = new ByteArrayInputStream(htmlSource.toString().getBytes());                                                                                      

XMLWorkerHelper.getInstance().parseXHtml(writer, document, bis);                                                                   

document.close();     

With this am able to generate PDF.

But i would like to add CSS file while generating PDF.

Please Help me...

回答1:

i am not sure in java how can you use but in c# you can add external style sheet code or syntax by this code:-

StyleSheet css = new StyleSheet();
    css.LoadTagStyle("body", "face", "Garamond");
    css.LoadTagStyle("body", "encoding", "Identity-H");
    css.LoadTagStyle("body", "size", "12pt");

may be this helps you

Regards, vinit



回答2:

Please take a look at the ParseHtmlTable1 example. In this example, we have HTML stored in a StringBuilder object and some CSS stored in a String. In my example, I convert the sb object and the CSS object to an InputStream. If you have files with the HTML and the CSS, you could easily use a FileInputStream.

Once you have an InputStream for the HTML and the CSS, you can use this code:

// CSS
CSSResolver cssResolver = new StyleAttrCSSResolver();
CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
cssResolver.addCss(cssFile);

// HTML
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

// Pipelines
PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
p.parse(new ByteArrayInputStream(sb.toString().getBytes()));

Or, if you don't like all that code:

ByteArrayInputStream bis = new ByteArrayInputStream(htmlSource.toString().getBytes());  
ByteArrayInputStream cis = new ByteArrayInputStream(cssSource.toString().getBytes());
XMLWorkerHelper.getInstance().parseXHtml(writer, document, bis, cis);  


回答3:

You may try this code reference.

$html .= '<style>'.file_get_contents(_BASE_PATH.'stylesheet.css').'</style>';


回答4:

Change content type to

response.setContentType("application/pdf");


标签: java pdf itext