HTML to PDF - page break with PdfSharp and HtmlRenderer

拈花ヽ惹草 提交于 2019-11-30 12:50:29

问题


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

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.


回答1:


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.




回答2:


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);



回答3:


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

td { page-break-inside: avoid; }




回答4:


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>";
            }


来源:https://stackoverflow.com/questions/37660448/html-to-pdf-page-break-with-pdfsharp-and-htmlrenderer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!