我正在尝试将html转换为pdf,而这样做我无法在pdf中看到图像。 当我使用一个简单的html文件时,图像显示在pdf中,但是如果将html作为字符串,则图像不会出现。 我使用以下代码:
public static final String DEST = "C://temp/rounded_corners.pdf";
public static final String HTML = "<html><head><title>Summary log</title></head><body><h1 style='margin-top:-5px'>Trading & Liqidity</h1><p style='margin-bottom:-3px'><img src='areaOfFocus.PNG' height=35 width=235 alt='Area of Focus'/></p></body>";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new SamplePDFGEneration().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document(PageSize.A4.rotate());
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
htmlContext.setImageProvider(new AbstractImageProvider() {
public String getImageRootPath() { return "/samplePDF/src/samplePDF/"; }
});
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new StringReader(HTML));
document.close();
}
而不是上面我使用
public static final String HTML = "C://temp/sample_final7.html";
并标记为"img src="C://Temp/areaofFocus.png" height=35 width=158 alt="Programs"/>
在我的HTML中,然后显示图像。