我使用的是從html字符串生成pdf的itext-5.3.4.jar和xmlworker-1.2.1.jar庫,html包含圖像標誌和內聯css。使用itext從Html字符串與圖像和css生成PDF的Android
我的html文件和圖像是在資產文件夾中,使用這個庫我的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」被在資產文件夾中。
爲什麼不使用Android端口iTextG?爲什麼不使用當前版本5.5.9? –
java.lang。NoSuchMethodError:無虛方法addCell(Lcom/itextpdf/text/pdf/PdfPCell;)類Lcom/itextpdf/text/pdf/PdfPTable中的V;或它的超類('com.itextpdf.text.pdf.PdfPTable'聲明出現在/data/app/com.test.pdfgenerate-1/base.apk) –
它會在更新後給我這個錯誤itext 5.5.9 –