1

I have a Blazor page that has output from a form submission rendered from it's related model (plansold) such as:

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

Im trying to find a method to extract the rendered HTML into a string that I can use to generate a pdf file. Im in the middle of testing several 'HTML TO PDF' libraries, but none of them have described how to do this. I did try the link at How to render a Blazor component into an HTML string but got compile errors on the Service injection side. Lots of articles out there on printing "Hello World" from Blazor, but that's not helping me, as I need to include my customer information. Ideally looking to extract all rendered html from my div tags only.

Im using .NET 5 / Blazor 5.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
AndyC
  • 47
  • 6

2 Answers2

0

Perhaps you could do it with js on the browser, something similar to https://github.com/telerik/blazor-ui/tree/master/common/pdf-jpg-export-js

And/or use js to get the html string (better be on .net5 to reduce whitespace and comments) and send it to the server for c# processing, similarly to https://github.com/telerik/blazor-ui/tree/master/grid/pdf-export-server

Edit: i am not aware of a "blazor" way to get the html though

rdmptn
  • 5,413
  • 1
  • 16
  • 29
  • Thanks! I was about to down through the JS Rabbit-hole, but have decided Im going to just code the pdf using IronPDF library. Seems like the most straightforward thing to do. – AndyC Dec 21 '20 at 13:33
  • I am facing the exact same problem. Downside to going with IronPDF or similar libraries is that whenever you change your html rendering, you will have to make similar changes on the controller side as well to keep the views in sync. With HTML to PDF conversion, hopefully we have all the HTML code in one place. – user3656651 Oct 24 '21 at 13:26
0

One possible way I've just found is to use RazorLight: https://github.com/toddams/RazorLight

It is used by FluentEmail which uses it as a Renderer for Razor syntax templates: https://github.com/lukencode/FluentEmail

I haven't implemented this yet but looks like it might solve your problem.

PazMaster
  • 11
  • 4