Html To Pdf Basic

This example demonstrates how to convert HTML file/Url to a PDF file using simple settings. Click on About to see full description of the example.

 
Webpage URL or local file path:
Base URL:
Viewport
Width: px Height: px
PDF Page Settings
Size: Orientation:
Margins (left, top, right, bottom):  pt
Misc
Conversion delay:  ms Timeout:  ms

Our HTML to PDF Converter library adds the ability to convert HTML pages and text to PDF documents with as little as 5 lines of code. In this example we explore some basic options of the converter as follows:

You can choose the source of HTML by clicking on "Convert URL or local file" or "Convert HTML string":

  • Webpage URL or local file path: The URL to the webpage or local file to convert to PDF. If authentication is needed or you are behind a proxy server, you will need to set the converter proxy options as illustrated in our documentation.
  • HTML text: The HTML text to be converted to PDF. This conversion mode also lets you specify the Base URL for the HTML text. That Base URL is used to resolve the relative URLs found in the HTML text to absolute URLs. You can omit this parameter when your HTML text does not include any relative URLs.

Viewport Settings

  • Viewport Width: This is the width of the virtual browser window in pixels used in our converter. When you change the Viewport width, the content in the window may have different layout, resulting in a different PDF output.
  • Viewport Height: This is the height of the virtual browser window in pixels. When set to 0, the converter will use default value of 768px. If you set this value large enough the converter will capture the whole webpage. It can also be used to limit the part of the webpage you want to convert.

PDF Page Settings

  • PDF Page Size: This specifies the size of the page in the converted PDF document. In this example, we include several standard page sizes like A4, B3, Letter, etc. The page size can be changed to custom values in points (1 points = 1/72 inches).
  • Page Orientation: Orientation of PDF page can be set to Portrait or Landscape. By default the page orientation is Portrait

Misc Settings

  • Conversion Delay: This is the amount of time, in ms, the converter waits for the webpage to load before rendering it. There are several options for the conversion delay in WaitMode property including: Delay, DocumentReady and Manual. Delay mode lets you specify how long the converter waits before converting. DocumentReady mode instructs the converter to wait until the document is read. It's similar to $(document).ready() event in jQuery. Manual let you trigger the conversion manually by calling Javascript method named officePdfReady. You can specify whether to wait for all the conditions to meet before triggering the conversion or wait for one of them only in option UseOrOperatorInWaitMode.
  • Timeout: This is the amount of time, in ms, the converter waits for the page to load before returning timed out exception.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
#if !WEB
using System.Windows.Forms;
#else
using System.Web.UI;
using System.Web.UI.WebControls;
#endif
using OfficeComponent.HtmlToPdf;

namespace OfficeComponent.Samples
{
    partial class HtmlToPdfBasicExample : ExampleBase
#if WEB
        , IUIExample
#endif
    {
        // Options
        public bool ConvertUrl { get; set; }
        public string Url { get; set; }
        public string HtmlString { get; set; }
        public string HtmlStringBaseUrl { get; set; }

        public int Width { get; set; }
        public int Height { get; set; }
        public OfficeComponent.HtmlToPdf.PageSize PageSize { get; set; }
        public bool Portrait { get; set; }
        public OfficeComponent.HtmlToPdf.PageMargins Margin { get; set; }

        public int ConversionDelay { get; set; }
        public int Timeout { get; set; }

        public bool SinglePage { get; set; }

        public EventHandler Progress;

#if WEB
        public HtmlToPdfBasicExample() : base(null,null)
        {
        }
#endif

        public HtmlToPdfBasicExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {

        }

        public HtmlToPdfBasicExample(string commonDataPath, string outputDir, string xmlFile)
            : base(commonDataPath, outputDir, xmlFile)
        {

        }

        Action onComplete;
        Action onError;
        string outputPath;

        public void ExecuteAsync(Action onComplete, Action onError)
        {
            if (Width <= 0 || Height <= 0)
            {
                ShowError("Invalid Width or Height");
                onComplete(null);
                return;
            }

            if (ConversionDelay < 0)
            {
                ShowError("Invalid conversion delay");
                onComplete(null);
                return;
            }

            // Set output path.
            outputPath = Path.Combine(OutputDir, this.GetType().Name + ".pdf");
            if (File.Exists(outputPath))
                File.Delete(outputPath);

            // Create a new converter.
            var c = new HtmlToPdfConverter()
            {
                ViewportWidth = Width,
                ViewportHeight = Height,
                PageSize = PageSize,
                PageMargin = Margin,
                Portrait = Portrait,
                Delay = ConversionDelay,
                Timeout = Timeout,

                SinglePage = SinglePage
            };

            if (Progress != null)
                c.Progress += Progress;

            this.onComplete = onComplete;
            this.onError = onError;

            if (ConvertUrl)
            {
                if (string.IsNullOrWhiteSpace(Url))
                {
                    ShowError("Please specify the URL to convert.");
                    onComplete(null);
                    return;
                }

                // Convert URL
                c.ConvertAsync(Url, outputPath, OnConversionComplete);
            }
            else
            {
                if (string.IsNullOrWhiteSpace(HtmlString))
                {
                    ShowError("Please specify the HTML string to convert.");
                    onComplete(null);
                    return;
                }

                // Convert the specified HTML string.
                c.ConvertStringAsync(HtmlString, HtmlStringBaseUrl, outputPath, OnConversionComplete);
            }
        }

        void OnConversionComplete(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
                onError(e.Error);
            else
                onComplete(outputPath);
        }

        public override string Execute()
        {
            if (Width <= 0 || Height < 0)
            {
                ShowError("Invalid Width or Height");
                return null;
            }

            if (ConversionDelay < 0)
            {
                ShowError("Invalid conversion delay");
                return null;
            }

            // Set output path.
            var outputPath = Path.Combine(OutputDir, this.GetType().Name + ".pdf");
            if (File.Exists(outputPath))
                File.Delete(outputPath);

            // Create a new converter.
            var c = new HtmlToPdfConverter()
            {
                ViewportWidth = Width,
                ViewportHeight = Height,
                PageSize = PageSize,
                PageMargin = Margin,
                Portrait = Portrait,
                Delay = ConversionDelay,
                Timeout = Timeout,

                SinglePage = SinglePage
            };

            if (Progress != null)
                c.Progress += Progress;

            try
            {
                if (ConvertUrl)
                {
                    if (string.IsNullOrWhiteSpace(Url))
                    {
                        ShowError("Please specify the URL to convert.");
                        return null;
                    }

                    // Convert URL
                    c.Convert(Url, outputPath);
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(HtmlString))
                    {
                        ShowError("Please specify the HTML string to convert.");
                        return null;
                    }

                    // Convert the specified HTML string.
                    c.ConvertString(HtmlString, HtmlStringBaseUrl, outputPath);
                }
            }
            catch (Exception ex)
            {
                ShowError(ex.Message);
            }

            return outputPath;
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
#if !WEB
using System.Windows.Forms;
#else
using System.Web.UI;
using System.Web.UI.WebControls;
#endif
using OfficeComponent.HtmlToPdf;

namespace OfficeComponent.Samples
{
    partial class HtmlToPdfBasicExample : ExampleBase
#if WEB
        , IUIExample
#endif
    {
        // Options
        public bool ConvertUrl { get; set; }
        public string Url { get; set; }
        public string HtmlString { get; set; }
        public string HtmlStringBaseUrl { get; set; }

        public int Width { get; set; }
        public int Height { get; set; }
        public OfficeComponent.HtmlToPdf.PageSize PageSize { get; set; }
        public bool Portrait { get; set; }
        public OfficeComponent.HtmlToPdf.PageMargins Margin { get; set; }

        public int ConversionDelay { get; set; }
        public int Timeout { get; set; }

        public bool SinglePage { get; set; }

        public EventHandler Progress;

#if WEB
        public HtmlToPdfBasicExample() : base(null,null)
        {
        }
#endif

        public HtmlToPdfBasicExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {

        }

        public HtmlToPdfBasicExample(string commonDataPath, string outputDir, string xmlFile)
            : base(commonDataPath, outputDir, xmlFile)
        {

        }

        Action onComplete;
        Action onError;
        string outputPath;

        public void ExecuteAsync(Action onComplete, Action onError)
        {
            if (Width <= 0 || Height <= 0)
            {
                ShowError("Invalid Width or Height");
                onComplete(null);
                return;
            }

            if (ConversionDelay < 0)
            {
                ShowError("Invalid conversion delay");
                onComplete(null);
                return;
            }

            // Set output path.
            outputPath = Path.Combine(OutputDir, this.GetType().Name + ".pdf");
            if (File.Exists(outputPath))
                File.Delete(outputPath);

            // Create a new converter.
            var c = new HtmlToPdfConverter()
            {
                ViewportWidth = Width,
                ViewportHeight = Height,
                PageSize = PageSize,
                PageMargin = Margin,
                Portrait = Portrait,
                Delay = ConversionDelay,
                Timeout = Timeout,

                SinglePage = SinglePage
            };

            if (Progress != null)
                c.Progress += Progress;

            this.onComplete = onComplete;
            this.onError = onError;

            if (ConvertUrl)
            {
                if (string.IsNullOrWhiteSpace(Url))
                {
                    ShowError("Please specify the URL to convert.");
                    onComplete(null);
                    return;
                }

                // Convert URL
                c.ConvertAsync(Url, outputPath, OnConversionComplete);
            }
            else
            {
                if (string.IsNullOrWhiteSpace(HtmlString))
                {
                    ShowError("Please specify the HTML string to convert.");
                    onComplete(null);
                    return;
                }

                // Convert the specified HTML string.
                c.ConvertStringAsync(HtmlString, HtmlStringBaseUrl, outputPath, OnConversionComplete);
            }
        }

        void OnConversionComplete(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
                onError(e.Error);
            else
                onComplete(outputPath);
        }

        public override string Execute()
        {
            if (Width <= 0 || Height < 0)
            {
                ShowError("Invalid Width or Height");
                return null;
            }

            if (ConversionDelay < 0)
            {
                ShowError("Invalid conversion delay");
                return null;
            }

            // Set output path.
            var outputPath = Path.Combine(OutputDir, this.GetType().Name + ".pdf");
            if (File.Exists(outputPath))
                File.Delete(outputPath);

            // Create a new converter.
            var c = new HtmlToPdfConverter()
            {
                ViewportWidth = Width,
                ViewportHeight = Height,
                PageSize = PageSize,
                PageMargin = Margin,
                Portrait = Portrait,
                Delay = ConversionDelay,
                Timeout = Timeout,

                SinglePage = SinglePage
            };

            if (Progress != null)
                c.Progress += Progress;

            try
            {
                if (ConvertUrl)
                {
                    if (string.IsNullOrWhiteSpace(Url))
                    {
                        ShowError("Please specify the URL to convert.");
                        return null;
                    }

                    // Convert URL
                    c.Convert(Url, outputPath);
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(HtmlString))
                    {
                        ShowError("Please specify the HTML string to convert.");
                        return null;
                    }

                    // Convert the specified HTML string.
                    c.ConvertString(HtmlString, HtmlStringBaseUrl, outputPath);
                }
            }
            catch (Exception ex)
            {
                ShowError(ex.Message);
            }

            return outputPath;
        }
    }
}