我正在尝试在服务器端 Blazor 中生成 pdf。我使用 DinkToPdf 作为外部库将 HTML 字符串转换为 pdf。但是我在将 blazor 组件转换为 HTML 字符串时遇到了麻烦。
有一种方法可以使用 Razor ViewEngine 将 Razor 模板呈现为字符串。从这个网站http://fizzylogic.nl/2017/08/03/how-to-generate-pdf-documents-in-asp-net-core/
[HttpGet]
public async Task<IActionResult> CreatePDF()
{
var globalSettings = new GlobalSettings
{
ColorMode = ColorMode.Color,
Orientation = Orientation.Portrait,
PaperSize = PaperKind.A4,
Margins = new MarginSettings { Top = 10 },
DocumentTitle = "PDF Report",
};
var objectSettings = new ObjectSettings
{
PagesCount = true,
HtmlContent = "<h>Hello World</h>",
WebSettings = { DefaultEncoding = "utf-8"},
HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true },
FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Center = "Report Footer" }
};
var pdf = new HtmlToPdfDocument()
{
GlobalSettings = globalSettings,
Objects = { objectSettings }
};
var file = _converter.Convert(pdf);
return File(file,"application/pdf");
}
我需要将 ObjectSettings.HtmlContent 修改为我的 blazor 组件 html 字符串。