我试图让产生在服务器端Blazor的PDF文件。我用DinkToPdf作为外部库,以HTML字符串转换为PDF格式。但我具有blazor成分转换为HTML字符串的麻烦。
有一种方法,通过使用Razor视图引擎渲染剃刀模板的字符串。从本网站qazxsw POI
http://fizzylogic.nl/2017/08/03/how-to-generate-pdf-documents-in-asp-net-core/
我需要修改ObjectSettings.HtmlContent是我blazor组件的HTML字符串。
这对我的作品:
...... HtmlContent TemplateGenerator.GetHTMLString =(),......
[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");
}