-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the Blog to PDF converter sample using the HTMLConverter library
- Loading branch information
1 parent
4431239
commit 77de12f
Showing
84 changed files
with
78,281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
using HTMLToPDF_WebApplication.Models; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Syncfusion.Drawing; | ||
using Syncfusion.HtmlConverter; | ||
using Syncfusion.Pdf; | ||
using Syncfusion.Pdf.Graphics; | ||
using Syncfusion.Pdf.Interactive; | ||
using Syncfusion.Pdf.Parsing; | ||
using System.Diagnostics; | ||
using System.Net; | ||
using System.Text; | ||
|
||
namespace HTMLToPDF_WebApplication.Controllers | ||
{ | ||
public class HomeController : Controller | ||
{ | ||
private readonly ILogger<HomeController> _logger; | ||
|
||
public HomeController(ILogger<HomeController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public IActionResult Index() | ||
{ | ||
return View(); | ||
} | ||
|
||
public IActionResult Privacy() | ||
{ | ||
return View(); | ||
} | ||
|
||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] | ||
public IActionResult Error() | ||
{ | ||
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); | ||
} | ||
public ActionResult ExportToPDF(URL url) | ||
{ | ||
//Initialize HTML to PDF converter. | ||
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); | ||
|
||
//Initialize Blink converter settings. | ||
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings(); | ||
|
||
//Enable lazy load images. | ||
blinkConverterSettings.EnableLazyLoadImages = true; | ||
|
||
//Set the page size and orientation of the PDF document. | ||
blinkConverterSettings.PdfPageSize = PdfPageSize.A4; | ||
blinkConverterSettings.Orientation = PdfPageOrientation.Landscape; | ||
|
||
//Set the Scale of the PDF document. | ||
blinkConverterSettings.Scale = 1.0f; | ||
|
||
//Set the margin of the PDF document. | ||
blinkConverterSettings.Margin.All = 0; | ||
|
||
//Set the JavaScript to remove the unwanted elements from the HTML page. | ||
//Set the JavaScript to remove the unwanted elements from the HTML page. | ||
blinkConverterSettings.JavaScript = "document.getElementById(\"liveChatApp\").remove();\ndocument.getElementById(\"wpfront-scroll-top\").remove();\n document.getElementById(\"top-section\").remove(); \n document.getElementById(\"main-menu-section\").remove(); \n document.getElementById(\"home-page-header\").remove();\n document.getElementById(\"social-icon\").remove(); \n document.getElementById(\"subscription-section\").remove();\n document.getElementById(\"toc-section\").remove();\n document.getElementById(\"category-ad-section\").remove();\n document.getElementById(\"comments-section\").remove();\n document.getElementById(\"cookie\").remove()"; | ||
|
||
//Assign the header element to PdfHeader of Blink converter settings. | ||
blinkConverterSettings.PdfHeader = AddHeader(blinkConverterSettings.PdfPageSize, url.HeaderText); | ||
|
||
//Assign the footer element to PdfFooter of Blink converter settings. | ||
blinkConverterSettings.PdfFooter = AddFooter(blinkConverterSettings.PdfPageSize); | ||
|
||
blinkConverterSettings.AdditionalDelay = 20000; | ||
|
||
//Assign the Blink converter settings to HTML converter. | ||
htmlConverter.ConverterSettings = blinkConverterSettings; | ||
|
||
if(url.BlogLink == string.Empty) | ||
{ | ||
throw new PdfException("Blog link is empty. Kindly add Blog link in the text box before performing conversion"); | ||
} | ||
|
||
PdfDocument document = htmlConverter.Convert(url.BlogLink); | ||
//Create memory stream. | ||
MemoryStream stream = new MemoryStream(); | ||
//Save and close the document. | ||
document.Save(stream); | ||
document.Close(); | ||
|
||
|
||
//Load the PDF document | ||
PdfLoadedDocument doc = new PdfLoadedDocument(stream); | ||
|
||
//Get first page from document | ||
PdfLoadedPage? page = doc.Pages[0] as PdfLoadedPage; | ||
|
||
//Create PDF graphics for the page | ||
PdfGraphics graphics = page.Graphics; | ||
|
||
// Gets the stream of the Image URL. | ||
Stream receiveStream; | ||
if (url.ImageURL != null) | ||
{ | ||
receiveStream = DownloadImage(url.ImageURL); | ||
} | ||
else | ||
{ | ||
/* Below URL act as a default URL if TextBox is empty. It can be modified */ | ||
receiveStream = DownloadImage("https://www.syncfusion.com/blogs/wp-content/uploads/2023/11/NET-MAUI.png"); | ||
} | ||
|
||
//Load the image | ||
PdfBitmap image = new PdfBitmap(receiveStream); | ||
|
||
// Define the position and size for the image | ||
float x = 570; | ||
float y = 330; | ||
float width = 235; | ||
float height = 245; | ||
|
||
// Draw the image on the page | ||
page.Graphics.DrawImage(image, x, y, width, height); | ||
|
||
// Define the URL for the hyperlink | ||
|
||
string AdURL = string.Empty; | ||
|
||
if(url.AdURL != null) | ||
{ | ||
AdURL = url.AdURL; | ||
} | ||
else | ||
{ | ||
/* Below URL act as a default URL if AdURL TextBox is empty. It can be modified */ | ||
AdURL = "https://www.syncfusion.com/downloads/maui"; | ||
} | ||
|
||
// Create a hyperlink annotation | ||
PdfUriAnnotation hyperlink = new PdfUriAnnotation(new RectangleF(x, y, width, height), AdURL); | ||
hyperlink.Border = new PdfAnnotationBorder(0); // Set border to 0 for invisible border | ||
|
||
// Add the hyperlink annotation to the page | ||
page.Annotations.Add(hyperlink); | ||
|
||
//Creating the stream object | ||
stream = new MemoryStream(); | ||
//Save the document as stream | ||
doc.Save(stream); | ||
//Close the document | ||
doc.Close(true); | ||
return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "HTML-to-PDF.pdf"); | ||
} | ||
private static PdfPageTemplateElement AddHeader(SizeF pdfPageSize, string headerText) | ||
{ | ||
//Create PDF page template element for header with bounds. | ||
PdfPageTemplateElement header = new PdfPageTemplateElement(new RectangleF(0, 0, pdfPageSize.Height, 50)); | ||
//Create font and brush for header element. | ||
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 8); | ||
var color = Color.FromArgb(179, 179, 179); | ||
PdfBrush brush = new PdfSolidBrush(color); | ||
if(headerText == string.Empty) | ||
{ | ||
headerText = "Syncfusion Blog"; | ||
} | ||
//Draw the header string in header template element. | ||
header.Graphics.DrawString(headerText, font, brush, new PointF(200, 20)); | ||
|
||
return header; | ||
} | ||
|
||
private static PdfPageTemplateElement AddFooter(SizeF pdfPageSize) | ||
{ | ||
//Create PDF page template element for footer with bounds. | ||
PdfPageTemplateElement footer = new PdfPageTemplateElement(new RectangleF(0, 0, pdfPageSize.Height, 50)); | ||
//Create font and brush for header element. | ||
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 8); | ||
//Create page number field. | ||
PdfPageNumberField pageNumber = new PdfPageNumberField(font, PdfBrushes.Black); | ||
//Create page count field. | ||
PdfPageCountField count = new PdfPageCountField(font, PdfBrushes.Black); | ||
var color = Color.FromArgb(179, 179, 179); | ||
PdfBrush brush = new PdfSolidBrush(color); | ||
//Add the fields in composite fields. | ||
PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count); | ||
//Draw the composite field in footer | ||
compositeField.Draw(footer.Graphics, new PointF(250, 20)); | ||
return footer; | ||
} | ||
public static Stream DownloadImage(string url) | ||
{ | ||
using (WebClient client = new()) | ||
{ | ||
byte[] imageBytes = client.DownloadData(url); | ||
return new MemoryStream(imageBytes); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="25.2.6" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<NameOfLastUsedPublishProfile>C:\Users\SivaramGunabalan\Downloads\HTMLToPDF_WebApplication_%281%29-992781036\HTMLToPDF_WebApplication\Properties\PublishProfiles\HTMLToPDFWebApplication20240508143652 - Zip Deploy.pubxml</NameOfLastUsedPublishProfile> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.9.34622.214 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HTMLToPDF_WebApplication", "HTMLToPDF_WebApplication.csproj", "{8561E39B-8497-4255-8C5D-320D49E81DD8}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
Release-Xml|Any CPU = Release-Xml|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{8561E39B-8497-4255-8C5D-320D49E81DD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{8561E39B-8497-4255-8C5D-320D49E81DD8}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{8561E39B-8497-4255-8C5D-320D49E81DD8}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{8561E39B-8497-4255-8C5D-320D49E81DD8}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{8561E39B-8497-4255-8C5D-320D49E81DD8}.Release-Xml|Any CPU.ActiveCfg = Release|Any CPU | ||
{8561E39B-8497-4255-8C5D-320D49E81DD8}.Release-Xml|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {6DE2CB99-5F1C-48F9-B7F7-FC235D336C47} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace HTMLToPDF_WebApplication.Models | ||
{ | ||
public class ErrorViewModel | ||
{ | ||
public string? RequestId { get; set; } | ||
|
||
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace HTMLToPDF_WebApplication.Models | ||
{ | ||
public class URL | ||
{ | ||
[Required] | ||
public string AdURL { get; set; } | ||
[Required] | ||
public string ImageURL { get; set; } | ||
[Required] | ||
public string HeaderText { get; set; } | ||
[Required] | ||
public string BlogLink { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
namespace HTMLToPDF_WebApplication | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(""); | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
builder.Services.AddControllersWithViews(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (!app.Environment.IsDevelopment()) | ||
{ | ||
app.UseExceptionHandler("/Home/Error"); | ||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. | ||
app.UseHsts(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
app.UseStaticFiles(); | ||
|
||
app.UseRouting(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.MapControllerRoute( | ||
name: "default", | ||
pattern: "{controller=Home}/{action=Index}/{id?}"); | ||
|
||
app.Run(); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
Properties/PublishProfiles/HTMLToPDFWebApplication20240508130203 - Zip Deploy.pubxml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
This file is used by the publish/package process of your Web project. You can customize the behavior of this process | ||
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. | ||
--> | ||
<Project> | ||
<PropertyGroup> | ||
<WebPublishMethod>ZipDeploy</WebPublishMethod> | ||
<IsLinux>true</IsLinux> | ||
<ResourceId>/subscriptions/01133bdd-20a6-4231-88bc-8d6b8c7cad41/resourcegroups/AzureCloudServiceBlink/providers/Microsoft.Web/sites/HTMLToPDFWebApplication20240508130203</ResourceId> | ||
<ResourceGroup>AzureCloudServiceBlink</ResourceGroup> | ||
<LaunchSiteAfterPublish>true</LaunchSiteAfterPublish> | ||
<SiteUrlToLaunchAfterPublish>https://htmltopdfwebapplication20240508130203.azurewebsites.net</SiteUrlToLaunchAfterPublish> | ||
<PublishProvider>AzureWebSite</PublishProvider> | ||
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration> | ||
<LastUsedPlatform>Any CPU</LastUsedPlatform> | ||
<ProjectGuid>8561e39b-8497-4255-8c5d-320d49e81dd8</ProjectGuid> | ||
<PublishUrl>https://htmltopdfwebapplication20240508130203.scm.azurewebsites.net/</PublishUrl> | ||
<UserName>$HTMLToPDFWebApplication20240508130203</UserName> | ||
<_SavePWD>true</_SavePWD> | ||
</PropertyGroup> | ||
</Project> |
13 changes: 13 additions & 0 deletions
13
Properties/PublishProfiles/HTMLToPDFWebApplication20240508130203 - Zip Deploy.pubxml.user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
This file is used by the publish/package process of your Web project. You can customize the behavior of this process | ||
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. | ||
--> | ||
<Project> | ||
<PropertyGroup> | ||
<TimeStampOfAssociatedLegacyPublishXmlFile /> | ||
<EncryptedPassword>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAABtL2x4KpQ0qlWAi+B7dCvAAAAAACAAAAAAAQZgAAAAEAACAAAADclX4QWOdErZFqIr90t30hpogrWwIQjkWeljiDu7/4bQAAAAAOgAAAAAIAACAAAADm4qLnsPSDxyplcInxt6xf/JBjVTKAS4dAZ/FfOUAi4oAAAAD6sSBI+J3y5QmotQ4rkCF1UGUgHr3HjIoa+wuY5ebRpcUkSywS32VgzdBfaVIo++iuetqNvAl4guMaXfnTppTb7LqgIxHWE+PnPDDM7raUIGtW4ZadcKG1nUhDhe/L+GuZa4fCIASvR/hzxSPkmFZuaoekXLncTg8iRXuIHjRJiEAAAABiV9RU0oYDIdpv6zoZPTuaRp/4D2NWDwogzO3aBB7vxBe6ThsRkDE7+HtZJJl34kpAKGALDVL9qmFE20C1gLPa</EncryptedPassword> | ||
<History>True|2024-05-08T09:00:37.8630145Z;False|2024-05-08T14:20:39.6722340+05:30;True|2024-05-08T13:17:15.8991970+05:30;</History> | ||
<LastFailureDetails /> | ||
</PropertyGroup> | ||
</Project> |
22 changes: 22 additions & 0 deletions
22
Properties/PublishProfiles/HTMLToPDFWebApplication20240508143652 - Zip Deploy.pubxml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
This file is used by the publish/package process of your Web project. You can customize the behavior of this process | ||
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. | ||
--> | ||
<Project> | ||
<PropertyGroup> | ||
<WebPublishMethod>WebDeploy</WebPublishMethod> | ||
<IsLinux>true</IsLinux> | ||
<ResourceId>/subscriptions/01133bdd-20a6-4231-88bc-8d6b8c7cad41/resourcegroups/AzureCloudServiceBlink/providers/Microsoft.Web/sites/HTMLToPDFWebApplication20240508143652</ResourceId> | ||
<ResourceGroup>AzureCloudServiceBlink</ResourceGroup> | ||
<LaunchSiteAfterPublish>true</LaunchSiteAfterPublish> | ||
<SiteUrlToLaunchAfterPublish>https://htmltopdfwebapplication20240508143652.azurewebsites.net</SiteUrlToLaunchAfterPublish> | ||
<PublishProvider>AzureWebSite</PublishProvider> | ||
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration> | ||
<LastUsedPlatform>Any CPU</LastUsedPlatform> | ||
<ProjectGuid>8561e39b-8497-4255-8c5d-320d49e81dd8</ProjectGuid> | ||
<PublishUrl>https://htmltopdfwebapplication20240508143652.scm.azurewebsites.net/</PublishUrl> | ||
<UserName>$HTMLToPDFWebApplication20240508143652</UserName> | ||
<_SavePWD>true</_SavePWD> | ||
</PropertyGroup> | ||
</Project> |
13 changes: 13 additions & 0 deletions
13
Properties/PublishProfiles/HTMLToPDFWebApplication20240508143652 - Zip Deploy.pubxml.user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
This file is used by the publish/package process of your Web project. You can customize the behavior of this process | ||
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. | ||
--> | ||
<Project> | ||
<PropertyGroup> | ||
<TimeStampOfAssociatedLegacyPublishXmlFile /> | ||
<EncryptedPassword>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAABtL2x4KpQ0qlWAi+B7dCvAAAAAACAAAAAAAQZgAAAAEAACAAAADvNe2aOMC2C0V/amYhTL8sGy8vwCo3h30RhqjnU4NTLwAAAAAOgAAAAAIAACAAAADJXi3WjjVOnIVCP4SafbH8o2wSUScTGu0IJ0cx2Yvqj4AAAACz3zHDbyEmjUnB/TNqnblMVzZsiqABb8pKAe/nnnXIggJI0r9ufCe//fTnQVq9P+vmONuJHw8bO8F84tF2FCEPpUlKgETaDZHmSZXkbkjC0EW0YOsMymUp2lJhTRPbJyygBwqlFpmgZVN/8OXxMHXLZ/+6vBRnQNouU7EUoeShSkAAAADr8EXJ99hlr/ky0yJGWGh1s7CB5Osn/CkTVCne3gVsy5DjH8ZScNgoTFY3P4WDUJr9WsBaSFp6Nvwr/3DbKm5j</EncryptedPassword> | ||
<History>True|2024-05-08T09:12:03.4702809Z;</History> | ||
<LastFailureDetails /> | ||
</PropertyGroup> | ||
</Project> |
Oops, something went wrong.