Skip to content

Commit

Permalink
Update HomeController.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
chinnumuniyappan committed Sep 4, 2024
1 parent 990f057 commit 783edda
Showing 1 changed file with 147 additions and 113 deletions.
260 changes: 147 additions & 113 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,119 +40,150 @@ public IActionResult Error()
}
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(\"main-footer-policy\").remove();\n document.getElementById(\"main-footer-desktop\").remove();\n document.getElementById(\"detail-page-secondary-header\").remove();\n document.getElementById(\"post-tags-section\").remove();\n document.getElementById(\"scroll-progress-bar-container\").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)
if (url.BlogLink != null)
{
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
//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.
var elementsToRemove = new[]
{
"scroll-progress-bar-container",
"detail-page-secondary-header",
"post-tags-section",
"cookie",
"liveChatApp",
"wpfront-scroll-top",
"top-section",
"main-menu-section",
"home-page-header",
"social-icon",
"subscription-section",
"toc-section",
"category-ad-section",
"comments-section",
"main-footer-policy",
"main-footer-desktop",
};

var scriptBuilder = new StringBuilder();
foreach (var element in elementsToRemove)
{
scriptBuilder.AppendLine($"document.getElementById(\"{element}\").remove();");
}

blinkConverterSettings.JavaScript = scriptBuilder.ToString();

//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, url.BlogLink);

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");
} 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);
Expand All @@ -161,18 +194,18 @@ private static PdfPageTemplateElement AddHeader(SizeF pdfPageSize, string header
}

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

//Draw the header string in header template element.
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, 70));
//Create font and brush for header element.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 8);
//Create page number field.
Expand All @@ -184,8 +217,8 @@ private static PdfPageTemplateElement AddFooter(SizeF pdfPageSize)
//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 + 46);
float y = 25 - (font.Height) / 2;
float x = pdfPageSize.Height - (font.MeasureString("Page {99} of {99}").Width + 20);
float y = 35 - (font.Height) / 2;

//Draw the composite field in footer
compositeField.Draw(footer.Graphics, new PointF(x, y));
Expand All @@ -196,8 +229,9 @@ private static PdfPageTemplateElement AddFooter(SizeF pdfPageSize)
PdfBitmap logo = new PdfBitmap(logoImage);

//Draw the logo on the footer
footer.Graphics.DrawImage(logo, new RectangleF(46, 0, 75, 40));
footer.Graphics.DrawString("Copyright 2001 - Present. Syncfusion, Inc. All Rights Reserved.", font, brush, new PointF(46, 35));
footer.Graphics.DrawImage(logo, new RectangleF(20, 0, 75, 40));
footer.Graphics.DrawString("Copyright 2001 - Present. Syncfusion, Inc. All Rights Reserved.", font, brush, new PointF(20, 35));
footer.Graphics.DrawString(url, font, PdfBrushes.Blue, new PointF(20, 50));
return footer;
}
public static Stream DownloadImage(string url)
Expand Down

0 comments on commit 783edda

Please sign in to comment.