Android ItextG 图像标签未呈现
Android ItextG Image tag is not rendering
我正在使用 iTextPdf(Android 的 iTextG)库将 Html 转换为我的 android 应用程序的 PDF 文档。除了收据上的徽标外,一切对我来说都很好。我的 html 包含 <img>
标签和图片的来源 http url
<img src="http...."></img>
创建的 pdf 没有图像。相同的代码和 html 运行 在我的 Java 应用程序中显示带有创建的 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>
主要函数 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
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.
我正在使用 iTextPdf(Android 的 iTextG)库将 Html 转换为我的 android 应用程序的 PDF 文档。除了收据上的徽标外,一切对我来说都很好。我的 html 包含 <img>
标签和图片的来源 http url
<img src="http...."></img>
创建的 pdf 没有图像。相同的代码和 html 运行 在我的 Java 应用程序中显示带有创建的 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>
主要函数 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
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.