我正在尝试使用itextpdf 5.5.6将HTML转换为PDF。 并且不会导出下一个图像。
<img alt="" src="http://localhost:8080/images/logo.jpg" style="height:53px; width:161px" />
这是我正在使用的源代码。
InputStream is = new ByteArrayInputStream(bytes);
InputStream resourceInputStream = servletContext.getResourceAsStream("/css/doc_template.css");
if (resourceInputStream != null){
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is, resourceInputStream, Charset.forName("UTF-8"));
} else {
throw new Exception("Not available resource:" + servletContext.getResource("/css/doc_template.css").getPath());
}
答案 0 :(得分:1)
固定。我不得不像这里https://developers.itextpdf.com/examples/xml-worker-itext5/html-images
那样扩展AbstractImageProvider以下是方法:
private void createAndSavePdfWithImages(String dest, String content) throws Exception {
OutputStream file = new FileOutputStream(new File(dest));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
byte[] bytes = content.getBytes( Charset.forName("UTF-8"));
InputStream is = new ByteArrayInputStream(bytes);
CssFilesImpl cssFiles = new CssFilesImpl();
InputStream inCssFile = servletContext.getResourceAsStream("/css/doc_template.css");
CSSResolver cssResolver = null;
if(inCssFile != null) {
cssFiles.add(XMLWorkerHelper.getInstance().getCSS(inCssFile));
cssResolver = new StyleAttrCSSResolver(cssFiles);
} else {
cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
}
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
htmlContext.setImageProvider(new ImageProvider());
PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
p.parse(is, Charset.forName("UTF-8"));
document.close();
}
class ImageProvider extends AbstractImageProvider {
@Override
public Image retrieve(String src) {
try {
String path = servletContext.getRealPath(src);
Image img = com.itextpdf.text.Image.getInstance(path);
return Image.getInstance(img);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public String getImageRootPath() {
return null;
}
}