Skip to content

Commit 6435e05

Browse files
Merge pull request #1 from SuganyaRathinam/master
Added a PDF getting started sample
2 parents 763018b + 592beea commit 6435e05

34 files changed

+1408
-1
lines changed

App.razor

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Router AppAssembly="@typeof(App).Assembly">
2+
<Found Context="routeData">
3+
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
4+
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
5+
</Found>
6+
<NotFound>
7+
<PageTitle>Not found</PageTitle>
8+
<LayoutView Layout="@typeof(MainLayout)">
9+
<p role="alert">Sorry, there's nothing at this address.</p>
10+
</LayoutView>
11+
</NotFound>
12+
</Router>

BlazorPDF.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="20.4.0.43" />
11+
</ItemGroup>
12+
13+
</Project>

BlazorPDF.csproj.user

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<ActiveDebugProfile>IIS Express</ActiveDebugProfile>
5+
</PropertyGroup>
6+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
7+
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
8+
</PropertyGroup>
9+
</Project>

Pages/Counter.razor

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@page "/counter"
2+
3+
<PageTitle>Counter</PageTitle>
4+
5+
<h1>Counter</h1>
6+
7+
<p role="status">Current count: @currentCount</p>
8+
9+
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
10+
11+
@code {
12+
private int currentCount = 0;
13+
14+
private void IncrementCount()
15+
{
16+
currentCount++;
17+
}
18+
}

Pages/Error.cshtml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
@page
2+
@model BlazorPDF.Pages.ErrorModel
3+
4+
<!DOCTYPE html>
5+
<html lang="en">
6+
7+
<head>
8+
<meta charset="utf-8" />
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
10+
<title>Error</title>
11+
<link href="~/css/bootstrap/bootstrap.min.css" rel="stylesheet" />
12+
<link href="~/css/site.css" rel="stylesheet" asp-append-version="true" />
13+
</head>
14+
15+
<body>
16+
<div class="main">
17+
<div class="content px-4">
18+
<h1 class="text-danger">Error.</h1>
19+
<h2 class="text-danger">An error occurred while processing your request.</h2>
20+
21+
@if (Model.ShowRequestId)
22+
{
23+
<p>
24+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
25+
</p>
26+
}
27+
28+
<h3>Development Mode</h3>
29+
<p>
30+
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
31+
</p>
32+
<p>
33+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
34+
It can result in displaying sensitive information from exceptions to end users.
35+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
36+
and restarting the app.
37+
</p>
38+
</div>
39+
</div>
40+
</body>
41+
42+
</html>

Pages/Error.cshtml.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.Mvc.RazorPages;
3+
using System.Diagnostics;
4+
5+
namespace BlazorPDF.Pages
6+
{
7+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
8+
[IgnoreAntiforgeryToken]
9+
public class ErrorModel : PageModel
10+
{
11+
public string? RequestId { get; set; }
12+
13+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
14+
15+
private readonly ILogger<ErrorModel> _logger;
16+
17+
public ErrorModel(ILogger<ErrorModel> logger)
18+
{
19+
_logger = logger;
20+
}
21+
22+
public void OnGet()
23+
{
24+
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
25+
}
26+
}
27+
}

Pages/FetchData.razor

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
@page "/fetchdata"
2+
@using BlazorPDF.Data
3+
@inject WeatherForecastService ForecastService
4+
5+
<PageTitle>Weather forecast</PageTitle>
6+
7+
<h1>Weather forecast</h1>
8+
9+
<p>This component demonstrates fetching data from a service.</p>
10+
11+
@if (forecasts == null)
12+
{
13+
<p><em>Loading...</em></p>
14+
}
15+
else
16+
{
17+
<table class="table">
18+
<thead>
19+
<tr>
20+
<th>Date</th>
21+
<th>Temp. (C)</th>
22+
<th>Temp. (F)</th>
23+
<th>Summary</th>
24+
</tr>
25+
</thead>
26+
<tbody>
27+
@foreach (var forecast in forecasts)
28+
{
29+
<tr>
30+
<td>@forecast.Date.ToShortDateString()</td>
31+
<td>@forecast.TemperatureC</td>
32+
<td>@forecast.TemperatureF</td>
33+
<td>@forecast.Summary</td>
34+
</tr>
35+
}
36+
</tbody>
37+
</table>
38+
}
39+
40+
@code {
41+
private WeatherForecast[]? forecasts;
42+
43+
protected override async Task OnInitializedAsync()
44+
{
45+
forecasts = await ForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now));
46+
}
47+
}

Pages/Index.razor

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@page "/"
2+
@using BlazorPDF.Data;
3+
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment hostingEnvironment
4+
@inject Microsoft.JSInterop.IJSRuntime JS
5+
6+
<h2>PDF Library</h2>
7+
<p>Click the button to view a PDF document generated by Blazor PDF library.</p>
8+
<button class="btn btn-primary" onclick="@CreatePDF">Create PDF</button>
9+
@code{
10+
protected async void CreatePDF()
11+
{
12+
ExportService service = new ExportService(hostingEnvironment);
13+
MemoryStream documentStream = service.CreatePDF();
14+
await JS.InvokeAsync<object>(
15+
"saveAsFile",
16+
"Invoice.pdf",
17+
Convert.ToBase64String(documentStream.ToArray()));
18+
}
19+
}

Pages/_Host.cshtml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
@page "/"
2+
@using Microsoft.AspNetCore.Components.Web
3+
@namespace BlazorPDF.Pages
4+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
5+
6+
<!DOCTYPE html>
7+
<html lang="en">
8+
<head>
9+
<meta charset="utf-8" />
10+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
11+
<base href="~/" />
12+
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
13+
<link href="css/site.css" rel="stylesheet" />
14+
<link href="BlazorPDF.styles.css" rel="stylesheet" />
15+
<link rel="icon" type="image/png" href="favicon.png"/>
16+
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
17+
</head>
18+
<body>
19+
<component type="typeof(App)" render-mode="ServerPrerendered" />
20+
21+
<div id="blazor-error-ui">
22+
<environment include="Staging,Production">
23+
An error has occurred. This application may no longer respond until reloaded.
24+
</environment>
25+
<environment include="Development">
26+
An unhandled exception has occurred. See browser dev tools for details.
27+
</environment>
28+
<a href="" class="reload">Reload</a>
29+
<a class="dismiss">🗙</a>
30+
</div>
31+
<script type="text/javascript">
32+
function saveAsFile(filename, bytesBase64) {
33+
if (navigator.msSaveBlob) {
34+
//Download document in Edge browser
35+
var data = window.atob(bytesBase64);
36+
var bytes = new Uint8Array(data.length);
37+
for (var i = 0; i < data.length; i++) {
38+
bytes[i] = data.charCodeAt(i);
39+
}
40+
var blob = new Blob([bytes.buffer], { type: "application/octet-stream" });
41+
navigator.msSaveBlob(blob, filename);
42+
}
43+
else {
44+
var link = document.createElement('a');
45+
link.download = filename;
46+
link.href = "data:application/octet-stream;base64," + bytesBase64;
47+
document.body.appendChild(link); // Needed for Firefox
48+
link.click();
49+
document.body.removeChild(link);
50+
}
51+
}
52+
</script>
53+
<script src="_framework/blazor.server.js"></script>
54+
</body>
55+
</html>

Program.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using BlazorPDF.Data;
2+
using Microsoft.AspNetCore.Components;
3+
using Microsoft.AspNetCore.Components.Web;
4+
5+
var builder = WebApplication.CreateBuilder(args);
6+
7+
8+
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("OTEwODM4QDMyMzAyZTM0MmUzMGwybS9hbGswSEo2eURRRjFCVVUxc0FlWFAvWDdCanIvRGlvS084N0pYR1k9");
9+
10+
// Add services to the container.
11+
builder.Services.AddRazorPages();
12+
builder.Services.AddServerSideBlazor();
13+
builder.Services.AddSingleton<WeatherForecastService>();
14+
15+
var app = builder.Build();
16+
17+
// Configure the HTTP request pipeline.
18+
if (!app.Environment.IsDevelopment())
19+
{
20+
app.UseExceptionHandler("/Error");
21+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
22+
app.UseHsts();
23+
}
24+
25+
app.UseHttpsRedirection();
26+
27+
app.UseStaticFiles();
28+
29+
app.UseRouting();
30+
31+
app.MapBlazorHub();
32+
app.MapFallbackToPage("/_Host");
33+
34+
app.Run();

0 commit comments

Comments
 (0)