.netasp.net-corehtml-to-pdfjsreport

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


I have multiple HTML Files (for e.g.: "test.html", "test2.html","test3.html") in my project Now I need to generate the single PDF file using JsReport. it means "test.html" on first page "test2.html" on second page and "test3.html" on third page in pdf generated File.

here is the code:

 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[] { };
            }
        }

Solution

  • You can use the PdfOperation.Append for this purpose as shown below:

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