18

I try to convert HTML to PDF using HtmlRenderer. This is part of code:

private byte[] CreateHtmlContent()
{
    string htmlContent = File.ReadAllText(@"htmlExample.txt");

    using (MemoryStream ms = new MemoryStream())
    {
        PdfDocument pdfDocument = new PdfDocument();
        PdfDocument pdf = PdfGenerator.GeneratePdf(htmlContent, PdfSharp.PageSize.A4, 60);
        pdf.Save(ms);
        res = ms.ToArray();
    }
    return res;
}

Everything works fine except page break. On some pages I have result like on this image

HTML page break

Is it possible to fix this? HTML content is simple HTML that contains only headings and paragraphs and no other tags. I had no problem with iTextSharp but on this project I have to use PDFSharp and MigraDoc.

kiriz
  • 655
  • 1
  • 7
  • 24
  • I am facing a similar issue. Did you get a fix for it? – user1071979 Jul 11 '16 at 16:37
  • I am facing similar problem from 2 days.. Did you fix that??? – Mallikarjun Aug 30 '16 at 08:25
  • I (actually a coleague from work) had this issue and he didn't find a solution. He "solved" the issue by installing the HtmlAgilityPack library for loading HTML and than created element by element using MigraDoc. – kiriz Aug 30 '16 at 13:12
  • I created a work-around for adding page breaks - details at https://stackoverflow.com/a/53549470/1118569 – Matt Eno Nov 30 '18 at 00:18

5 Answers5

20

I had a similar challenge and resolved it as I found this pull request on github: https://github.com/ArthurHub/HTML-Renderer/pull/41

You can set the custom-css-property

td { page-break-inside: avoid; }

on all elements or selectors you want (td, p, .my-class, etc.) to control the page breaking.

You can use the value "auto" if you want the library to control your page breaking on certain elements

td { page-break-inside: auto; }

There is also a example for page breaking in running text.

nvm-uli
  • 626
  • 1
  • 7
  • 14
  • How to include reference to CSS? I am able to generate PDF but it does not consider any css styles. (My css are in different file - not inline) – TechTurtle Aug 29 '16 at 13:56
  • 1
    This worked for me, or at least it seems so now. – Alf Kåre Lefdal Nov 17 '16 at 16:54
  • 1
    Please note that the page-break attribute is not included in the official version that is available via NuGet, etc. Download the assembly directly from GitHub instead. – nvm-uli Mar 12 '18 at 15:17
  • 5
    It's available in the 1.5.1 beta on NuGet – abney317 Apr 30 '18 at 22:14
5

This is a little late, but I ran into the same issue. The problem is the margin set on the GeneratePdf call. Remove it and it's fine.

    PdfDocument pdf = PdfGenerator.GeneratePdf(htmlContent, PdfSharp.PageSize.A4);
user94554
  • 151
  • 1
  • 1
1

You can use the prerelase version in Nuget (1.5.1-beta1) and then:

td { page-break-inside: avoid; }

0

This is also resolved by adding an appropriate DIV tag if you're not using tables.

foreach (DataRow row in group)
            {
                HTMLoutput += "<div style=\"page-break-inside: avoid\"> ";
                HTMLoutput += "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
                HTMLoutput += "</div>";
            }
Tuffknitz
  • 36
  • 6
0
    table{
    page-break-inside: avoid;    
}

When I used this CSS I was facing this issue.

enter image description here

So what I did? I just set also

table{
border:none;
}

if you want to show the border for a specific table also, you can use inline CSS or set border by using a specific table id.

Here is the final result of the pdf generated using pdfsharp.

enter image description here