How to Convert HTML to PDF | UniHTML

When you convert HTML to PDF, you can keep web content in a format that can be printed and shared. Making a PDF locks in the layout, fonts, images, and formatting of your HTML pages or documents, no matter how simple or complicated they are. This is why a lot of people look for “html to pdf converter,” “convert html file to pdf,” or “chrome html document to pdf.”

Online tools are useful, but they aren’t always the best choice for automation, privacy, or processing on the server side. UniHTML is a Go SDK from UniDoc that lets you convert HTML to PDF in a powerful, programmable way. This guide covers everything, from the basics to more advanced automation.

What Is UniHTML?

UniHTML is a module within UniDoc’s Go-based library. It renders HTML, CSS, and JavaScript and embeds the output directly into a PDF using UniPDF. You can convert HTML content from:

  • Inline strings

  • Local `.html files

  • URL sources

  • Entire directories containing HTML assets

With UniHTML, you can configure page dimensions (e.g., A4, Letter), margins, orientation, and more.

This is quite different from a basic online html file to pdf converter—UniHTML gives you full control in code, with high quality results.

Why Convert HTML to PDF Programmatically?

People commonly want to convert html file to pdf, but online tools like Sejda or Adobe Acrobat come with limits. For instance:

  • Sejda is simple and free, but it has limits on how many files you can use and how often you can use them.

  • Adobe Acrobat has a button on the toolbar, but you have to use it manually and have a live browser.

These tools are good for quick jobs, but they don’t work well for:

  • Batch jobs that run on their own

  • Making PDFs on the server

  • Content that is sensitive to privacy

  • Integrations in APIs or apps

UniHTML fills that gap by handling automation, privacy, and customization all in code.

Competitors at a Glance

Here’s how UniHTML stacks up against common solutions:

  • Sejda: Online HTML-to-PDF tool; free with limits

  • Adobe Acrobat: Browser toolbar tool; manual and Windows-focused

  • Pdfcrowd / html2pdf.com: Web-based converters for files or URLs

  • wkhtmltopdf, Puppeteer, WeasyPrint, etc.: EU libraries and engines for HTML-to-PDF

These tools are strong, but UniHTML offers:

  • Full automation

  • Server-side usage

  • Deep customization in Go code

  • No browser dependency

You might also like:

“How to Add Chapters to PDF: A Step-by-Step Guide with UniHTML”

Getting Started with UniHTML

Here’s how to convert HTML to PDF using UniHTML:

Step 1: Pull the UniHTML Docker Image

Before starting the UniHTML API, you need to pull the UniHTML Docker image and run it locally.

Get the UniHTML-Server Docker image:

docker pull unidoccloud/unihtml:latest

Start the UniHTML server with a defined output port:

docker run -p 8080:8080 -e UNIHTML_LICENSE_PATH=path/to/license -e UNIHTML_CUSTOMER_NAME=customer_name unidoccloud/unihtml

If using license API Key

docker run -p 8080:8080 -e UNIDOC_METERED_API_KEY=your_license_api_key unidoccloud/unihtml

Set Your License Information:

If you are using a standard license file, set the license path and customer name:

export UNIPDF_LICENSE_PATH=/path/to/your/license export UNIPDF_CUSTOMER_NAME=your_customer_name

####Get and Configure Your API Key:

If you are using a metered License API Key from UniCloud:

  1. Sign up at UniCloud to obtain a metered License API Key.

  2. If you are using the License API Key option, set the environment variable as:

    export UNIDOC_METERED_API_KEY=your_api_key

Step 2: Install UniHTML

Clone the UniHTML examples repo:

git clone https://github.com/unidoc/unihtml-examples.git

cd unihtml-examples/simple

Install dependencies in Go:

go get github.com/unidoc/unihtml

go get github.com/unidoc/unipdf/v3

Step 3: Review the Example Code

Here’s a simplified version of the core conversion:

license.SetMeteredKey(os.Getenv("UNIDOC_LICENSE_API_KEY"))

conn, err := unihtml.Connect(os.Args[1])
creator := unihtml.NewCreator(conn)

doc := unihtml.NewDocumentFromString(htmlContent)
creator.Draw(doc)
pdf := creator.CreatePDF()
pdf.WriteToFile("output.pdf")

This code:

  1. Connects to a UniHTML server

  2. Renders HTML content

  3. Draws it into a PDF

  4. Saves the result

Match this with your simple.html file

Full Example in Go Code

Below is a complete Go program to convert HTML to PDF using local HTML:

package main

import (
  "os"
  "log"

  "github.com/unidoc/unihtml"
  "github.com/unidoc/unipdf/v3/creator"
  "github.com/unidoc/unipdf/v3/license"
)

func main() {
  license.SetMeteredKey(os.Getenv("UNIDOC_LICENSE_API_KEY"))

  conn, err := unihtml.Connect("http://localhost:9090")
  if err != nil {
    log.Fatal(err)
  }

  defer conn.Close()

  c := creator.New()

  doc, err := unihtml.NewDocumentFromFile("simple.html")
  if err != nil {
    log.Fatal(err)
  }

  c.Draw(doc)
  if err := c.WriteToFile("simple.pdf"); err != nil {
    log.Fatal(err)
  }

  log.Println("✅ Created simple.pdf")
}

With this script, you’re using UniHTML to generate a professional PDF from HTML in PDF format, all in code.

You might also like:

“How to Deploy UniHTML on Kubernetes: A Step-by-Step Guide”

Handling HTML Assets and URLs

UniHTML supports rich HTML content including:

  • External CSS, images, fonts

  • JavaScript rendering

  • Entire web directories

  • Remote URLs

Simply call methods like NewDocumentFromDir() or NewDocumentFromURL() to convert full pages into PDF documents.

You can even use build-in CSS @page rules to control output size:

@page { size: A4; margin: 1cm; }

This gives fine control over html to pdf format details.

Automating Batch Conversion

Need to convert many HTML files? Use Go’s file handling:

for _, f := range []string{"a.html", "b.html"} {
  doc := unihtml.NewDocumentFromFile(f)
  c.Draw(doc)
  c.WriteToFile(f + ".pdf")
}

This turns multiple HTML files into separate PDFs—a robust html to pdf file converter workflow.

Advanced Features

UniHTML + UniPDF let you customize PDFs:

  • Page size/orientation: A4, Letter, landscape, portrait

  • Margins: Bleed settings

  • Headers and footers: Add dynamic page numbers

  • Watermarks: Add logos or stamps

  • Security: Password protection, signatures

  • Metadata: Titles, authors, creation dates

Unlike simple html converter to pdf tools, UniHTML gives you programmatic access to all of this.

You might also like:

“How to Build Your One-Page Resume Using UniHTML”

Performance and Quality

UniHTML uses a containerized headless browser engine for rendering. This ensures:

  • Pixel-perfect HTML rendering

  • Handling of modern CSS and JavaScript

  • Speed and scalability

According to StackOverflow, UniHTML is “container-based with API… rendering is perfect and has all HTML features”, and it also supports creating outlines, forms, or signatures.

UniHTML vs Other Libraries

This is how UniHTML compares:

  • wkhtmltopdf: Command line interface, open source, good CSS support

  • Puppeteer / Playwright: JavaScript engines are heavy but adaptable.

  • WeasyPrint: Is a CSS engine that runs on Python.

  • IronPDF: Is a .NET library that uses Chromium.

  • UniHTML: UniHTML is the best choice for Go developers because it is native, high-performance, and can be programmed in Go.

Use Cases for UniHTML

Reporting Tools

Generate PDF reports from web dashboards or HTML templates.

Invoice Generation

Turn styled HTML invoices into PDF outputs.

Turn styled HTML invoices into PDF outputs.

Archival Systems

Automatically convert web pages into PDFs for storage.

Web Apps & APIs

Serve downloadable PDF versions of blog posts or documents.

Sensitive Content

Keep processing internally without sending files online.

Common Questions

How to convert html to pdf?

Use UniHTML’s Go API, render HTML then write to PDF file.

Can I convert HTML files to PDF?

Yes, unihtml.NewDocumentFromFile("file.html") loads a local HTML file.

How to change HTML to PDF size or format?

Apply CSS page rules like @page { size: Letter; margin:10mm; }.

Can it handle remote web pages?

Yes, call unihtml.NewDocumentFromURL("https://example.com") to render and convert.

Does it support images and CSS?

Yes, it loads and embeds all referenced assets from HTML.

Quick Comparison Table

FeatureOnline ToolsUniHTML Go SDK
Ease of useDrag & dropRequires coding
One-off conversionsFast and easyOverkill for small jobs
Bulk or automated useManualFully automated
Control over layout/metadataVery limitedFull programmatic control
Privacy (no uploads)Public serversLocal or private usage
Language ecosystemWeb/toolsGo-first integration

Conclusion

If you’ve ever searched how to convert html to pdf, html in pdf format, or how to change html to pdf, UniHTML offers a powerful, automated path. Backed by UniPDF, you get production-quality PDF generation in Go.

For quick tasks, tools like Sejda or Adobe are straightforward. But for developers building apps, batch processes, or privacy-sensitive pipelines, UniHTML is the smart choice.