Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logo update in the HTML to PDF converter Blog tool #3

Merged
merged 9 commits into from
Sep 23, 2024
310 changes: 207 additions & 103 deletions Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
using System.ComponentModel;
using System;
using System.Diagnostics;
using System.Net;
using System.Text;
Expand Down Expand Up @@ -38,160 +40,262 @@ public IActionResult Error()
}
public ActionResult ExportToPDF(URL url)
{
//Initialize HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
if (url.BlogLink != null)
{
//Initialize HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

//Initialize Blink converter settings.
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Initialize Blink converter settings.
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();

//Enable lazy load images.
blinkConverterSettings.EnableLazyLoadImages = true;
//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 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 Scale of the PDF document.
blinkConverterSettings.Scale = 1.0f;

//Set the margin of the PDF document.
blinkConverterSettings.Margin.All = 0;
//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()";
//Set the JavaScript to remove the unwanted elements from the HTML page.
var elementsToRemove = new[]
{
"scroll-progress-bar-container",
"detail-page-secondary-header",
"post-tags-section",
"cookie",
"wpfront-scroll-top",
"top-section",
"main-menu-section",
"subscription-section",
"toc-section",
"category-ad-section",
"comments-section",
"main-footer-policy",
"main-footer-desktop",
"boldchat-host"
};

//Assign the header element to PdfHeader of Blink converter settings.
blinkConverterSettings.PdfHeader = AddHeader(blinkConverterSettings.PdfPageSize, url.HeaderText);
var scriptBuilder = new StringBuilder();
foreach (var element in elementsToRemove)
{
scriptBuilder.AppendLine($"document.getElementById(\"{element}\").remove();");
}

//Assign the footer element to PdfFooter of Blink converter settings.
blinkConverterSettings.PdfFooter = AddFooter(blinkConverterSettings.PdfPageSize);
blinkConverterSettings.JavaScript = scriptBuilder.ToString();

blinkConverterSettings.AdditionalDelay = 20000;
//Assign the header element to PdfHeader of Blink converter settings.
blinkConverterSettings.PdfHeader = AddHeader(blinkConverterSettings.PdfPageSize, url.HeaderText);

//Assign the Blink converter settings to HTML converter.
htmlConverter.ConverterSettings = blinkConverterSettings;
//Assign the footer element to PdfFooter of Blink converter settings.
blinkConverterSettings.PdfFooter = AddFooter(blinkConverterSettings.PdfPageSize, url.BlogLink);

if(url.BlogLink == string.Empty)
{
throw new PdfException("Blog link is empty. Kindly add Blog link in the text box before performing conversion");
}
blinkConverterSettings.AdditionalDelay = 20000;

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;
//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 = 170;
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);

// Hex color code for hyperlink color
var color = ConvertHexToRGB("0057FF");
PdfBrush brush = new PdfSolidBrush(color);

for (int i = 0; i < doc.Pages.Count; i++)
{
PdfTextWebLink weblink = new PdfTextWebLink();
weblink.Text = " View blog Link";
weblink.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 8, PdfFontStyle.Bold);
weblink.Brush = brush;
weblink.Url = url.BlogLink;
weblink.DrawTextWebLink(doc.Pages[i].Graphics, new PointF(250, 570));
}

//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");
}
else
{
/* Below URL act as a default URL if AdURL TextBox is empty. It can be modified */
AdURL = "https://www.syncfusion.com/downloads/maui";
return View();
}

// 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));
PdfPageTemplateElement header = new PdfPageTemplateElement(new RectangleF(0, 0, pdfPageSize.Height, 30));
//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)
if (headerText == string.Empty)
{
headerText = "Syncfusion Blog";
}

float x = (pdfPageSize.Height / 2) - (font.MeasureString(headerText).Width / 2);
float y = 15 - (font.Height / 2);

// Hex color code for header background color
var color = ConvertHexToRGB("FFFFFF");
PdfBrush brush = new PdfSolidBrush(color);

header.Graphics.DrawRectangle(brush, new RectangleF(0, 0, pdfPageSize.Height, 30));

// Hex color code for header Background stoke color
color = ConvertHexToRGB("E5EDF3");
brush = new PdfSolidBrush(color);

header.Graphics.DrawLine(new PdfPen(color),new PointF(0,30),new PointF(pdfPageSize.Height,30));

//Hex color code for header text color
color = ConvertHexToRGB("475569");
brush = new PdfSolidBrush(color);

//Draw the header string in header template element.
header.Graphics.DrawString(headerText, font, brush, new PointF(200, 20));
header.Graphics.DrawString(headerText, font, brush, new PointF(x, y));

return header;
}

private static PdfPageTemplateElement AddFooter(SizeF pdfPageSize)
private static PdfPageTemplateElement AddFooter(SizeF pdfPageSize, string url)
{
//Create PDF page template element for footer with bounds.
PdfPageTemplateElement footer = new PdfPageTemplateElement(new RectangleF(0, 0, pdfPageSize.Height, 50));
PdfPageTemplateElement footer = new PdfPageTemplateElement(new RectangleF(0, 0, pdfPageSize.Height, 60));
//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);

// Hex color code for footer CompositeField text color
var color = ConvertHexToRGB("475569");
PdfBrush brush = new PdfSolidBrush(color);
//Add the fields in composite fields.
PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count);

float x = pdfPageSize.Height - (font.MeasureString("Page {99} of {99}").Width + 20);
float y = 35;


// Hex color code for footer background color
color = ConvertHexToRGB("FAFBFF");
brush = new PdfSolidBrush(color);

footer.Graphics.DrawRectangle(brush, new RectangleF(0, 0, pdfPageSize.Height,60));

// Hex color code for footer background stoke color
color = ConvertHexToRGB("E5EDF3");
brush = new PdfSolidBrush(color);

footer.Graphics.DrawLine(new PdfPen(color), new PointF(0, 0), new PointF(pdfPageSize.Height, 0));

//Draw the composite field in footer
compositeField.Draw(footer.Graphics, new PointF(250, 20));
compositeField.Draw(footer.Graphics, new PointF(x, y));

FileStream logoImage = new FileStream("wwwroot/images/logo.png", FileMode.Open, FileAccess.Read);

//Draw the logo
PdfBitmap logo = new PdfBitmap(logoImage);
//Draw the logo on the footer
footer.Graphics.DrawImage(logo, new RectangleF(20, 0, 75, 40));

//Hex color code for footer text color
color = ConvertHexToRGB("475569");
brush = new PdfSolidBrush(color);

footer.Graphics.DrawString("Copyright 2001 - Present. Syncfusion, Inc. All Rights Reserved. |", font, brush, new PointF(20, 35));
return footer;
}
public static Stream DownloadImage(string url)
{
using (WebClient client = new())
{
// Set the User-Agent header to mimic a request from a browser
client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
// Set the User-Agent header to mimic a request from a browser
client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
byte[] imageBytes = client.DownloadData(url);
return new MemoryStream(imageBytes);
}
}
public static Color ConvertHexToRGB(string hexColor)
{
// Convert hex to integer values for red, green, and blue
int r = int.Parse(hexColor.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
int g = int.Parse(hexColor.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
int b = int.Parse(hexColor.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
return Color.FromArgb(r, g, b);
}
}
}
Binary file added wwwroot/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading