HTML 텍스트에서 pdf를 생성하기 위해 itext-5.3.4.jar 및 xmlworker-1.2.1.jar 라이브러리를 사용하고 있습니다. html에는 이미지 로고와 인라인 CSS가 포함되어 있습니다.HTML 텍스트 문자열에서 pdf를 생성하고 itext를 사용하여 안드로이드 용 CSS를 생성합니다.
내 HTML 파일 및 이미지가 Asset 폴더에 있습니다.이 라이브러리를 사용하면 성공적으로 생성되지만 이미지 표시가없고 CSS 스타일이 없습니다. 누구든지이 문제를 해결하기 위해해야 할 제안이 있습니다. 다른 라이브러리 또는 html에서 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"입니다 : PDF 파일을 생성하기위한
나는이 기능을 사용하고 있습니다 자산 폴더에 있습니다.
왜 안드로이드 포트 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 이후에이 오류가 발생합니다 –