javaitextflying-saucer

HTML to PDF conversion, Base64 image tag is not displaying image in PDF


Trying to convert html content to pdf in java using dependencies core renderer.jar, itext-2.0.8.jar and itextpdf-5.5.13.1.jar.I'm not getting image content but other contents of HTML can able to view in PDF.

The html file contains an image tag with base64 encoded

<img style = "width = 10px; height = 10px;" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARMAAAC3CAMAAAAGjUrGAAAAk1BMV...g==" />

creating pdf using java function which accepts the html content in string buffer as input and creates the PDF file.

public void createReport(StringBuffer sb) {
        try {
            
              String tempfile = outputPath + File.separator + "outputPdf.pdf";

              PrintWriter writer = new PrintWriter(inputPath, StandardCharsets.UTF_8);
              writer.println(sb.toString());
              writer.close();
            
              String url = new File(inputPath).toURI().toURL().toString(); 
              OutputStream os = new FileOutputStream(new File(tempfile)); 
              ITextRenderer renderer = new ITextRenderer(); 
              ITextFontResolver resolver = renderer.getFontResolver();
              resolver.addFont("C:\\Users\\DELL\\AppData\\Local\\Microsoft\\Windows\\Fonts\\ANGSA.ttf",BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 
              renderer.setDocument(url); 
              renderer.layout(); 
              renderer.createPDF(os);
              os.close();
             

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Solution

  • used dependencies:

    enter image description here

    and refer the link https://knpcode.com/java-programs/convert-html-to-pdf-in-java-using-flying-saucer-openpdf/