在使用 Flying Saucer/iText 从 XHTML 创建 pdf 时,希望每个页面都有风景(正在使用这种方法),但是在初始页面和结束页面中获得了额外的空白页面,有什么想法吗?
问问题
2509 次
4 回答
4
如果你想在同一个文档中使用交替的纵向/横向布局(即第一页是纵向的,其余的都是横向的),你需要为每个@page
也有选择器的规则指定一个实际的宽度和高度,否则飞碟会生成一个额外的白页(根据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>
于 2014-09-23T13:50:44.093 回答
1
于 2013-07-11T08:21:26.787 回答
0
为了从 .vm 文件制作 pdf,我遇到了同样的问题。在我的情况下删除:宽度:100%;高度:100%;from by body{} 标签有助于摆脱多余的页面。希望它对其他人也可能有用。
于 2016-04-12T07:23:10.940 回答
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);
}
于 2017-02-03T07:42:39.977 回答