Android ItextG图片代码未呈现

时间:2017-12-20 19:31:52

标签: android itext itextg

我正在使用iTextPdf(iTextG for Android)库将Html转换为我的Android应用程序的PDF文档。除了收据上的标志外,一切都正常。我的html包含<img>标记,其中包含图片的源http网址

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

创建的pdf没有图像。在我的Java应用程序中运行的相同代码和html显示带有创建的PDF的徽标(这表明访问图像没有问题)。我想知道这个功能是否只与Java兼容,而不是与Android兼容? 我正在使用以下依赖项:

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

Html代码:

<?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>

主要活动中的功能:

 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();
    }
}

它只渲染<h1>标签并显示Fischerstube但在ANDROIRD DEVICE上没有图像。 任何人都可以在这方面帮助我,将不胜感激。

1 个答案:

答案 0 :(得分:0)

查看提供的文档here为我解决了。

  

确保您在清单中拥有互联网权限。

创建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方法将您的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方法。因为你将进行网络操作。