Skip to content

About

Converting-html-to-pdf while preserving the style and size and amazing look

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

9 Commits

Folders and files

Repository files navigation

HTML to PDF Converter

A lightweight Linux command-line script for converting HTML files to PDF using Chromium, Google Chrome or Brave.

The conversion is performed using a real Chromium-based browser engine. Screenshot from 2026-10-03 07-32-05

Helping preserve the original HTML appearance including:

  • CSS styling
  • Tables
  • Borders and boxes
  • Rounded corners
  • Gradients
  • Colours
  • Images
  • SVG graphics
  • Flexbox
  • CSS Grid
  • Web fonts
  • JavaScript-rendered content
  • Print CSS
  • A4 page layouts

Features

  • Simple command-line interface
  • Automatically detects an installed Chromium-based browser
  • Automatically generates the PDF filename
  • Preserves the original filename
  • Removes the input file extension automatically
  • Adds .pdf
  • No output filename needs to be specified
  • Uses headless browser rendering
  • Supports local HTML files
  • Suitable for invoices, reports, forms and other styled documents

Requirements

One of the following browsers must be installed:

  • Chromium
  • Google Chrome
  • Brave

The script checks for them in this order:

  1. Google Chrome
  2. Google Chrome Stable
  3. Chromium
  4. Chromium Browser
  5. Brave

Install Chromium on Ubuntu/Debian

sudo apt install -y chromium

Installation

Make the script executable:

chmod +x convert-html-to-pdf.sh

You can then run it directly:

./convert-html-to-pdf.sh invoice.html

Usage

./convert-html-to-pdf.sh filename

The output filename is generated automatically.

Example

Input:

invoice.html

Command:

./convert-html-to-pdf.sh invoice.html

Output:

invoice.pdf

Another example:

monthly-report.html

becomes:

monthly-report.pdf

The script removes whatever extension is present and replaces it with .pdf.

Directory Example

Given:

documents/
├── invoice.html
├── report.html
├── convert-html-to-pdf.sh
└── stylesheet.css

Running:

./convert-html-to-pdf.sh invoice.html

produces:

documents/
├── invoice.html
├── invoice.pdf
├── report.html
├── convert-html-to-pdf.sh
└── stylesheet.css

HTML and CSS

For the best visual results, use print-specific CSS.

For example:

@page{
    size:A4;
    margin:0;
}

@media print{
    body{
        margin:0;
    }
}

It is also recommended to include:

*{
    box-sizing:border-box;
}

html,
body{
    -webkit-print-color-adjust:exact;
    print-color-adjust:exact;
}

This helps ensure that backgrounds, colours and other visual elements are retained in the generated PDF.

Browser Rendering

The script uses Chromium's headless PDF renderer rather than attempting to interpret HTML itself.

This means the document is rendered using a browser engine before being converted to PDF.

The following options are used:

--headless=new
--disable-gpu
--disable-dev-shm-usage
--print-to-pdf
--no-pdf-header-footer
--run-all-compositor-stages-before-draw
--virtual-time-budget=3000

The virtual time budget gives JavaScript and dynamically generated content time to render before the PDF is created.

Local Files

Local HTML files can be converted directly:

./convert-html-to-pdf.sh invoice.html

Images referenced by the HTML can also be loaded when they are accessible from the local document.

For example:

<img src="images/logo.png" alt="Company Logo">

A local stylesheet can similarly be referenced:

<link rel="stylesheet" href="css/invoice.css">

Output Location

The PDF is created in the same directory as the input HTML file.

For example:

/home/USERNAME/Documents/invoice.html

becomes:

/home/USERNAME/Documents/invoice.pdf

The script resolves both paths to absolute paths before starting the browser.

Filename Handling

The output filename is generated with:

OUTPUT="${INPUT%.*}.pdf"

This removes the final extension from the input filename and adds .pdf.

Examples:

Input Output
invoice.html invoice.pdf
report.htm report.pdf
document.xhtml document.pdf
page.test.html page.test.pdf

Error Handling

The script checks that:

  1. An input filename was supplied.
  2. The input file exists.
  3. A supported browser is installed.
  4. The PDF was successfully created.

Example:

Error: HTML file not found: invoice.html

or:

Error: Chromium, Chrome or Brave was not found.
Install Chromium with:
sudo apt install -y chromium

Example Workflow

Create an HTML document:

gedit invoice.html

Make the converter executable:

chmod +x convert-html-to-pdf.sh

Convert it:

./convert-html-to-pdf.sh invoice.html

Result:

PDF created:
/path/to/invoice.pdf

Licence

Copyright (c) J~Net 2026.

Provided for personal and commercial use.


J~Net HTML to PDF Converter

Simple HTML → PDF conversion using a real Chromium-based rendering engine.

About

Converting-html-to-pdf while preserving the style and size and amazing look

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages