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.

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
- 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
One of the following browsers must be installed:
- Chromium
- Google Chrome
- Brave
The script checks for them in this order:
- Google Chrome
- Google Chrome Stable
- Chromium
- Chromium Browser
- Brave
sudo apt install -y chromiumMake the script executable:
chmod +x convert-html-to-pdf.shYou can then run it directly:
./convert-html-to-pdf.sh invoice.html./convert-html-to-pdf.sh filename
The output filename is generated automatically.
Input:
invoice.html
Command:
./convert-html-to-pdf.sh invoice.htmlOutput:
invoice.pdf
Another example:
monthly-report.html
becomes:
monthly-report.pdf
The script removes whatever extension is present and replaces it with .pdf.
Given:
documents/
├── invoice.html
├── report.html
├── convert-html-to-pdf.sh
└── stylesheet.css
Running:
./convert-html-to-pdf.sh invoice.htmlproduces:
documents/
├── invoice.html
├── invoice.pdf
├── report.html
├── convert-html-to-pdf.sh
└── stylesheet.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.
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 HTML files can be converted directly:
./convert-html-to-pdf.sh invoice.htmlImages 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">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.
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 |
The script checks that:
- An input filename was supplied.
- The input file exists.
- A supported browser is installed.
- 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
Create an HTML document:
gedit invoice.htmlMake the converter executable:
chmod +x convert-html-to-pdf.shConvert it:
./convert-html-to-pdf.sh invoice.htmlResult:
PDF created:
/path/to/invoice.pdf
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.