在asp.net core中使用JSReport将多个HTML文件转换为PDF



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

下面是代码:
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[] { };
}
}

可以使用PdfOperation。为此添加如下所示的内容:

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

最新更新