Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML to PDF - page break with PdfSharp and HtmlRenderer

I try to convert HTML to PDF using PdfSharp and 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 this problem with iTextSharp but on this project I have to use PdfSharp and MigraDoc.

like image 397
kiriz Avatar asked Jun 06 '16 14:06

kiriz


2 Answers

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.

like image 156
nvm-uli Avatar answered Sep 19 '22 16:09

nvm-uli


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);
like image 24
user94554 Avatar answered Sep 23 '22 16:09

user94554