【发布时间】:2016-10-30 12:03:27
【问题描述】:
我正在使用 itext-5.3.4.jar 和 xmlworker-1.2.1.jar 库从 html 字符串生成 pdf,html 包含图像徽标和内联 css。
我的 html 文件和图像位于 Asset 文件夹中,使用此库成功生成了我的 pdf,但没有图像显示和 css 样式排序。如果任何其他库或任何其他从 html 生成 pdf 的选项会有所帮助,任何人都有建议我应该怎么做来解决这个问题。
我正在使用这些函数来生成 pdf:
public static void generatePdfFromHtlm(String fileName, String htmlString){
try {
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), AppConfig.getContext().getPackageName() + "/" + "Pdf");
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
File fileWithinMyDir = new File(mediaStorageDir, fileName);
FontFactory.registerDirectories();
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(fileWithinMyDir));
document.open();
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
htmlContext.setImageProvider(new AbstractImageProvider() {
public String getImageRootPath() {
Uri uri = Uri.parse("file:///android_asset/");
String newPath = uri.toString();
return newPath;
}
});
CSSResolver cssResolver =
XMLWorkerHelper.getInstance().getDefaultCssResolver(false);
/*Pipeline<?> pipeline =
new CssResolverPipeline(cssResolver,
new HtmlPipeline(htmlContext,
new PdfWriterPipeline(document, writer)));*/
// Pipelines
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);
InputStream is = new ByteArrayInputStream(htmlString.getBytes());
p.parse(is);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
我的 html 文件是
<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8"> <![endif]-->
<!--[if IE 9]> <html lang="en" class="ie9"> <![endif]-->
<!--[if !IE]><!-->
<html lang="en"> <!--<![endif]-->
<!-- BEGIN HEAD -->
<head>
<meta charset="utf-8" />
<title>WeighInsheet</title>
<style type="text/css">
body { font-family:Arial; }
.Wrapper { border:1px solid #cccccc; height:auto; margin:0px 65px;}
.header { height:105px; margin:5px; float:left;}
.logo { width:100px; height:100px; float:left; }
.heading { width:600px;}
h2{ margin:40px 0px 0px 0px;font-size: 22px; font-weight:bold;}
h3{ margin:0px; font-weight:normal;font-size: 16px;}
table { border-collapse: collapse; border-spacing: 0; width:100%; }
table td { font-family: arial; font-size: 14px; padding: 10px 5px; border: 1px solid #ddd; }
table th { background-color:#000000; color:#ffffff; font-family: arial; font-size: 16px; font-weight: bold;
border: 1px solid #ddd; }
</style>
</head>
<!-- END HEAD -->
<!-- BEGIN BODY -->
<body>
<div class="Wrapper">
<div class="header">
<div class="logo"><img src="logo.jpg" class="logo"/></div>
<div class="heading" align="center">
<h2>Description</h2>
<h3>Title Meta</h3>
</div>
</div>
<table>
<thead>
##CHANGEHEADER##
</thead>
<tbody>
##CHANGEBODY##
</tbody>
</table>
</div>
</body>
<!-- END BODY -->
</html>
我的图片文件“logo.jpg”在资产文件夹中。
【问题讨论】:
-
你为什么不用安卓端口,iTextG?为什么不使用当前版本 5.5.9?
-
java.lang.NoSuchMethodError:Lcom/itextpdf/text/pdf/PdfPTable 类中没有虚拟方法 addCell(Lcom/itextpdf/text/pdf/PdfPCell;)V;或其超类(“com.itextpdf.text.pdf.PdfPTable”的声明出现在 /data/app/com.test.pdfgenerate-1/base.apk 中)
-
更新 itext 5.5.9 后会出现此错误
-
它为我工作的好问题
标签: android html css pdf-generation itext