while creating pdf from XHTML by using Flying Saucer/iText, want landscape in every pages ( am using this method ) but am getting extra blank page in initial and end pages, any ideas?
If you want lanscape on every page, don't use any selector on the @page
rule.
Just use:
@page {size: landscape;}
It will apply on the whole document, and won't add blank pages in the document.
If you want to use alternate portrait/landscape layouts in the same document (i.e. first page is portrait and the rest are landscape), you need to specify an actual width and height for each @page
rule that also has a selector, otherwise Flying Saucer will generate an extra white page (as per bug).
What worked for me:
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>
@page { size: a4 portrait; }
–
Hospice If you want lanscape on every page, don't use any selector on the @page
rule.
Just use:
@page {size: landscape;}
It will apply on the whole document, and won't add blank pages in the document.
For making pdf from .vm file I faced the same issue. In my case removing : width: 100%; height: 100%; from by body{} tag helped get rid of the extra page. Hopefully it might be useful for someone else too.
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);
}
© 2022 - 2024 — McMap. All rights reserved.