Skip to main content

sghtmltopdf

A Python binding for sghtmltopdf, converting HTML to PDF using Servo's layout and rendering components.

Requirements

  • Python >= 3.10
  • Rust toolchain (for building from source)

Installation

pip install sghtmltopdf

Usage

import sghtmltopdf

html = "<html><body><h1>Hello, World!</h1><p>Generate PDF from HTML with Servo engine.</p></body></html>"
pdf_bytes = sghtmltopdf.render(html)

with open("output.pdf", "wb") as f:
    f.write(pdf_bytes)

In-memory rendering with CSS and images (no filesystem dependency)

You can pass bytes directly and embed styles/images in-memory using data: URLs or inline tags without touching the filesystem:

import base64
import sghtmltopdf

# 1. In-memory HTML & CSS (via <style> tag or <link>)
css_bytes = b"h1 { color: #0066cc; } p { font-size: 14px; }"
css_b64 = base64.b64encode(css_bytes).decode("ascii")

# 2. In-memory image (via data: URL)
image_bytes = b"..."  # PNG/JPEG bytes
img_b64 = base64.b64encode(image_bytes).decode("ascii")

html_bytes = f"""<!DOCTYPE html>
<html>
<head>
  <!-- <link> with data: URL -->
  <link rel="stylesheet" href="data:text/css;base64,{css_b64}">
  <!-- or inline <style>: <style>{css_bytes.decode('utf-8')}</style> -->
</head>
<body>
  <h1>Title</h1>
  <p>Hello from in-memory HTML!</p>
  <img src="data:image/png;base64,{img_b64}">
</body>
</html>""".encode("utf-8")

pdf_bytes = sghtmltopdf.render(html_bytes)

Rendering with local assets

To allow loading local resources (e.g. images, stylesheets) from specified directories:

import sghtmltopdf

html = '<html><head><link rel="stylesheet" href="/path/to/assets/style.css"></head><body><img src="/path/to/assets/logo.png"></body></html>'
pdf_bytes = sghtmltopdf.render(
    html,
    allow_path="/path/to/assets",  # Pass a list for multiple paths: ["/path/to/assets", "/path/to/fonts"]
)

Rendering with remote assets (HTTP/HTTPS)

To allow fetching remote resources such as images or stylesheets via HTTP/HTTPS:

import sghtmltopdf

html = '<html><body><img src="https://example.com/logo.png"></body></html>'
pdf_bytes = sghtmltopdf.render(html, allow_remote_assets=True)

Options

Supported options correspond to sghtmltopdf CLI arguments:

  • page_size: "A4", "A3", "A5", "Letter", "Legal"
  • orientation: "Portrait", "Landscape"
  • page_width: e.g. "210mm"
  • page_height: e.g. "297mm"
  • margin_top, margin_bottom, margin_left, margin_right: e.g. "10mm", "1in"
  • grayscale: True / False
  • dpi: e.g. 96.0
  • zoom: e.g. 1.0
  • title, author, subject, keywords: PDF metadata strings
  • font: Font path or list of font paths/dicts (e.g. font="font.ttf" or font={"path": "font.ttc", "index": 1})
  • header_left, header_center, header_right: Header text (supports [page], [topage])
  • footer_left, footer_center, footer_right: Footer text
  • toc: True to insert a table of contents before the document body
  • allow / allow_path: Allowed directory paths for local resources

Testing

pip install -e .
pytest

License

MIT License

Metadata

Release files for sghtmltopdf 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sghtmltopdf 0.1.0
File Size Uploaded
sghtmltopdf-0.1.0.tar.gz 6.1 kB Details

Release files / sghtmltopdf-0.1.0.tar.gz

Download URL sghtmltopdf-0.1.0.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
efee9aeb50773fa5e80b58c06b6349af5738204df3f7a66230f0cd5344e33ba9
BLAKE2b-256 checksum
How to use checksums
07cc3754e6e8e8addb33aa18a3cf82d203e038229f223a2c19dd703c40f9457d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.3

Release history Release notifications | RSS feed

0.1.1

1 release file

This release

0.1.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page