iText – HTML to PDF - Image is not displayed in PDF

前端 未结 5 2002
感动是毒
感动是毒 2020-12-31 02:03

I have a html page with text, image and I am parsing the HTML content to iText to generate the PDF. In the generated PDF,Included images are not getting displayed and , only

5条回答
  •  青春惊慌失措
    2020-12-31 02:46

    to show image with Itext, you have to Changing the default configuration about Image Provider Like it : i do it from http://demo.itextsupport.com/xmlworker/itextdoc/flatsite.html

    public class HtmlToPDF1 {
      public static void main(String ... args ) throws DocumentException, IOException {    
    
          FontFactory.registerDirectories();
          Document document = new Document();
          PdfWriter writer = PdfWriter.getInstance(document,
              new FileOutputStream("src/test/ressources/mypdf.pdf"));
          document.open(); HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
          htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
          htmlContext.setImageProvider(new AbstractImageProvider() {
              public String getImageRootPath() {
                  return "/home/fallphenix/workspace/JAVA/JEE/testHTMLtoPDF/src/test/ressources/";
              }
          }); CSSResolver cssResolver =
                XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
            Pipeline pipeline =
                new CssResolverPipeline(cssResolver,
                        new HtmlPipeline(htmlContext,
                            new PdfWriterPipeline(document, writer)));
            XMLWorker worker = new XMLWorker(pipeline, true);
            XMLParser p = new XMLParser(worker);
            p.parse(new FileInputStream("src/test/ressources/other.html"));
            document.close();
              System.out.println("Done.");        
        }}
    

提交回复
热议问题