Extra blank page when converting HTML to PDF using abcPDF
Asked Answered
P

9

10

I have an HTML report, with each print page contained by a <div class="page">. The page class is defined as

width: 180mm;
height: 250mm;
page-break-after: always;
background-position: centre top;
background-image: url(Images/MainBanner.png);
background-repeat: no-repeat;
padding-top: 30mm;

After making a few changes to my report content, when I call abcPDF to convert the report to PDF, suddenly I'm getting a blank page inserted after every real report page. I don't want to roll back the changes I've just made to remove this problem, so I'm hoping someone may know why the extra pages are being inserted.

Paresh answered 4/3, 2010 at 13:23 Comment(0)
M
8

I have experienced the same exact problem. the empty page is due to the page-break-after: always; in the CSS. Not just ABCpdf but also the printed will spit out an extra page. So I used the following code to eliminate the last page: MyDoc.Delete(MyDoc.Page);

This however lead to a different kind of a problem. On development server, which has IE 8 I get an extra blank page and on production where I have IE6, I get no extra blank page. So I have emailed the support team at websupergoo to show me a way to look for a blank page. The idea is to iterate through a pdf and identify all blank pages and delete them using above logic.

And I second Jakkwylde's opinion. Websupergoo folks are extremely helpful and prompt in responding. I had another problem getting ABCpdf to work under 64 bit and had spent almost a day trying to figure it out. They provided me multiple scenarios which I could try out. Their support was right on the money and I got my app up and running in minutes.

Mariandi answered 15/3, 2010 at 23:24 Comment(1)
Had the same problem with different IE versions on different environments. I follow the same approach and it has worked well for me so far.Pisarik
P
4
protected void RemoveBlankPages(Doc pdf)
{
    for (int i = pdf.PageCount; i > 0; i--)
    {
        pdf.PageNumber = i;

        //get the pdf content
        string textContent = pdf.GetText("Text");

        //delete the page if it is blank
        if (string.IsNullOrEmpty(textContent))
            pdf.Delete(pdf.Page);
    }
}
Pisarik answered 7/9, 2012 at 18:46 Comment(1)
This will not work if there are only images on one page.Bradney
W
2

I've found abcPDF to be strange and unpredictable. That being said, what may be happening is that the combination of the page size and page-break-after may be the culprit. Reduce your page height and/or remove the page break.

Walcott answered 4/3, 2010 at 14:20 Comment(0)
S
2

One thing worth revisiting is the validity of your HTML markup if you are using the AddImageUrl method. Instances where the rendered PDF is not as expected can result from bad markup, busted tags, etc.

For what it's worth, WebSuperGoo has excellent support and respond great when you encounter anomalies. Often they can advise a work around or provide alternatives to your implementation if you send them your source code.

Starwort answered 11/3, 2010 at 3:18 Comment(0)
R
0

Kush is correct in that "I have experienced the same exact problem. the empty page is due to the page-break-after: always; in the CSS. Not just ABCpdf but also the printed will spit out an extra page."

If a div has "page-break-after:always" IE will literally always start a new page, and if nothing is added it will just print blank. Firefox does not.

abcpdf uses IE8s rendering engine, and as such makes a blank page. For purposes of the OP, just using an explicit height should solve the problem, and the engine will insert the page breaks for you.

I am trying to solve a similar issue, where I can't set the height explicitly because sometimes the content may take 2 pages. (Each page corresponds to a person, and each person should start on a new page when printed). I emailed abcpdf as well to see if they have a hack fix to detect the empty page, but was curious if anyone knows how to fix the underlying problem and css hack IE8 so as to make it not print the final page if empty. I'm guessing it's not possible, but wanted to make sure I'm not missing something obvious.

Redtop answered 30/4, 2010 at 19:38 Comment(0)
H
0

The AddImageURL() method of ABCPDF is loosely bind method which doesn't render html tightly within required area which causes new blank page.

try to use AddImageHTML() method to convert your desired HTML into PDF..

Doc theDoc = new Doc();
theDoc.Page = theDoc.AddPage();

int theID = 0;
theDoc.SetInfo(0, "CheckBgImages", "1");
theDoc.SetInfo(0, "RenderDelay", "5000");
theDoc.HtmlOptions.Engine = EngineType.MSHtml;
theID = theDoc.AddImageHtml(HTML);

while (true)
{
 if (!theDoc.Chainable(theID))
                break;
            theDoc.Page = theDoc.AddPage();
            theID = theDoc.AddImageToChain(theID);
        }

        for (int i = 0; i <= theDoc.PageCount; i++)
        {
            theDoc.PageNumber = i;
            theDoc.Flatten();
        }

        theDoc.Save(HttpContext.Current.Server.MapPath(Path));
        theDoc.Clear();

It will always give accurate results.

Handsome answered 16/7, 2012 at 11:9 Comment(0)
T
0

To avoid page breaking in the last page, I did something like this and it worked.

I made sure that the last page didn't have the page-break-after: always, this can be done with any templating or front-end framework like angularJS, but for this example I use blade templating (but any php will do...)

@if ($last_page)
   <div class='footer last-page'>
@else
   <div class='footer'>

and then I have this in my style sheet

.footer {
    page-break-after:always;
}

.last-page {
    page-break-after:avoid;
}
Titter answered 6/1, 2015 at 15:25 Comment(0)
C
0

We had the same issue in production environment only but not in test environment. We only had page-brek-after used at multiple places in the html.

Fix for first issue: I spotted the issue by removing the page-brek-after attributes one by one and this finally gave me the DIV section where page break was causing by some of it's element.

I fixed the height of each elements inside the DIV and this finally fixed my issue without removing the page-break-after attribute.

Fix for similar issue: If you have a custom hard coded footer, make sure to check by increasing/decresing it's height and margine.

Curzon answered 10/12, 2018 at 19:21 Comment(0)
D
0

I had this same thing happen with html to pdf where abcpdf was adding a blank page with nothing but a footer on it before the rest of the content (as page 1). This would occur when my content included a table surrounded by a div where the div height was height:auto followed by a page-break-before: always. It only happened when the table data contained "", string.Empty, or a single line of text. If the table data contained 2 lines or more of text, the issue did not present.

I solved it by adding a min-hight: 1in style to the div that had height: auto.

Darwin answered 22/6, 2021 at 15:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.