Skip to content

Commit

Permalink
Merge pull request #54 from SyncfusionExamples/848818-html-sample
Browse files Browse the repository at this point in the history
848818 -Added the HTML to PDF sample
  • Loading branch information
SowmiyaLoganathan authored Sep 27, 2023
2 parents 160e728 + 3033907 commit 4a14f00
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32819.101
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Inject_custom_CSS_to_HTML", "Inject_custom_CSS_to_HTML\Inject_custom_CSS_to_HTML.csproj", "{A00BDA8D-F1AB-4CB0-B97D-9A80631F6BB4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A00BDA8D-F1AB-4CB0-B97D-9A80631F6BB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A00BDA8D-F1AB-4CB0-B97D-9A80631F6BB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A00BDA8D-F1AB-4CB0-B97D-9A80631F6BB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A00BDA8D-F1AB-4CB0-B97D-9A80631F6BB4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FC291101-A10D-4F23-8A31-BDC2251AE445}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="23.1.38" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// See https://aka.ms/new-console-template for more information

using Syncfusion.Drawing;
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Set the Blink viewport size.
blinkConverterSettings.ViewPortSize = new Size(1280, 0);
blinkConverterSettings.Margin.All = 30;
//Set the custom CSS
blinkConverterSettings.Css = "body {\r\n background-color: lightyellow; \r\n}";
htmlConverter.ConverterSettings = blinkConverterSettings;
//Convert the URL to PDF document.
PdfDocument document = htmlConverter.Convert("https://www.google.com");

//Create file stream.
FileStream fileStream = new FileStream("../../../HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32819.101
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Inject_custom_JavaScript_to_HTML", "Inject_custom_JavaScript_to_HTML\Inject_custom_JavaScript_to_HTML.csproj", "{634C42CC-B7A6-406F-AC6B-50E5D9FCEF7C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{634C42CC-B7A6-406F-AC6B-50E5D9FCEF7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{634C42CC-B7A6-406F-AC6B-50E5D9FCEF7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{634C42CC-B7A6-406F-AC6B-50E5D9FCEF7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{634C42CC-B7A6-406F-AC6B-50E5D9FCEF7C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CF5A3C10-D91E-4031-83C3-D3D28685BCEB}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="23.1.38" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// See https://aka.ms/new-console-template for more information

using Syncfusion.Drawing;
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using System.IO;

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

//Initialize the blink converter settings.
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Set the Blink viewport size.
blinkConverterSettings.ViewPortSize = new Size(1280, 0);
blinkConverterSettings.Margin.All = 30;

//Set the Custom JavaScript
blinkConverterSettings.JavaScript = "document.querySelectorAll('img').forEach((node)=>{node.remove();})";
htmlConverter.ConverterSettings = blinkConverterSettings;

//Convert the URL to PDF document.
PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");

//Create a filestream.
FileStream fileStream = new FileStream("../../../HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32819.101
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Optimize-HTML-to-PDF-performance", "Optimize-HTML-to-PDF-performance\Optimize-HTML-to-PDF-performance.csproj", "{4613D841-222D-4A43-A27F-AA8C29A6F195}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4613D841-222D-4A43-A27F-AA8C29A6F195}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4613D841-222D-4A43-A27F-AA8C29A6F195}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4613D841-222D-4A43-A27F-AA8C29A6F195}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4613D841-222D-4A43-A27F-AA8C29A6F195}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4F1700C7-9DA0-47F7-94D0-7C97A709821A}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Optimize_HTML_to_PDF_performance</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="23.1.38" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// See https://aka.ms/new-console-template for more information

using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using System.IO;

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Reuse the browser instance.
htmlConverter.ReuseBrowserProcess = true;

//Create PDF document.
PdfDocument document = new PdfDocument();
//Create memory stream.
MemoryStream memoryStream = new MemoryStream();

for (int i = 0; i < 10; i++)
{
//Initialize the blink converter settings.
BlinkConverterSettings settings = new BlinkConverterSettings();
settings.AdditionalDelay = 1000;
settings.EnableJavaScript = true;
//Assign the settings to HTML converter.
htmlConverter.ConverterSettings = settings;

//Convert the URL to PDF document.
document = htmlConverter.Convert("https://www.google.com");

//Save and close the PDF document.
document.Save(memoryStream);
document.Close(true);
File.WriteAllBytes("ReuseBrowserProcess" + Guid.NewGuid().ToString() + ".pdf", memoryStream.ToArray());
}

//Close HTML converter.
htmlConverter.Close();
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32819.101
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Set_blink_path_in_HTML_to_PDF", "Set_blink_path_in_HTML_to_PDF\Set_blink_path_in_HTML_to_PDF.csproj", "{DCE10516-E8CB-4FE2-9AF2-6AFF2DE04026}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DCE10516-E8CB-4FE2-9AF2-6AFF2DE04026}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DCE10516-E8CB-4FE2-9AF2-6AFF2DE04026}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DCE10516-E8CB-4FE2-9AF2-6AFF2DE04026}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DCE10516-E8CB-4FE2-9AF2-6AFF2DE04026}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A2535A2F-9E26-4961-88B6-733DDA34D313}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// See https://aka.ms/new-console-template for more information

using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;

//Initialize HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Set Blink binaries path.
blinkConverterSettings.BlinkPath = @"../../../BlinkBinaries/";
//Assign Blink converter settings to HTML converter.
htmlConverter.ConverterSettings = blinkConverterSettings;
//Convert URL to PDF document.
PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");

//Create file stream to save the PDF document.
FileStream fileStream = new FileStream("../../../HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="23.1.38" />
</ItemGroup>

</Project>

0 comments on commit 4a14f00

Please sign in to comment.