Blazor服务器:正在尝试将渲染的页面转换为用于创建pdf的html字符串



我有一个Blazor页面,它的相关模型(planseld(提供了表单提交的输出,例如:

<div class="container" id="PdfContent">
<p style="text-align: center;"><img src="/images/Branding.jpg" alt="" /></p>
<h5 style="text-align: center;"><strong>Title Text</strong></h5>
<p>&nbsp;</p>
<p><span style="font-weight: bold; text-decoration: underline;">Customer Information:</span></p>
<p>@plansold.CustName<br />@plansold.Address1&nbsp; @plansold.Address2<br />@plansold.City,&nbsp;@plansold.State&nbsp;@plansold.Zip<br />@plansold.Phone</p>
<p>Customer Code: @plansold.ContactCode&nbsp; &nbsp; &nbsp;Customer Acct No:&nbsp;@plansold.CustARNumber</p>
<p><span style="font-weight: bold; text-decoration: underline;">Machine Information:</span></p>
<p>Make:&nbsp; @plansold.Make<br />Model: @plansold.Model<br />VIN / Serial:&nbsp;@plansold.SerialNumber<br />Hours:&nbsp;@plansold.MachineHours</p>
<p><span style="font-weight: bold; text-decoration-line: underline;">Maintenance Plan Details:</span></p>
.etc..etc..etc..</div>

我试图找到一种方法,将渲染的HTML提取到一个字符串中,我可以使用它来生成pdf文件。我正在测试几个"HTMLTOPDF"库,但没有一个描述如何做到这一点。我确实尝试了如何将Blazor组件呈现为HTML字符串的链接,但在服务注入方面出现了编译错误。很多关于印刷的文章;Hello World"来自Blazor,但这对我没有帮助,因为我需要包括我的客户信息。理想情况下,只从我的div标记中提取所有渲染的html。

我正在使用.NET 5/Blazor 5。

也许你可以在浏览器上使用js,类似于https://github.com/telerik/blazor-ui/tree/master/common/pdf-jpg-export-js

和/或使用js获取html字符串(最好在.net5上,以减少空白和注释(,并将其发送到服务器进行c#处理,类似于https://github.com/telerik/blazor-ui/tree/master/grid/pdf-export-server

编辑:我不知道;blazor";通过获取html的方法

我刚刚发现的一种可能的方法是使用RazorLight:https://github.com/toddams/RazorLight

它由FluentMail使用,FluentMail将它用作Razor语法模板的渲染器:https://github.com/lukencode/FluentEmail

我还没有实现这个,但看起来它可能会解决你的问题。

最新更新