@@ -6,118 +6,138 @@ The Syncfusion® HTML to PDF converter is a .NET library used to convert HTML
66
77## Steps to convert HTML to PDF in Blazor application
88
9- 1 . Create a new C# Blazor Server application project. Select Blazor App from the template and click the Next button.
10- <img src =" HTML_to_PDF_Blazor/htmlconversion_images/blazor_step1.png " alt =" Blazor_step1 " width =" 100% " Height =" Auto " />
11-
12- In the project configuration window, name your project and select Create.
13- <img src =" HTML_to_PDF_Blazor/htmlconversion_images/blazor_step2.png " alt =" Blazor_step2 " width =" 100% " Height =" Auto " />
14- <img src =" HTML_to_PDF_Blazor/htmlconversion_images/blazor_step3.png " alt =" Blazor_step3 " width =" 100% " Height =" Auto " />
15-
16- 2 . Install the [ Syncfusion.HtmlToPdfConverter.Net.Windows] ( https://www.nuget.org/packages/Syncfusion.HtmlToPdfConverter.Net.Windows/ ) NuGet package as a reference to your Blazor Server application from [ NuGet.org] ( https://www.nuget.org/ ) .
17- <img src =" HTML_to_PDF_Blazor/htmlconversion_images/blazor_step_nuget.png " alt =" Blazor_step_nuget " width =" 100% " Height =" Auto " />
18-
19- 3 . Create a new class file named ExportService under Data folder and include the following namespaces in the file.
20-
21- ``` csharp
22- using Syncfusion .HtmlConverter ;
23- using Syncfusion .Pdf ;
24- using System .IO ;
25- ```
26-
27- 4 . Add the following code to convert HTML to PDF document in [ ExportService] ( HTML_to_PDF_Blazor/Data/ExportService.cs ) class.
28-
29- ``` csharp
30- public MemoryStream CreatePdf (string url )
31- {
32- // Initialize HTML to PDF converter.
33- HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter ();
34- // Convert URL to PDF document.
35- PdfDocument document = htmlConverter .Convert (url );
36- // Create memory stream.
37- MemoryStream stream = new MemoryStream ();
38- // Save the document to memory stream.
39- document .Save (stream );
40- return stream ;
41- }
42- ```
43-
44- 5 . Register your service in the ConfigureServices method available in the Startup.cs class as follows.
45-
46- ``` csharp
47- /// <summary >
48- /// Register your ExportService
49- /// </summary >
50- public void ConfigureServices (IServiceCollection services )
51- {
52- services .AddRazorPages ();
53- services .AddServerSideBlazor ();
54- services .AddSingleton <WeatherForecastService >();
55- services .AddSingleton <ExportService >();
56- }
57- ```
58-
59- 6 . Inject ExportService into FetchData.razor using the following code.
60-
61- ``` csharp
62- @inject ExportService exportService
63- @inject Microsoft .JSInterop .IJSRuntime JS
64- @inject NavigationManager NavigationManager
65- @using System .IO ;
66- ```
67-
68- 7 . Create a button in the FetchData.razor using the following code.
69-
70- ``` csharp
71- < button class="btn btn-primary" @onclick="@ExportToPdf">Export to PDF</button>
72- ```
73-
74- 8. Add the ExportToPdf method in FetchData.razor page to call the export service.
75-
76- ```csharp
77- @code {
78- private string currentUrl ;
79- /// <summary >
80- /// Get the current URL
81- /// </summary >
82- protected override void OnInitialized ()
83- {
84- currentUrl = NavigationManager .Uri ;
85- }
86- }
87- @functions
88- {
89- /// <summary >
90- /// Create and download the PDF document
91- /// </summary >
92- protected async Task ExportToPdf ()
93- {
94- ExportService exportService = new ExportService ();
95- using (MemoryStream excelStream = exportService .CreatePdf (currentUrl ))
96- {
97- await JS .SaveAs (" HTML-to-PDF.pdf" , excelStream .ToArray ());
98- }
99- }
100- }
101- ```
102-
103- 9 . Create a class file with FileUtil name and add the following code to invoke the JavaScript action to download the file in the browser.
104-
105- ``` csharp
106- public static class FileUtil
107- {
108- public static ValueTask <object > SaveAs (this IJSRuntime js , string filename , byte [] data )
9+ 1 . Create a new C# Blazor server-side application project. Select ** Blazor Web App** from the template and click the Next button.
10+ <img src =" HTML_to_PDF_Blazor/htmlconversion_images/Blazor-web-app.png " alt =" Blazor_step1 " width =" 100% " Height =" Auto " />
11+
12+ 2 . In the project configuration window, name your project and select Create.
13+ <img src =" HTML_to_PDF_Blazor/htmlconversion_images/Blazor-Server-App.png " alt =" Blazor_step2 " width =" 100% " Height =" Auto " />
14+
15+ 3 . To create a PDF document in a Blazor Server app, install the [ Syncfusion.PDF.Net.Core] ( https://www.nuget.org/packages/Syncfusion.pdf.Net.Core ) package into the Blazor project.
16+ <img src =" HTML_to_PDF_Blazor/htmlconversion_images/Blazor_server_NuGet.png " alt =" Blazor_step_nuget " width =" 100% " Height =" Auto " />
17+
18+ Step 4: Create a new cs file named ** ExportService.cs** under ** Data** folder and include the following namespaces in the file.
19+
20+ ``` csharp
21+ using Syncfusion .Pdf ;
22+ using Syncfusion .Pdf .Graphics ;
23+ using Syncfusion .Pdf .Grid ;
24+ using Syncfusion .Drawing ;
25+ ```
26+
27+ Step 5: The [ PdfDocument] ( https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.PdfDocument.html ) object represents an entire PDF document that is being created. The [ PdfTextElement] ( https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfTextElement.html ) adds text in a PDF document and provides the layout result of the added text by using the location of the next element that decides to prevent content overlapping. The [ PdfGrid] ( https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Grid.PdfGrid.html ) allows table creation by entering data manually or from external data sources.
28+
29+ Add the following code sample in the ` ExportService ` class which illustrates how to create a simple PDF document using ` PdfTextElement ` and ` PdfGrid ` .
30+
31+ ``` csharp
32+ // Export weather data to PDF document.
33+ public static MemoryStream CreatePdf (WeatherForecast [] forecasts )
34+ {
35+ if (forecasts == null )
36+ {
37+ throw new ArgumentNullException (" Forecast cannot be null" );
38+ }
39+ // Create a new PDF document.
40+ using (PdfDocument pdfDocument = new PdfDocument ())
41+ {
42+ int paragraphAfterSpacing = 8 ;
43+ int cellMargin = 8 ;
44+ // Add page to the PDF document.
45+ PdfPage page = pdfDocument .Pages .Add ();
46+ // Create a new font.
47+ PdfStandardFont font = new PdfStandardFont (PdfFontFamily .TimesRoman , 16 );
48+
49+ // Create a text element to draw a text in PDF page.
50+ PdfTextElement title = new PdfTextElement (" Weather Forecast" , font , PdfBrushes .Black );
51+ PdfLayoutResult result = title .Draw (page , new PointF (0 , 0 ));
52+ PdfStandardFont contentFont = new PdfStandardFont (PdfFontFamily .TimesRoman , 12 );
53+ PdfTextElement content = new PdfTextElement (" This component demonstrates fetching data from a service and Exporting the data to PDF document using Syncfusion .NET PDF library." , contentFont , PdfBrushes .Black );
54+ PdfLayoutFormat format = new PdfLayoutFormat ();
55+ format .Layout = PdfLayoutType .Paginate ;
56+ // Draw a text to the PDF document.
57+ result = content .Draw (page , new RectangleF (0 , result .Bounds .Bottom + paragraphAfterSpacing , page .GetClientSize ().Width , page .GetClientSize ().Height ), format );
58+
59+ // Create a PdfGrid.
60+ PdfGrid pdfGrid = new PdfGrid ();
61+ pdfGrid .Style .CellPadding .Left = cellMargin ;
62+ pdfGrid .Style .CellPadding .Right = cellMargin ;
63+ // Applying built-in style to the PDF grid.
64+ pdfGrid .ApplyBuiltinStyle (PdfGridBuiltinStyle .GridTable4Accent1 );
65+
66+ // Assign data source.
67+ pdfGrid .DataSource = forecasts ;
68+ pdfGrid .Style .Font = contentFont ;
69+ // Draw PDF grid into the PDF page.
70+ pdfGrid .Draw (page , new Syncfusion .Drawing .PointF (0 , result .Bounds .Bottom + paragraphAfterSpacing ));
71+
72+ using (MemoryStream stream = new MemoryStream ())
73+ {
74+ // Saving the PDF document into the stream.
75+ pdfDocument .Save (stream );
76+ // Closing the PDF document.
77+ pdfDocument .Close (true );
78+ return stream ;
79+ }
80+ }
81+ }
82+ ```
83+
84+ Step 6: Register the service in the ` Program.cs ` class as follows.
85+
86+ ``` csharp
87+ services .AddRazorPages ();
88+ services .AddServerSideBlazor ();
89+ services .AddSingleton <WeatherForecastService >();
90+ services .AddSingleton <ExportService >();
91+ ```
92+
93+ Step 7: Inject ` ExportService ` into ` Weather.razor ` using the following code.
94+
95+ ``` csharp
96+ @inject ExportToFileService exportService
97+ @inject Microsoft .JSInterop .IJSRuntime JS
98+ @using System .IO ;
99+
100+ ```
101+
102+ Create a button in the ` Weather.razor ` using the following code.
103+
104+ ``` csharp
105+ < button class="btn btn-primary" @onclick="@ExportToPdf">Export to PDF</button>
106+ ```
107+
108+ Add the `ExportToPdf` method in `Weather.razor` page to call the export service.
109+
110+ ```csharp
111+ @functions
112+ {
113+ protected async Task ExportToPdf ()
114+ {
115+ using (MemoryStream excelStream = ExportService .CreatePdf (forecasts ))
116+ {
117+ await JS .SaveAs (" Sample.pdf" , excelStream .ToArray ());
118+ }
119+ }
120+ }
121+ ```
122+
123+ Step 8: Include the ` FileUtil ` class within the ` ExportService.cs ` file to enable file-related operations as part of the export functionality.
124+
125+ ``` csharp
126+ public static class FileUtil
127+ {
128+ public static ValueTask <object > SaveAs (this IJSRuntime js , string filename , byte [] data )
109129 => js .InvokeAsync <object >(
110130 " saveAsFile" ,
111131 filename ,
112132 Convert .ToBase64String (data ));
113- }
114- ```
133+ }
134+ ```
115135
116- 10 . Add the following JavaScript function in the _ Host.cshtml available under the Pages folder.
136+ Step 9: Add the following JavaScript function in the ` App.razor ` available under the ` Components ` folder.
117137
118- ``` csharp
119- < script type = " text/javascript" >
120- function saveAsFile (filename , bytesBase64 ) {
138+ ``` csharp
139+ < script type = " text/javascript" >
140+ function saveAsFile (filename , bytesBase64 ) {
121141 if (navigator .msSaveBlob ) {
122142 // Download document in Edge browser
123143 var data = window .atob (bytesBase64 );
@@ -129,18 +149,28 @@ The Syncfusion® HTML to PDF converter is a .NET library used to convert HTML
129149 navigator .msSaveBlob (blob , filename );
130150 }
131151 else {
132- var link = document .createElement ('a' );
133- link .download = filename ;
134- link .href = " data:application/octet-stream;base64," + bytesBase64 ;
135- document .body .appendChild (link ); // Needed for Firefox
136- link .click ();
137- document .body .removeChild (link );
138- }
152+ var link = document .createElement ('a' );
153+ link .download = filename ;
154+ link .href = " data:application/octet-stream;base64," + bytesBase64 ;
155+ document .body .appendChild (link ); // Needed for Firefox
156+ link .click ();
157+ document .body .removeChild (link );
158+ }
139159 }
140- < / script >
141- ```
160+ < / script >
161+
162+ ```
163+
164+ Step 10 : Build the project .
165+
166+ Click on Build > Build Solution or press Ctrl + Shift + B to build the project .
167+
168+ Step 11 : Run the project .
169+
170+ Click the Start button (green arrow ) or press F5 to run the app .
171+
172+ The following output appears in the browser after executing the program .
173+ < img src = " HTML_to_PDF_Blazor/htmlconversion_images/blazor_step4.png" alt = " Blazor_step4" width = " 100%" Height = " Auto" / >
142174
143- By executing the program , you will get the following output in the browser .
144- < img src = " HTML_to_PDF_Blazor/htmlconversion_images/blazor_step4.png" alt = " Blazor_step4" width = " 100%" Height = " Auto" / >
145- Click the Export to PDF button , and you will get the PDF document with the following output .
146- < img src = " HTML_to_PDF_Blazor/htmlconversion_images/HtmlBlazorOutput.png" alt = " HTMLTOPDF" width = " 100%" Height = " Auto" / >
175+ Click the `Export to PDF ` button to obtain the PDF document with the following output .
176+ < img src = " HTML_to_PDF_Blazor/htmlconversion_images/HtmlBlazorOutput.png" alt = " HTMLTOPDF" width = " 100%" Height = " Auto" / >
0 commit comments