Convert Multiple HTML Files to PDF Single file with multiple pages in asp.net core using JSReport

问题描述 投票:0回答:1

我的项目中有多个 HTML 文件(例如:“test.html”、“test2.html”、“test3.html”)现在我需要使用 JsReport 生成单个 PDF 文件。它表示第一页上的“test.html”,第二页上的“test2.html”和 pdf 生成的文件中第三页上的“test3.html”。

这里是代码:

 byte[] pdfBytes = new byte[5];
 pdfBytes = await GetPdfReport(sbFirstParagraph.ToString());

HtmlText: Single file data passing I need to pass multiple html file data.
public async Task<byte[]> GetPdfReport(string HtmlText)
        {
            try
            {
                var reportObj = await jsreportObj.RenderAsync(new RenderRequest()
                {
                    Template = new Template
                    {
                        Content = HtmlText, 
                        Engine = Engine.None,
                        Recipe = Recipe.ChromePdf,
                        Chrome = new Chrome { MarginLeft = "0", MarginRight = "0", Format = "A4", Width = "100%" }
                    },
                    Options = new RenderOptions()
                    {
                        Preview = true,
                        Timeout = 600000
                    }
                });
                using (var memoryStream = new MemoryStream())
                {
                    await reportObj.Content.CopyToAsync(memoryStream);
                    return memoryStream.ToArray();
                }
            }
            catch (Exception e)
            {
                return new byte[] { };
            }
        }
.net asp.net-core html-to-pdf jsreport
1个回答
0
投票

您可以为此目的使用 PdfOperation.Append,如下所示:

            Template = new Template
        {
            Content = html1,
            Engine = Engine.JsRender,
            Recipe = Recipe.ChromePdf,
            Chrome = new Chrome
            {
                MarginTop = "0mm",
                MarginLeft = "0mm",
                MarginBottom = "0mm",
                MarginRight = "0mm",
                Format = "A4"
            },
            PdfOperations = new List<PdfOperation>()
            {
                new()
                {
                    Type = PdfOperationType.Append,
                   
                    Template = new Template
                    {
                        Content = html2,
                        Engine = Engine.JsRender,
                        Recipe = Recipe.ChromePdf,
                        Chrome = new Chrome
                        {
                            MarginTop = "0mm",
                            MarginLeft = "0mm",
                            MarginBottom = "0mm",
                            MarginRight = "0mm",
                            Format = "A4"
                        }
                    }
                },
                new()
                {
                    Type = PdfOperationType.Append,
                   
                    Template = new Template
                    {
                        Content = html3,
                        Engine = Engine.JsRender,
                        Recipe = Recipe.ChromePdf,
                        Chrome = new Chrome
                        {
                            MarginTop = "0mm",
                            MarginLeft = "0mm",
                            MarginBottom = "0mm",
                            MarginRight = "0mm",
                            Format = "A4"
                        }
                    }
                }
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.