有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

未显示java iText HTML到PDF的转换图像

我正在尝试将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中,然后显示图像


共 (0) 个答案