Android tag ItextG Image not rendering

I am working with iTextPdf (iTextG for Android) to convert an Html document to PDF for my Android application. Everything works fine for me, except for the logo on the receipt. My html contains a tag <img>with the source url for the image

<img src="http...."></img>

created pdf has no image. The same code and html running in my Java application show the logo with the created PDF (this shows that there is no problem accessing the image). I am wondering if this feature is compatible with Java, but not with Android? I use the following dependencies:

compile 'com.itextpdf:itextg:5.5.10'
compile 'com.itextpdf.tool:xmlworker:5.5.10'

HTML code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="English">
<head>
    <title>Title</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>

<body>
<img src="https://image.flaticon.com/teams/slug/google.jpg"></img>
<h1>Fischerstube</h1>
</body>
</html>

Function in core business:

 private void htmlToPdf(String html) throws DocumentException, IOException {

    try {

        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "logo.pdf");
        OutputStream fileOutputStream = new FileOutputStream(file);
        Document document = new Document();
        document.setPageSize(new Rectangle(201,720));
        PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream);
        document.open();
        InputStream is = new ByteArrayInputStream(html.getBytes());
        XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
        document.close();
        fileOutputStream.close();

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

The only rendering tag <h1>and shows Fischerstube, but without an image on ANDROIRD DEVICE. Can anyone help me in this regard, we will be grateful.

+4
1

, .

, .

Base64ImageProvider

class Base64ImageProvider extends AbstractImageProvider {

    @Override
    public Image retrieve(String src) {
        int pos = src.indexOf("base64,");
        try {
            if (src.startsWith("data") && pos > 0) {
                byte[] img = Base64.decode(src.substring(pos + 7));
                return Image.getInstance(img);
            }
            else {
                return Image.getInstance(src);
            }
        } catch (BadElementException ex) {
            return null;
        } catch (IOException ex) {
            return null;
        }
    }

    @Override
    public String getImageRootPath() {
        return null;
    }
}

create pdf- yout HTML pdf

public void createPdf() throws IOException, DocumentException {
    String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" +
            "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"English\">\n" +
            "<head>\n" +
            "    <title>Title</title>\n" +
            "    <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/>\n" +
            "</head>\n" +
            "\n" +
            "<body>\n" +
            "<img src=\"https://image.flaticon.com/teams/slug/google.jpg\"></img>\n" +
            "<h1>Fischerstube</h1>\n" +
            "</body>\n" +
            "</html>";


    // step 1
    File file = new File(Environment.getExternalStorageDirectory() + File.separator + "logo.pdf");
    OutputStream fileOutputStream = new FileOutputStream(file);
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream);
    // step 3
    document.open();
    // step 4

    // CSS
    CSSResolver cssResolver =
            XMLWorkerHelper.getInstance().getDefaultCssResolver(true);

    // HTML
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
    htmlContext.setImageProvider(new Base64ImageProvider());

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

    // XML Worker
    XMLWorker worker = new XMLWorker(css, true);
    XMLParser p = new XMLParser(worker);
    p.parse(new ByteArrayInputStream(str.getBytes()));

    // step 5
    document.close();
}

, createPdf . .

0

Source: https://habr.com/ru/post/1691136/


All Articles