gpt4 book ai didi

Android ItextG 图像标签未呈现

转载 作者:太空宇宙 更新时间:2023-11-03 10:59:28 25 4
gpt4 key购买 nike

我正在使用 iTextPdf(Android 版 iTextG)库将 Html 转换为 PDF 文档以供我的 Android 应用程序使用。除了收据上的 Logo 外,一切对我来说都很好。我的 html 包含 <img>使用图像的源 http url 标记

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

创建的 pdf 没有图像。在我的 Java 应用程序中运行的相同代码和 html 显示带有创建的 PDF 的 Logo (这表明访问图像没有问题)。我想知道这个功能是否只兼容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>

主要 Activity 中的功能:

 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 设备上没有图像。任何人都可以在这方面帮助我,将不胜感激。

最佳答案

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

make sure you have Internet permission in the manifest.

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

Make sure you execute createPdf method on background thread. since you will be performing network operation.

关于Android ItextG 图像标签未呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47913159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com