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.

Watch the video demo:

Convert an HTML file to PDF

  1. First, we’ll create .NET Core console application.

  2. Add the HTML file to your project.

  3. Right-click ‘Dependencies’ and select ‘Add Packages.’

  4. 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.

  1. Add the packages to your project.

  2. Add necessary namespaces to the project.

  3. Create GcPdfDocument object and add new page to it. Also, initialize page graphics.

  4. 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.

  5. Save the document to PDF file and run the application. You have just converted HTML file to PDF document.

Add an HTML string to a PDF

  1. Create a variable that holds the HTML content.

  2. 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.

  3. You can set the MaxWidth of the page to a required size. You can draw a rounded rectangle around this string for visual effects.

  4. Save the document to PDF and run the application. Your HTML string renders on a PDF Document.

Render an entire web page to a PDF (without loss of quality)

  1. Create a variable uri that holds the uri to the web page.

  2. Create a variable for GcHtmlRenderer and pass the URI into the constructor.

  3. Initialize the PDF Settings instance that holds the settings for the generated PDF.

  4. Call the RenderToPdf method of the GcHtmlRenderer and pass the PDF File and the PDF settings that will apply on the PDF File.

  5. 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.

Try GrapeCity Documents for PDF free for 30 days

Download the latest version of GrapeCity Documents for PDF

Download Now!