I am trying to convert html to pdf, while doing so I am not able to see the images in the pdf. When I use a simple html file , then the image is shown in the pdf but in case of having html as string, the image is not coming. I am using following code:
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();
}
When instead of above I use
public static final String HTML = "C://temp/sample_final7.html";
and have the tag "img src="C://Temp/areaofFocus.png" height=35 width=158 alt="Programs"/>
in my html, then the image is displayed.