Python getting started

Generate PDF bytes
from Python

Install a native wheel, keep the Python API small, and let the Rust engine handle rendering in process.

CPython 3.8+ · Linux, macOS, Windows

Install the Python package

python -m pip install ironpress

PyPI provides CPython ABI3 wheels for supported Linux, macOS, and Windows targets. An installed wheel does not need a Rust toolchain, browser executable, or system PDF library.

Create an HTML PDF

The convenience function returns a Python bytes value:

from pathlib import Path
import ironpress

html = """
<style>
  @page { size: A4; margin: 18mm; }
  h1 { color: #295dff; }
</style>
<h1>Hello from Python</h1>
"""

pdf = ironpress.html_to_pdf(html)
Path("output.pdf").write_bytes(pdf)
Returned type

Store the bytes, return them from a web response, or write them with Path.write_bytes.

Render Markdown

pdf = ironpress.markdown_to_pdf(
    "# Release notes\n\nEverything shipped."
)
Path("release-notes.pdf").write_bytes(pdf)

Reuse a configured converter

Python configuration methods mutate the converter and return None. Configure once, then reuse the object.

converter = ironpress.HtmlConverter()
converter.page_size("Letter")
converter.margin_sides(36, 48, 36, 48)
converter.header("Quarterly report")
converter.footer("Page {page} of {pages}")

pdf = converter.convert("<h1>Results</h1>")
markdown_pdf = converter.convert_markdown("# Results")

Use convert_to_file or convert_markdown_to_file when direct output is more convenient than receiving bytes.

Load local resources and fonts

Grant a canonical directory before relative document URLs can access local files:

from pathlib import Path

converter = ironpress.HtmlConverter()
converter.base_path("templates")
converter.resource_root(".")
converter.add_font("Inter", Path("assets/Inter.ttf").read_bytes())

pdf = converter.convert("""
  <style>body { font-family: Inter }</style>
  <img src="assets/logo.png">
""")

Optional CJK and emoji font packs also enter as verified bytes through add_font_pack. Rendering never downloads a font pack.

Handle failures and runtime limits

Invalid configuration and conversion failures raise ValueError. File writes can also raise normal Python I/O exceptions.

  • HTML sanitization is enabled by default.
  • The Python binding has no async or streaming conversion API.
  • Remote HTTP document resources are not available from the binding.
  • Use a browser renderer for JavaScript-driven pages or exact Chrome output.

Go further