Skip to content

Commit 5d1a297

Browse files
author
SowmiyaLoganathan
authored
Merge pull request #12 from SyncfusionExamples/840386-Html-Sample
840386- Need to add the explanation for each step and update the sample
2 parents 094b5c9 + 9c918ee commit 5d1a297

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

Blazor/HTML_to_PDF_Blazor/Data/ExportService.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,12 @@ namespace HTML_to_PDF_Blazor.Data
1010
{
1111
public class ExportService
1212
{
13-
public MemoryStream CreatePdf()
13+
public MemoryStream CreatePdf(string url)
1414
{
1515
//Initialize HTML to PDF converter.
1616
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
17-
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
18-
//Set Blink viewport size.
19-
blinkConverterSettings.ViewPortSize = new Syncfusion.Drawing.Size(1280, 0);
20-
//Assign Blink converter settings to HTML converter.
21-
htmlConverter.ConverterSettings = blinkConverterSettings;
2217
//Convert URL to PDF document.
23-
PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
18+
PdfDocument document = htmlConverter.Convert(url);
2419
//Create memory stream.
2520
MemoryStream stream = new MemoryStream();
2621
//Save the document to memory stream.

Blazor/HTML_to_PDF_Blazor/HTML_to_PDF_Blazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="20.3.0.58" />
8+
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="22.2.5" />
99
</ItemGroup>
1010

1111
</Project>

Blazor/HTML_to_PDF_Blazor/Pages/FetchData.razor

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,33 @@
44
@inject WeatherForecastService ForecastService
55
@inject ExportService exportService
66
@inject Microsoft.JSInterop.IJSRuntime JS
7+
@inject NavigationManager NavigationManager
78
@using System.IO;
89
<h1>Weather forecast</h1>
910

1011
<p>This component demonstrates fetching data from a service.</p>
1112

1213
<button class="btn btn-primary" @onclick="@ExportToPdf">Export to PDF</button>
1314

15+
@code {
16+
private string currentUrl;
17+
/// <summary>
18+
/// Get the current URL
19+
/// </summary>
20+
protected override void OnInitialized()
21+
{
22+
currentUrl = NavigationManager.Uri;
23+
}
24+
}
1425
@functions
1526
{
16-
27+
/// <summary>
28+
/// Create and download the PDF document
29+
/// </summary>
1730
protected async Task ExportToPdf()
1831
{
1932
ExportService exportService = new ExportService();
20-
using (MemoryStream excelStream = exportService.CreatePdf())
33+
using (MemoryStream excelStream = exportService.CreatePdf(currentUrl))
2134
{
2235
await JS.SaveAs("HTML-to-PDF.pdf", excelStream.ToArray());
2336
}

Blazor/README.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,12 @@ The Syncfusion HTML to PDF converter is a .NET library used to convert HTML or w
2727
4. Add the following code to convert HTML to PDF document in [ExportService](HTML_to_PDF_Blazor/Data/ExportService.cs) class.
2828

2929
```csharp
30-
public MemoryStream CreatePdf()
30+
public MemoryStream CreatePdf(string url)
3131
{
3232
//Initialize HTML to PDF converter.
33-
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
34-
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
35-
//Set Blink viewport size.
36-
blinkConverterSettings.ViewPortSize = new Syncfusion.Drawing.Size(1280, 0);
37-
//Assign Blink converter settings to HTML converter.
38-
htmlConverter.ConverterSettings = blinkConverterSettings;
33+
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
3934
//Convert URL to PDF document.
40-
PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
35+
PdfDocument document = htmlConverter.Convert(url);
4136
//Create memory stream.
4237
MemoryStream stream = new MemoryStream();
4338
//Save the document to memory stream.
@@ -49,6 +44,9 @@ The Syncfusion HTML to PDF converter is a .NET library used to convert HTML or w
4944
5. Register your service in the ConfigureServices method available in the Startup.cs class as follows.
5045

5146
```csharp
47+
/// <summary>
48+
/// Register your ExportService
49+
/// </summary>
5250
public void ConfigureServices(IServiceCollection services)
5351
{
5452
services.AddRazorPages();
@@ -63,6 +61,7 @@ The Syncfusion HTML to PDF converter is a .NET library used to convert HTML or w
6361
```csharp
6462
@inject ExportService exportService
6563
@inject Microsoft.JSInterop.IJSRuntime JS
64+
@inject NavigationManager NavigationManager
6665
@using System.IO;
6766
```
6867

@@ -75,13 +74,25 @@ The Syncfusion HTML to PDF converter is a .NET library used to convert HTML or w
7574
8. Add the ExportToPdf method in FetchData.razor page to call the export service.
7675

7776
```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+
}
7887
@functions
7988
{
80-
89+
/// <summary>
90+
/// Create and download the PDF document
91+
/// </summary>
8192
protected async Task ExportToPdf()
8293
{
8394
ExportService exportService = new ExportService();
84-
using (MemoryStream excelStream = exportService.CreatePdf())
95+
using (MemoryStream excelStream = exportService.CreatePdf(currentUrl))
8596
{
8697
await JS.SaveAs("HTML-to-PDF.pdf", excelStream.ToArray());
8798
}

0 commit comments

Comments
 (0)