How to generate PDF documents programmatically

Do you need to generate PDF documents programmatically for your business or client? May be it’s an invoice or an agreement or a report or a ticket. The best and easiest way to create PDF documents via code is to create them as HTML – which gives you flexibility over how it looks. You can style the document with CSS and convert the HTML to PDF.

There are various ways to convert HTML to PDF. wkhtmltopdf is a popular open source command line tool to convert html to pdf.

wkhtmltopdf google.pdf

Headless Chrome is another option. Headless Chrome is better because nothing can render HTML better than a browser.
Puppeteer is a NodeJS library that you can use to interact with headless Chrome and save an HTML file as PDF.

const puppeteer = require('puppeteer');
async function createPDF() {
  const browser = await puppeteer.launch({ headless: true });
  const page = await browser.newPage();
  await page.goto("", {
      waitUntil: 'networkidle2',
  const pdf = await page.pdf({ 
    path: '/path-save-pdf.pdf'
  await browser.close();

If you do not want to bother with setting up headless chrome and Puppeteer and command line utilities like wkhtmltopdf, we have created a simple REST API based on headless chrome to generate PDF programmatically from HTML.

We have done all the hard work so you don’t have to and you can focus on the core aspects of your application and leave the PDF generation to our API.

PDFMark’s HTML to PDF API is a simple REST API, using which you can convert your HTML to PDF in seconds.

curl --location --request POST '' \
--header 'key: demokey' \
--header 'Content-Type: application/json' \
--data-raw '{
"html" : "<html> <body> <h1> Hello World </h1> </body> </html>"

You can customize the PDF by specifying page size, margins, header and footer. You can watermark the document and protect it with password and even prevent users from copying or printing if required.

PDFMark has very easy to use and extensive documentation.

What’s more, PDFMark even saves the generated PDF in cloud for you, and you can server it directly to your end users via our fast global CDN.

Check out the demo of PDFMark’s API at and sign up to get your API key and start generating PDF documents programmatically in your favorite programming language.