Documents for PDF, .NET Edition Documentation
In This Topic
    Render HTML to PDF
    In This Topic

    GcPdf library along with GcHtml library, lets you easily render HTML content to PDF document. With a utility library like GcHtml, you can convert HTML files like invoices and reports to PDF documents and print them without worrying about disarranged layouts, styles or formats. You can also add HTML content to PDF documents.

    GcHtml is based on the industry standard Chrome web browser engine working in headless mode, offering advantage of rendering HTML to PDF on any platform - Windows, Linux and macOS. Currently, GcHtml works only on Intel-based 64-bit processor architecture. It doesn’t matter whether your .NET application is built for x64, x86 or AnyCPU platform target. The browser is always working in a separate process.

    The GcHtml library consists of platform dependent and platform independent NuGet packages. You can add a platform-dependent Html package depending on the platform you are working. Alternatively, you can add all the Html packages and let GcHtml automatically select the correct package at runtime.

    GcHtml NuGet Packages

    Description

    GrapeCity.Documents.Html

    The GrapeCity.Documents.Html package contains the following namespaces:

    GrapeCity.Documents.Html.Windows.X64 It is the interface unit and Chromium browser engine for 64-bit Windows platform.

    GrapeCity.Documents.Html.Mac.X64

    It is the interface unit and browser engine for macOS platform.

    GrapeCity.Documents.Html.Linux.X64

    It is the interface unit and browser engine for 64-bit Linux platform.

    Note that to use GcHtml on Linux, Chromium dependencies must be installed. The following command installs the necessary packages on Ubuntu:

    Copy Code
    sudo apt-get update
    sudo apt-get install libxss1 libappindicator1 libindicator7 libnss3-dev
    

    Install GcHtml Package

    Refer the steps below to install GcHtml package in your project:

    1. Open Visual Studio and create a .Net Core Console application.

    2. Right-click Dependencies and select Manage NuGet Packages.

    3. With the Package source" set to Nuget website, search for GrapeCity.Documents.Pdf under the Browse tab and click Install.

    4. Similarly, install GrapeCity.Documents.Html and GrapeCity.Documents.Html.Windows.X64 (depending on the platform you are working).

      Note: During installation, you’ll receive two confirmation dialogs. Click OK in the Preview Changes dialog box and click I Agree in the License Acceptance dialog box to proceed installation.
    5. Once, the GcHtml package has been installed successfully, add the namespace in Program.cs file.

      C#
      Copy Code
      using GrapeCity.Documents.Html;
      using GrapeCity.Documents.Pdf;
      using GrapeCity.Documents.Drawing;
      
    6. Write the sample code.

    Render HTML Webpage to PDF Document

    GcHtml can render HTML webpage to PDF document. GcPdf provides the PdfSettings class and RenderToPdf method of GcHtmlRenderer class to render HTML files to PDF documents.

    To render the HTML webpage to a PDF document, follow the steps below:

    1. Specify the PDF file path for rendering HTML webpage.
    2. Specify the HTML source (URI).
    3. Define the PDF document settings using the PdfSettings class.
    4. Convert the HTML webpage to PDF file using RenderToPdf method of GcHtmlRenderer class.
    C#
    Copy Code
            //This method demonstrates how to render a web page to a PDF file
            public void RenderWebpageToPdf()
            {
                // Specify the PDF file path to which the HTML webpage should be rendered
                var file = @"testPage.pdf";
    
                //Specify the HTML source
                var uri = new Uri(@"https://www.grapecity.com/controls/documents-pdf/whats-new");
    
                using (var re = new GcHtmlRenderer(uri))
                {
                    //Define parameters for the PDF generator
                    var pdfSettings = new PdfSettings()
                    {
                        // Skip the first page which is basically empty
                        PageRanges = "2-100",
                        // Sets the page width in inches
                        PageWidth = 8.5f,
                        // Sets the page height in inches
                        PageHeight = 11f,
                        // Sets page margins all around (default is no margins)
                        Margins = new Margins(0.5f),
                        // Ignores the page size defined by CSS
                        IgnoreCSSPageSize = true,
                        // Use landscape orientation to make sure long code lines are not truncated
                        Landscape = true,
                        // Sets the background color of the HTML page
                        DefaultBackgroundColor = Color.Azure,
                        // Sets the scale of the webpage 
                        Scale = 1.1f,
                        // Displays headers and footers on each page
                        DisplayHeaderFooter = true,
                        // Specify custom header
                        HeaderTemplate = 
    "<div style=\"font-size:12em;width:1000px;margin-left:30px;margin-right:30px\">" +
    "<span style=\"float:left\">Page <span class=\"pageNumber\"></span> of <span class=\"totalPages\"></span></span>" +
    "<span style=\"float:right\"><span class=\"date\"></span></span>" +
    "</div>"
                    };
    
                    // Create a PDF file from the source HTML
                    re.RenderToPdf(file, pdfSettings);
                }
            }
    

    Back to Top

    The snapshot of the resulting PDF document is depicted below:

    Note: In order to render an HTML page to PDF document, the fonts used on that page should be already installed on your system.

    Render HTML String to PDF Document

    GcHtml can render an HTML string to PDF document.

    To render the HTML string to a PDF document, follow the steps below:

    1. Specify the PDF file path for rendering HTML string.
    2. Specify the HTML string.
    3. Define the PDF document settings using the PdfSettings class.
    4. Convert the HTML string to PDF file using the RenderToPdf method of GcHtmlRenderer class.
    C#
    Copy Code
            //This method demonstrates how to render a web page to a PDF file
            public void RenderHTMLStringToPdf()
            {
                //Specify the PDF file path to which the HTML webpage should be rendered
                var file = @"testString.pdf";
    
                //Specify the HTML source
                var htmlString = 
    "<html><body><p>I am normal</p><p style = 'color:red;'> I am red</p><p style = 'color:blue;'>I am blue</p>" +
    "<p style = 'font-size:50px;'></p></body></html>";
    
                using (var re = new GcHtmlRenderer(htmlString))
                {
                    //Define parameters for the PDF generator
                    var pdfSettings = new PdfSettings()
                    {
                        // Skip the first page which is basically empty
                        PageRanges = "2-100",
                        // Sets the page width in inches
                        PageWidth = 8.5f,
                        // Sets the page height in inches
                        PageHeight = 11f,
                        // Sets page margins all around (default is no margins)
                        Margins = new Margins(0.5f),
                        // Ignores the page size defined by CSS
                        IgnoreCSSPageSize = true,
                        // Use landscape orientation to make sure long code lines are not truncated
                        Landscape = true,
                        //Sets the background color of the HTML page
                        DefaultBackgroundColor = Color.Azure,               
                    };
    
                    //Create a PDF file from the source HTML
                    re.RenderToPdf(file, pdfSettings);
                }
            }
    

    Back to Top

    The snapshot of the resulting PDF document is depicted below:

    Render HTML Table to PDF Document

    GcHtml can render an HTML table to PDF document.

    To render the HTML table to a PDF document, follow the steps below:

    1. Specify the PDF file path for rendering HTML table.
    2. Create the HTML table.
    3. Define the PDF document settings using the PdfSettings class.
    4. Convert the HTML table to PDF file using the RenderToPdf method of GcHtmlRenderer class.
    C#
    Copy Code
    public void RenderTable_ToPdf()
    {
        //Specify the PDF file path to which the HTML webpage should be rendered
        var file = @"testTable.pdf";
    
        //Create HTML Table
        var htmlTable = "<table border='1'><tr><th>Items</th><th>Quantity</th>" +
              "</tr><tr><td>Laptops</td><td>30</td></tr><tr><td>Chromebooks</td>" +
              "<td>12</td></tr></table>";
    
        using (var re = new GcHtmlRenderer(htmlTable))
        {
            //Define parameters for the PDF generator
            var pdfSettings = new PdfSettings()
            {
                // Sets the page width in inches
                PageWidth = 8.5f,
                // Sets the page height in inches
                PageHeight = 11f,
                // Sets page margins all around (default is no margins)
                Margins = new Margins(0.5f),
                // Ignores the page size defined by CSS 
                IgnoreCSSPageSize = true,
                // Use landscape orientation to make sure long code lines are not truncated
                Landscape = true,
                //Sets the background color of the HTML page 
                DefaultBackgroundColor = Color.Azure,               
            };
    
            //Create a PDF file from the source HTML
            re.RenderToPdf(file, pdfSettings);
        }
    }
    

    Back to Top

    The snapshot of the resulting PDF document is depicted below:

    Add HTML Content to PDF Document

    You can add an HTML page or string to a PDF document using the DrawHtml method of GcPdfGraphicsExt class.

    To add an HTML formatted string to a PDF document, follow the steps below:

    1. Create an instance of GcPdfDocument class.
    2. Configure the PDF settings using the HtmlToPdfFormat class.
    3. Add an HTML string to a PDF document using the DrawHtml method of GcPdfGraphicsExt class. You can also add a webpage to the PDF document in this manner. This is shown in the code snippet.
    4. Save the PDF file using the Save method of GcPdfDocument class.
    C#
    Copy Code
    //This method demonstrates how to add an HTML formatted string to a PDF file
    public void DrawHtmlToPdf()
    {
    
        //Configure PDF document
        var doc = new GcPdfDocument();
        var page = doc.Pages.Add();
        var g = page.Graphics;
    
        //Configure PDF settings
        HtmlToPdfFormat pdfFormat = new HtmlToPdfFormat(false);
        pdfFormat.DefaultBackgroundColor = Color.White;
        pdfFormat.MaxPageHeight = 8;
        pdfFormat.MaxPageWidth = 8;
    
        //Draw HTML string
        GcPdfGraphicsExt.DrawHtml(g, new Uri("https://www.amazon.com/"), 10, 30, pdfFormat, out SizeF size);
        //GcPdfGraphicsExt.DrawHtml(g, "Hello, <b>World!</b>", 72, 72, pdfFormat, out SizeF size);
    
        //Save the document
        doc.Save("DrawHtmlToPdf.pdf");
    }
    

    Back to Top

    The resulting image is shown below:

     

    For more information on rendering HTML to PDF using GcPdf, see GcPdf sample browser.