getting extra blank pdf page in Flying Saucer/iText
Asked Answered
C

4

5

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?

Chine answered 10/7, 2013 at 17:46 Comment(2)
Show some code (both the XHTML file and the processing code).Indubitability
I have a similar problem, when (I think) I add in the html content an image that is too height for the page (A4). The first page is blank and from the second page it start todivide the image in more than one page (that could be good but I would like to remove the first blank page)... And I cant find a way to set a page height based on html content (I think is not possilbe with Itext5). Any idea?Otolith
M
2

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.

Meghan answered 11/7, 2013 at 8:21 Comment(0)
P
5

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>
Preemption answered 23/9, 2014 at 13:50 Comment(3)
That works for me. thanks for saving my hours. I just need add this bit @page { size: a4 portrait; } Hospice
Be careful with the actual widths and heights. For example if the height of the landscape section in this example is too high, you could end up with an extra blank page at the bottom. I ended up using height: 14cm; width: 25.3cmFranfranc
@Shivan Dragon How can I avoid a blank page, I want the whole page to is portrait, and the first and second is landscape?Walke
M
2

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.

Meghan answered 11/7, 2013 at 8:21 Comment(0)
H
0

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.

Hoxie answered 12/4, 2016 at 7:23 Comment(0)
B
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);
    }
Blankenship answered 3/2, 2017 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.