Skip to content

Commit 094b5c9

Browse files
author
SowmiyaLoganathan
authored
Merge pull request #11 from SyncfusionExamples/HTMLToPDF-Sample
839931- Added the HTML to PDF sample
2 parents 8a46740 + ebbb32d commit 094b5c9

File tree

77 files changed

+74417
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+74417
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using HtmlToPDFSample.Models;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Syncfusion.HtmlConverter;
4+
using Syncfusion.Pdf;
5+
using Syncfusion.Pdf.Graphics;
6+
using System.Diagnostics;
7+
8+
namespace HtmlToPDFSample.Controllers
9+
{
10+
public class HomeController : Controller
11+
{
12+
private readonly ILogger<HomeController> _logger;
13+
14+
public HomeController(ILogger<HomeController> logger)
15+
{
16+
_logger = logger;
17+
}
18+
19+
public IActionResult ExportToPDF()
20+
{
21+
//Create a new PDF document.
22+
PdfDocument document = new PdfDocument();
23+
MemoryStream stream = new MemoryStream();
24+
25+
try
26+
{
27+
//Initialize HTML to PDF converter.
28+
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
29+
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
30+
//Set command line arguments to run without the sandbox.
31+
blinkConverterSettings.CommandLineArguments.Add("--no-sandbox");
32+
blinkConverterSettings.CommandLineArguments.Add("--disable-setuid-sandbox");
33+
//Set Blink viewport size.
34+
blinkConverterSettings.ViewPortSize = new Syncfusion.Drawing.Size(1280, 0);
35+
//Assign Blink converter settings to HTML converter.
36+
htmlConverter.ConverterSettings = blinkConverterSettings;
37+
//Convert URL to PDF document.
38+
document = htmlConverter.Convert("https://www.google.com");
39+
}
40+
catch (Exception ex)
41+
{
42+
// Set the page size.
43+
document.PageSettings.Size = PdfPageSize.A4;
44+
//Add a page to the document.
45+
PdfPage page = document.Pages.Add();
46+
47+
//Create PDF graphics for the page.
48+
PdfGraphics graphics = page.Graphics;
49+
//Set the font.
50+
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
51+
//Draw the text.
52+
graphics.DrawString(ex.Message, font, PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(0, 200, 400, 100));
53+
54+
}
55+
56+
//Save and close the document.
57+
document.Save(stream);
58+
document.Close();
59+
return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "HtmlOutput.pdf");
60+
}
61+
public IActionResult Index()
62+
{
63+
return View();
64+
}
65+
66+
public IActionResult Privacy()
67+
{
68+
return View();
69+
}
70+
71+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
72+
public IActionResult Error()
73+
{
74+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
75+
}
76+
}
77+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Linux" Version="21.1.41" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="data\dependenciesInstall.sh">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.33502.453
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HtmlToPDFSample", "HtmlToPDFSample.csproj", "{6BEA41A6-32C2-402A-B139-C492A2F73BC6}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{6BEA41A6-32C2-402A-B139-C492A2F73BC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{6BEA41A6-32C2-402A-B139-C492A2F73BC6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{6BEA41A6-32C2-402A-B139-C492A2F73BC6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{6BEA41A6-32C2-402A-B139-C492A2F73BC6}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {08546101-0A1B-4C7C-8EBB-23326CC45929}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace HtmlToPDFSample.Models
2+
{
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8+
}
9+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
// Add services to the container.
4+
builder.Services.AddControllersWithViews();
5+
6+
var app = builder.Build();
7+
8+
// Configure the HTTP request pipeline.
9+
if (!app.Environment.IsDevelopment())
10+
{
11+
app.UseExceptionHandler("/Home/Error");
12+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
13+
app.UseHsts();
14+
}
15+
16+
app.UseHttpsRedirection();
17+
app.UseStaticFiles();
18+
19+
app.UseRouting();
20+
21+
app.UseAuthorization();
22+
23+
app.MapControllerRoute(
24+
name: "default",
25+
pattern: "{controller=Home}/{action=Index}/{id?}");
26+
27+
app.Run();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:27294",
7+
"sslPort": 44311
8+
}
9+
},
10+
"profiles": {
11+
"HtmlToPDFSample": {
12+
"commandName": "Project",
13+
"dotnetRunMessages": true,
14+
"launchBrowser": true,
15+
"applicationUrl": "https://localhost:7075;http://localhost:5190",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"IIS Express": {
21+
"commandName": "IISExpress",
22+
"launchBrowser": true,
23+
"environmentVariables": {
24+
"ASPNETCORE_ENVIRONMENT": "Development"
25+
}
26+
}
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
@{
5+
Html.BeginForm("ExportToPDF", "Home", FormMethod.Post);
6+
{
7+
<div>
8+
<input type="submit" value="Convert HTML to PDF" style="width:250px;height:27px" />
9+
</div>
10+
}
11+
Html.EndForm();
12+
}
13+
<div class="text-center">
14+
<h1 class="display-4">Welcome</h1>
15+
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
16+
</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{
2+
ViewData["Title"] = "Privacy Policy";
3+
}
4+
<h1>@ViewData["Title"]</h1>
5+
6+
<p>Use this page to detail your site's privacy policy.</p>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@model ErrorViewModel
2+
@{
3+
ViewData["Title"] = "Error";
4+
}
5+
6+
<h1 class="text-danger">Error.</h1>
7+
<h2 class="text-danger">An error occurred while processing your request.</h2>
8+
9+
@if (Model.ShowRequestId)
10+
{
11+
<p>
12+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
13+
</p>
14+
}
15+
16+
<h3>Development Mode</h3>
17+
<p>
18+
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
19+
</p>
20+
<p>
21+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
22+
It can result in displaying sensitive information from exceptions to end users.
23+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
24+
and restarting the app.
25+
</p>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>@ViewData["Title"] - HtmlToPDFSample</title>
7+
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
8+
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
9+
<link rel="stylesheet" href="~/HtmlToPDFSample.styles.css" asp-append-version="true" />
10+
</head>
11+
<body>
12+
<header>
13+
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
14+
<div class="container-fluid">
15+
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">HtmlToPDFSample</a>
16+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
17+
aria-expanded="false" aria-label="Toggle navigation">
18+
<span class="navbar-toggler-icon"></span>
19+
</button>
20+
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
21+
<ul class="navbar-nav flex-grow-1">
22+
<li class="nav-item">
23+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
24+
</li>
25+
<li class="nav-item">
26+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
27+
</li>
28+
</ul>
29+
</div>
30+
</div>
31+
</nav>
32+
</header>
33+
<div class="container">
34+
<main role="main" class="pb-3">
35+
@RenderBody()
36+
</main>
37+
</div>
38+
39+
<footer class="border-top footer text-muted">
40+
<div class="container">
41+
&copy; 2023 - HtmlToPDFSample - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
42+
</div>
43+
</footer>
44+
<script src="~/lib/jquery/dist/jquery.min.js"></script>
45+
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
46+
<script src="~/js/site.js" asp-append-version="true"></script>
47+
@await RenderSectionAsync("Scripts", required: false)
48+
</body>
49+
</html>

0 commit comments

Comments
 (0)