How To Convert From HTML To PDF On Windows

The pdflayer API is a quick and seamless way to automate your HTML to PDF conversion in any application. Its lightweight RESTful infrastructure uses an efficient combination of the most powerful PDF rendering engines available. This makes it the most cost-effective and reliable option for anyone looking to quickly process a small or large number of documents.

The pdflayer API  also has a full set of customizable functions. These include document configuration, a series of layout adjustment options, and authentication and security, design as well as branding tweaks, and much more.

In this article, we look at how you can use the REST-based pdflayer API in a low-code Windows application to convert HTML to PDF.

What are some use cases for converting HTML to PDFs?

There are many different use cases for converting HTML to PDFs. PDF to HTML conversions are useful in everything from legal settings to corporate environments. You can use it to capture the look and feel of a website or to create customized reports with watermarks. It is also useful for developers to turn HTML reports into PDFs for easier consumption or storage. These are just a few of the example use cases for HTML to PDF conversion.

How easy is it to convert HTML to PDFs?

It’s pretty darn easy, let’s take a look at how.

Access & Specification

To authenticate with the pdflayer API, simply attach your access_key to the base endpoint URL:

To get your Access Key, sign up here https://pdflayer.com/product

The pdflayer API uses simple URL-based HTTP requests for maximum compatibility with any existing programming language. The next three steps outline how to build a basic API request.

Step 1: Base URL

Each API request is based on the following URL:

Step 2: Required parameters

First, authenticate with the API by adding your access_key and providing either a URL using the document_url parameter or raw HTML code using the document_html parameter.

Parameter Description
access_key Key to authenticate with the API – find it in your Account Dashboard
document_url The full URL (including the HTTP Protocol) of the website you want to convert to a PDF document, e.g. http://example.com/invoice.html
document_html HTTP POST only – raw HTML code you would like to convert to a PDF document.

Step 3: Optional parameters                 

In addition to the required parameters, there are a series of functions and parameters you can use to configure and customize your PDF document. Here is a summary:

Configuration

Parameter Description Default
document_name specify a PDF name of up to 180 characters. pdflayer.pdf
custom_unit set to px (Pixels), pt (Points), in (Inches) or mm (Millimeters) px
user_agent set to your preferred User-Agent header string See below
accept_lang set to your preferred Accept-Language header string en-US
text_encoding set to your preferred text encoding string utf-8
ttl the time (in seconds) a generated PDF is cached 2,592,000
force set to 1 to force new PDF
inline set to 1 to display PDF document inline Attachment triggers download
auth_user specify username used to access password-protected site
auth_pass specify password used to access password-protected site
encryption set to 40 (40-bit) or 128 (128-bit)
owner_password specify owner password to password protect PDF
user_password specify user password to password protect PDF
Misc Options a series of configuration options [Learn more]
Permissions a series of permission options [Learn more]

Layout

Parameter Description Default
page_size set to preferred page size, e.g. A4A5, etc. A4
page_width specify page width (numeric), e.g. 200 (overrides page_size) A4 width
page_height specify page height (numeric), e.g. 600 (overrides page_size) A4 height
orientation set to portrait or landscape portrait
margin_top set to preferred top margin value (numeric), e.g. 5
margin_bottom set to preferred bottom margin value (numeric), e.g. 5
margin_left set to preferred left margin value (numeric), e.g. 5
margin_right set to preferred right margin value (numeric), e.g. 5
header_spacing set to preferred header spacing value (numeric), e.g. 10
header_text set to preferred header text, e.g. This is my heading
header_align set to leftcenter or right center
header_url set to (urlencoded) URL containing your preferred header HTML elements
header_html HTTP POST only – specify raw HTML as header element
footer_spacing set to preferred footer spacing value (numeric), e.g. 10
footer_text set to preferred footer text, e.g. This is my footer
footer_align set to leftcenter or right center
footer_url set to (urlencoded) URL containing your preferred footer HTML elements
footer_html HTTP POST only – specify raw HTML as footer element
viewport Set to preferred viewport “width x height”, e.g. 320x480 1440x900

Customization

Parameter Description Default
css_url inject a custom CSS stylesheet using a (urlencoded) URL
delay specify a delay (in seconds) before PDF is captured
dpi specify the DPI resolution (numerical) between 10 and 10000 96
zoom specify page zoom factor between 0 and 50
watermark_url specify a watermark URL (urlencoded) containing a PNG or JPG image
watermark_offset_x specify a horizontal watermark offset, e.g. 10
watermark_offset_y specify a vertical watermark offset, e.g. 10
watermark_opacity specify watermark opacity percentage (numeric) between 0 and 100 20
watermark_in_background set to 1 to place watermark behind text
Page Numbering Page numbering options including dynamic tags [Learn more]

Document Details

Parameter Description Default
title specify a PDF document title of 150 characters max.
subject specify a PDF document subject of 150 characters max.
creator specify a PDF document creator name of 150 characters max. pdflayer.com
author specify a PDF document author name of 150 characters max.

For more details, you can also read the full documentation.

How can we access pdflayer API using a Low-Code Windows Client?

You must include the WebView2Loader.dll in the same location as the EXE for the Microsoft Edge browser to function.

Download the WebView2Loader.dll: https://getitnow.embarcadero.com/EdgeView2SDK-1.0.664.37/

Install the Microsoft Edge browser from: https://www.microsoftedgeinsider.com/en-us/downloadH

Here is the example code:

How well does the HTML to PDF conversion work?

Whether you have a simple HTML and CSS page with just text or a complex HTML page featuring rich media like XML, SVG, or JavaScript, everything visible in a browser should show up in the PDF.

You can set the dimensions, margins, page numbers, custom CSS, encryption, viewport, delay, and much more of your PDF output. This lets you customize the output PDF to your needs.

You can even customizable the background or foreground watermark images for your final PDF document. This is great in a corporate or enterprise environment to bring a professional edge to your PDFs.

Ready to get started converting HTML to PDFs?

In this article, we demonstrated how easy it is to convert HTML to PDF using the pdflayer API. We also showed you how to create a low-code Windows application for the pdflayer API. Full source code is provided for the Windows application so you can jump right in and get started implementing the API in your own projects.

Head over and download the full source code for the Windows client here.