7

I have this C# in my Program.cs:

    var page = "plain";

    var slnpath = $@"{Directory.GetCurrentDirectory()}\..\..\..\..";
    var htmlpath = $@"{slnpath}\HtmlTemplates\{page}.html";
    var pdfpath = $@"{slnpath}\PdfFiles\{page}.pdf";
    var dllpath = $@"{slnpath}\DinkNative64bit\libwkhtmltox.dll";

    var html = new StringBuilder(File.ReadAllText(htmlpath));

    var _converter = new SynchronizedConverter(new PdfTools());

    var context = new CustomAssemblyLoadContext().LoadUnmanagedLibrary(dllpath);

    var globalSettings = new GlobalSettings
    {
        ColorMode = ColorMode.Color,
        Orientation = Orientation.Portrait,
        PaperSize = PaperKind.A4,
        Margins = new MarginSettings { Top = 10 },
        DocumentTitle = "PDF Report",
        //Out = @"D:\PDFCreator\Employee_Report.pdf"  USE THIS PROPERTY TO SAVE PDF TO A PROVIDED LOCATION
    };

    var objectSettings = new ObjectSettings
    {
        PagesCount = true,
        HtmlContent = html.ToString(),
        //Page = "https://code-maze.com/", USE THIS PROPERTY TO GENERATE PDF CONTENT FROM AN HTML PAGE
        WebSettings = { DefaultEncoding = "utf-8" }, //, UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "styles.css") },
        HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true },
        FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Center = "Report Footer" },

    };

    var pdf = new HtmlToPdfDocument()
    {
        GlobalSettings = globalSettings,
        Objects = { objectSettings }
    };

    //_converter.Convert(pdf); IF WE USE Out PROPERTY IN THE GlobalSettings CLASS, THIS IS ENOUGH FOR CONVERSION

    var file = _converter.Convert(pdf);

    File.WriteAllBytes(pdfpath, file);

And I have this HTML file I have (too big to paste here).

The generated PDF is mostly fine, but on Page 3 the page break is not correct. The larger content buts up against the previous content - I assume because it will not fit into the following page.

How can every DIV with the page class be made to begin on a new page?

Matt W
  • 11,753
  • 25
  • 118
  • 215

5 Answers5

12

If you want to add a page break after every page, add this in your page class:

page-break-after: always;
Insano
  • 156
  • 1
  • 5
4

For those who want to prevent page break in the middle of the table like example below: Page break in the middle of the table

Just use css page-break-inside with avoid value:

table {
    page-break-inside: avoid;
}
bozydarlelutko
  • 511
  • 7
  • 21
1

I have used "page-break-after: always;" inside div and worked perfectly for me using DinkToPdf

 var sb = new StringBuilder();
 sb.AppendFormat("<div style='page-break-after: always;'></div>");
1

If you need a normal page break into html you can try these two ways.

  1. put this css on html page page-break-after: always;
  2. Or add this div to that place <div style="display:block; clear:both; page-break-after:auto;"></div>

If you have long tables in your html then to avoid overlappings with table header after page break use the following css.

thead { display: table-header-group }
tfoot { display: table-row-group }
tr { page-break-inside: avoid }

Happy Coding

Rajan Mishra
  • 1,178
  • 2
  • 14
  • 30
0

Found: https://github.com/rdvojmoc/DinkToPdf/issues/7

CSS:

table { 
    page-break-inside: auto;
} 

tr { 
    page-break-inside: avoid; 
    page-break-after: auto;
} 

thead { 
    display: table-header-group;
} 

tfoot { 
    display: table-footer-group;
}
Nelson Nyland
  • 444
  • 5
  • 11