1

I am trying to convert HTML to PDF using itextpdf 5.5.6. And the images like next are not exported.

<img alt="" src="http://localhost:8080/images/logo.jpg" style="height:53px; width:161px" /> 

Here is the source code I'm using.

        InputStream is = new ByteArrayInputStream(bytes);
        InputStream resourceInputStream = servletContext.getResourceAsStream("/css/doc_template.css");
        if (resourceInputStream != null){
            XMLWorkerHelper.getInstance().parseXHtml(writer, document, is, resourceInputStream, Charset.forName("UTF-8"));
        } else {
            throw new Exception("Not available resource:" + servletContext.getResource("/css/doc_template.css").getPath());
        }
iviorel
  • 312
  • 3
  • 10
  • Please take a look at this question and answer: https://stackoverflow.com/questions/47895935 – Bruno Lowagie Dec 19 '17 at 23:28
  • You question and answer are better than these ones. I did not found it when I tried to fix my issue. That's why I posted a new question. – iviorel Dec 20 '17 at 10:13

1 Answers1

3

Fixed. I had to extend AbstractImageProvider like here https://developers.itextpdf.com/examples/xml-worker-itext5/html-images

Here is the method:

private void createAndSavePdfWithImages(String dest, String content) throws Exception {
        OutputStream file = new FileOutputStream(new File(dest));
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, file);
        document.open();

        byte[] bytes = content.getBytes( Charset.forName("UTF-8"));
        InputStream is = new ByteArrayInputStream(bytes);

        CssFilesImpl cssFiles = new CssFilesImpl();
        InputStream inCssFile = servletContext.getResourceAsStream("/css/doc_template.css");
        CSSResolver cssResolver = null;
        if(inCssFile != null) {
            cssFiles.add(XMLWorkerHelper.getInstance().getCSS(inCssFile));
            cssResolver = new StyleAttrCSSResolver(cssFiles);
        } else {
            cssResolver  = XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
        }

        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
        htmlContext.setImageProvider(new ImageProvider());

        PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);
        p.parse(is, Charset.forName("UTF-8"));

        document.close();
    }

    class ImageProvider extends AbstractImageProvider {

        @Override
        public Image retrieve(String src) {
            try {
                String path = servletContext.getRealPath(src);
                Image img = com.itextpdf.text.Image.getInstance(path);
                return Image.getInstance(img);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public String getImageRootPath() {
            return null;
        }
    }
iviorel
  • 312
  • 3
  • 10