How to Page Break HTML Content in HTML Renderer

帅比萌擦擦* 提交于 2019-12-06 06:43:41

问题


I have a project where HTML code is converted to a PDF using HTML Renderer. The HTML code contains a single table. The PDF is displayed but the issue is that the contents of the table are cut off at the end. So is there any solution to the problem?

PdfDocument pdf=new PdfDocument();

            var config = new PdfGenerateConfig()
            {
                MarginBottom = 20,
                MarginLeft = 20,
                MarginRight = 20,
                MarginTop = 20,
            };
            //config.PageOrientation = PageOrientation.Landscape;
            config.ManualPageSize = new PdfSharp.Drawing.XSize(1080, 828);

            pdf = PdfGenerator.GeneratePdf(html, config);

            byte[] fileContents = null;
            using (MemoryStream stream = new MemoryStream())
            {
                pdf.Save(stream, true);
                fileContents = stream.ToArray();
                return new FileStreamResult(new MemoryStream(fileContents.ToArray()), "application/pdf");
            }

回答1:


HTMLRenderer should be able break the table to the next page.
See also:
https://github.com/ArthurHub/HTML-Renderer/pull/41

Make sure you are using the latest version. You may have to add those CSS properties.

Also see this answer:
https://stackoverflow.com/a/37833107/162529




回答2:


As far as I know page breaks are not supported, but I've done a bit of a work-around (which may not work for all cases) by splitting the HTML into separate pages using a page break class, then adding each page to the pdf.

See example code below:

    //This will only work on page break elements that are direct children of the body element.
    //Each page's content must be inside the pagebreak element
    private static PdfDocument SplitHtmlIntoPagedPdf(string html, string pageBreakBeforeClass, PdfGenerateConfig config, PdfDocument pdf)
    {
        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);
        var htmlBodyNode = htmlDoc.DocumentNode.SelectSingleNode("//body");

        var tempHtml = string.Empty;
        foreach (var bodyNode in htmlBodyNode.ChildNodes)
        {
            if (bodyNode.Attributes["class"]?.Value == pageBreakBeforeClass)
            {
                if (!string.IsNullOrWhiteSpace(tempHtml))
                {
                    //add any content found before the page break
                    AddPageToPdf(htmlDoc,tempHtml,config,ref pdf);
                    tempHtml = string.Empty;
                }
                AddPageToPdf(htmlDoc,bodyNode.OuterHtml,config,ref pdf);
            }
            else
            {
                tempHtml += bodyNode.OuterHtml;
            }
        }
        if (!string.IsNullOrWhiteSpace(tempHtml))
        {
            //add any content found after the last page break
            AddPageToPdf(htmlDoc, tempHtml, config, ref pdf);
        }

        return pdf;
    }

    private static void AddPageToPdf(HtmlDocument htmlDoc, string html, PdfGenerateConfig config, ref PdfDocument pdf)
    {
        var tempDoc = new HtmlDocument();
        tempDoc.LoadHtml(htmlDoc.DocumentNode.OuterHtml);
        var docNode = tempDoc.DocumentNode;
        docNode.SelectSingleNode("//body").InnerHtml = html;
        var nodeDoc = PdfGenerator.GeneratePdf(docNode.OuterHtml, config);
        using (var tempMemoryStream = new MemoryStream())
        {
            nodeDoc.Save(tempMemoryStream, false);
            var openedDoc = PdfReader.Open(tempMemoryStream, PdfDocumentOpenMode.Import);
            foreach (PdfPage page in openedDoc.Pages)
            {
                pdf.AddPage(page);
            }
        }
    }

Then call the code as follows:

            var pdf = new PdfDocument();
            var config = new PdfGenerateConfig()
            {
                MarginLeft = 5,
                MarginRight = 5,
                PageOrientation = PageOrientation.Portrait,
                PageSize = PageSize.A4
            };
            if (!string.IsNullOrWhiteSpace(pageBreakBeforeClass))
            {
                pdf = SplitHtmlIntoPagedPdf(html, pageBreakBeforeClass, config, pdf);
            }
            else
            {
                pdf = PdfGenerator.GeneratePdf(html, config);
            }

For any html that you want to have in its own page, just put the html inside a div with a class of "pagebreak" (or whatever you want to call it). If you want to, you could add that class to your css and give it "page-break-before: always;", so that the html will be print-friendly.



来源:https://stackoverflow.com/questions/44413259/how-to-page-break-html-content-in-html-renderer

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