HTMLの表をpdfに変換し、「無限のテーブルループを取得するためにItextライブラリを使用しました;行の内容がページよりも大きい画像の縮尺を調整しなかったため)」というエラーが表示されます。以下の私のコードを見つけてください。無限テーブルループ。 (例えば、画像を拡大/縮小しなかったため)
public static File createPDF(File outputDir, Context context) {
Document document = null;
String htmlStr = "<table><tr><td><div class="two-col-pair-one"><div align="center">1: 9_ITEM</div><div class="row-odd">
<div class="packet-font">
<div class="two-col">Cust. Name</div>
<div class="two-col">Gg</div>
</div>
<div style="clear:both"></div>
</div>
<div class="row-even">
<div class="packet-font">
<div class="two-col">Org. City</div>
<div class="two-col">D</div>
</div>
<div style="clear:both"></div>
</div>
<div class="row-odd">
<div class="packet-font">
<div class="two-col">Dest. City</div>
<div class="two-col">D</div>
</div>
<div style="clear:both"></div>
</div>
<div class="row-even">
<div class="packet-font">
<div class="two-col">Product Type</div>
<div class="two-col">Domestic Moving</div>
</div>
<div style="clear:both"></div>
</div>
<div class="row-odd">
<div class="packet-font">
<div class="two-col">Trans.Mode</div>
<div class="two-col">Road</div>
</div>
<div style="clear:both"></div>
</div>
<div class="row-even">
<div class="packet-font">
<div class="two-col">Description</div>
<div class="two-col">Fridge - Regular</div>
</div>
<div style="clear:both"></div>
</div>
</div>
<div class="two-col-pair-two"></div>
</td></tr></table>";
File pdfFile = null;
String CSS = body
{
font-family: Verdana;
font-size: 13px;
font-weight: bold;
}
@page {
size: A4;
}
@page {
margin: 0px;
}
@page :left {
margin: 0px;
}
@page :right {
margin: 0px;
}
@page :right {
margin: 0px;
}
.page-break{
page-break-after: always;
}
table{
border-collapse: collapse;
}
table, td, th{
border: 3px dotted black;
width: auto
}
td{
height: 5.75cm;
width: 5.1cm;
}
tr{
page-break-inside:avoid;
page-break-after:auto;
}
.two-col{
position:relative;
float: left;
width: 47%;
}
.two-col-pair-one{
float: left;
width: 100%;
padding:5px;
}
.two-col-pair-one-qr{
float: left;
width: 100%;
padding:5px;
}
.two-col-pair-two{
float: left;
width: 0%;
}
.two-col-pair-two-qr{
float: left;
width: 100%;
}
.row, .row-odd, .row-even, .row-default{
width: 100%;
display:block;
}
.container{
display: inline-block;
vertical-align: middle;
width: 99%;
max-height:100%
margin-left: auto;
margin-right: auto;
}
@media(max-width:800px){
.thumbnails img {
height: 50px;
border: 1px solid #555;
margin: 6px 6px 0 0;
}
}
@media(max-width:480px){
.thumbnails img {
height: 30px;
border: 1px solid #555;
margin: 4px 4px 0 0;
}
}
@media(orientation:landscape){
.container{
width:90%;
}
.thumbnails{
position: fixed;
top: 0;
right:0;
height: 100%;
}
};
try {
pdfFile = new File(outputDir.getAbsolutePath() + File.separator + String.format(Locale.ENGLISH, "%s_%s.pdf", "Packet_labels", "_" + job.getId()));
pdfFile.getParentFile().mkdirs();
document = new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
document.open();
PdfPTable table = getTable(CSS, htmlStr);
document.add(table);
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
Log.d("PDF Exception", "" + e);
e.printStackTrace();
}
return pdfFile;
}
public static PdfPTable getTable(String CSS, String table) throws IOException {
CSSResolver cssResolver = new StyleAttrCSSResolver();
CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
cssResolver.addCss(cssFile);
// HTML
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
htmlContext.setImageProvider(new Base64ImageProvider());
// Pipelines
ElementList elements = new ElementList();
ElementHandlerPipeline pdf = new ElementHandlerPipeline(elements, null);
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(table.getBytes()));
return (PdfPTable)elements.get(0);
}
ありがとうございます。
私はここで正確に質問をしません。 – mkl
@mkl質問はなぜ私がこのエラーを取得して、これを解決する方法ですか? – Prashant