GcHTML is a package available with GcPdf and GcImaging) products. This allows the conversion of HTML content to PDFs and Images. In this video, we’ll convert an HTML file to PDF using GcPdf.
First, we’ll create .NET Core console application.
Add the HTML file to your project.
Right-click ‘Dependencies’ and select ‘Add Packages.’
In nuget.org, search for GrapeCity.Documents.Pdf -> GrapeCity.Documents.Html.packages.
Depending on your Operating System, you may need to add a relevant package to your project. Since this project is being created on macOS, we will choose the GcHtml package for Mac.
Add the packages to your project.
Add necessary namespaces to the project.
Create GcPdfDocument object and add new page to it. Also, initialize page graphics.
Call the DrawHtml method that reads the html content from the invoice.html file, and draws at a specific position on the PDF graphics. The HtmlToPdfFormat attribute will set the default values for rendering the HTML content with default page size and background color.
Save the document to PDF file and run the application. You have just converted HTML file to PDF document.
Create a variable that holds the HTML content.
Call the DrawHtml method that renders the HTML string at a specific position on the PDF graphics. The HtmlToPdfFormat attribute will set the default values for rendering the HTML content with a default page size and background color.
You can set the MaxWidth of the page to a required size. You can draw a rounded rectangle around this string for visual effects.
Save the document to PDF and run the application. Your HTML string renders on a PDF Document.
Create a variable uri that holds the uri to the web page.
Create a variable for GcHtmlRenderer and pass the URI into the constructor.
Initialize the PDF Settings instance that holds the settings for the generated PDF.
Call the RenderToPdf method of the GcHtmlRenderer and pass the PDF File and the PDF settings that will apply on the PDF File.
Run the application and you will see entire web page convert into PDF without loss of quality.
What do you think about the new features? Leave a comment below.