【发布时间】:2018-04-18 07:22:26
【问题描述】:
public PdfPCell richTextRendering(PdfPCell pdfpCell, String HTML) throws DocumentException, IOException {
final String CSS = "";
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
CSSResolver cssResolver = new StyleAttrCSSResolver();
CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
cssResolver.addCss(cssFile);
ElementList elements=new ElementList();
ElementHandlerPipeline pdf = new ElementHandlerPipeline(elements, null);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
XMLWorker worker = new XMLWorker(css, false);
XMLParser p = new XMLParser(worker);
p.parse(new ByteArrayInputStream(HTML.getBytes()));
for (Element element : elements) {
pdfpCell.addElement(element);
}
return pdfpCell;
}
proposalDescriptionTableCell.setBorder(Rectangle.NO_BORDER);
String proposalDescriptionEnglish=proposalInformation.getProposalDescriptionEnglish().trim();
proposalDescriptionEnglish=proposalDescriptionEnglish.replaceAll("div", "p"); //XMLWorkerHelper not taking rendering <div> content its recommended to replace all the <div> with <p>
proposalDescriptionEnglish=proposalDescriptionEnglish.replaceAll("<br>", "<br/>");
proposalDescriptionEnglish="<p>"+proposalDescriptionEnglish+"</p>"; //some tags are not supported the plug-in keep the code inside the <p> for safe execution
proposalDescriptionTableCell=new CustomerViewPdfDelegationServiceImpl().richTextRendering(proposalDescriptionTableCell,proposalDescriptionEnglish); //getting the pdfPCell with HtmlRendered-Text
对于我的代码,除了标题标签之外,所有其他标签都可以工作
【问题讨论】:
标签: java xmlworkerhelper