使用Flying Saucer / iText从XHTML创建pdf时,想要在每个页面中使用横向(使用this method) 但是我在初始页和结束页中得到了额外的空白页,有什么想法吗?
答案 0 :(得分:2)
如果要在同一文档中使用备用纵向/横向布局(即第一页是纵向,其余为横向),则需要为每个@page
规则指定实际宽度和高度选择器,否则Flying Saucer将生成一个额外的白页(根据bug)。
对我有用的是什么:
CSS:
@page {
size: a4 portrait;
}
@page land { size: a4 landscape;}
.landscapePage { page:land; height: 21cm; width: 28.6cm}
HTML:
<div>
Portrait content here
</div>
<span class="pageBreak"/>
<div class="landscapePage">
landscape content page 1
<span class="pageBreak"/>
landscape content page 2
</div>
答案 1 :(得分:1)
答案 2 :(得分:0)
从.vm文件制作pdf我遇到了同样的问题。在我的情况下删除: 宽度:100%; 身高:100%; 来自body {}标签帮助摆脱了额外的页面。希望它也可能对其他人有用。
答案 3 :(得分:0)
public class HeaderFooterPageEvent extends PdfPageEventHelper {
private PdfTemplate t;
private Image total;
@Override
public void onOpenDocument(PdfWriter writer, Document document) {
super.onOpenDocument(writer, document);
System.out.println("On Open");
t = writer.getDirectContent().createTemplate(30, 16);
try {
total = Image.getInstance(t);
// total.setRole(new PdfName("Artifact"));
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
// addHeader(writer);
addFooter(writer);
}
private void addFooter(PdfWriter writer){
System.out.println("addFooter Called***************");
PdfPTable footer = new PdfPTable(3);
try {
// set defaults
footer.setWidths(new int[]{24, 2, 1});
footer.setTotalWidth(527);
footer.setLockedWidth(true);
footer.getDefaultCell().setFixedHeight(40);
footer.getDefaultCell().setBorder(Rectangle.TOP);
footer.getDefaultCell().setBorderColor(Color.LIGHT_GRAY);
// add copyright
footer.addCell(new Phrase("", new Font(Font.HELVETICA, 12, Font.BOLD)));
footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
footer.addCell(new Phrase(String.format("Page %d of", writer.getPageNumber()), new Font(Font.HELVETICA, 8)));
PdfPCell totalPageCount = new PdfPCell(total);
totalPageCount.setBorder(Rectangle.TOP);
totalPageCount.setBorderColor(Color.LIGHT_GRAY);
System.out.println("TOTAL PageCount: "+totalPageCount);
footer.addCell(totalPageCount);
// write page
PdfContentByte canvas = writer.getDirectContent();
canvas.beginText();
footer.writeSelectedRows(0,-1, 34, 50, canvas);
canvas.endText();
} catch(DocumentException de) {
throw new ExceptionConverter(de);
}
}
@Override
public void onCloseDocument(PdfWriter writer, Document document) {
System.out.println("On Close");
int totalLength = String.valueOf(writer.getPageNumber()).length();
int totalWidth = totalLength * 5;
ColumnText.showTextAligned(t, Element.ALIGN_RIGHT,
new Phrase(String.valueOf(writer.getPageNumber()-1), new Font(Font.HELVETICA, 8)),
totalWidth, 6, 0);
}