Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page break when HTML to PDF with AbcPdf

Tags:

html

pdf

abcpdf

I'm trying to create a report in PDF with abcPdf. Everything works but I would like to add a page number and margin at the bottom of each page as well as avoid cuts in the middle of a row as you can see in the picture:

page break

var theDoc = new Doc { TopDown = true };
var pageRef = theDoc.AddImageUrl(pdfUrl, true, 1903, true);
while (theDoc.Chainable(pageRef))
{
    theDoc.Page = theDoc.AddPage();
    //I guessI have to do something here???
    pageRef = theDoc.AddImageToChain(pageRef);
}

Does somebody know if it is possible?

like image 944
Remo H. Jansen Avatar asked Jul 02 '12 10:07

Remo H. Jansen


People also ask

How do I fix a page-break in PDF?

To insert a page break in pdf, the html code must have an element with the style page-break-before: always or page-break-after: always. Before or after that specific element, a page break will be inserted into the generated pdf document. The element can be anything (image, table, table row, div, text, etc).

How do I convert a page-break to a PDF in HTML?

You can insert page breaks before and after a HTML element in the generated PDF document by setting the 'page-break-before : always' and 'page-break-after : always' styles for that element in HTML. The Full Description and a Code Sample can be accessed from the top tabs.

How do I make a page-break dynamic in HTML?

page-break-before: auto instead of . page-break-before: always. The "auto" will break the page only if the contents are at the end if the page, this will prevent breaking the page and leaving a lot of blank space. Save this answer.


2 Answers

It did work but I think AbcPdf is using the HTML rendering of IE so the best thing you can do is to manually set the rendering engine to be gecko (Dont forget that you need and extra DLL) or to update IE in your web server.

theDoc.HtmlOptions.Engine = EngineType.Gecko; 

Then to add a page break just use

<div style="page-break-before:always">&nbsp;</div> 

Thanks to feeela for the comment.

like image 75
Remo H. Jansen Avatar answered Sep 23 '22 12:09

Remo H. Jansen


ABCpdf includes two HTML rendering engines.

The MSHTML one is based around Trident and will produce output broadly similar to the version of IE installed on your system.

The Gecko one is based around Firefox and as of June 2013 will produce output broadly similar to that you see in Firefox 21.

You can switch between the two engines using the Doc.HtmlOptions.Engine property.

Both engines support the page-break CSS styles. You can use the following:

<div style="page-break-before:always">some text</div>
<div style="page-break-after:always">some text</div> 
<div style="page-break-inside:avoid">some text</div> 

Note that the page-break-inside is an addition to the base MSHTML behavior.

The two engines treat these constructs slightly differently. In general MSHTML is more forgiving and intuitive. Howwever the element to which the style is applied must be visible for it to work.

As such, if the styles don't produce breaks in the places you would expect, the easiest way to debug them is to apply a border style to the element so you can see where the break should occur. This normally makes the cause of the problem obvious.

The page break styles in the Gecko engine are not always applied as intuitively as they are in MSHTML. The root of this is the CSS specification that which says that break styles must be appliable to block-level elements within the "normal flow of the root element". It allows for these styles to be applied to other elements but does not mandate it.

The upshot of this, within the Gecko engine, is that page break styles cannot be applied within tables, to elements such as table rows. If you are unsure about whether something is likely to work just try Print Preview from within Firefox 21.0 as a simple sanity check.

like image 36
OnceUponATimeInTheWest Avatar answered Sep 22 '22 12:09

OnceUponATimeInTheWest