Fast SVG to vector PDF conversion via typst/svg2pdf
Project description
svg2pdf-py
Fast, accurate SVG → PDF conversion for Python, powered by the svg2pdf Rust crate from Typst. Built with PyO3 and Maturin.
Unlike rasterization-based approaches, svg2pdf-py produces true vector PDFs — text stays selectable, paths stay sharp at any scale, and custom fonts are fully embedded.
Features
- Vector-accurate SVG → PDF (no rasterization)
- Custom font support via
usvg/fontdb - Batch conversion for multi-page PDFs
- Exposes raw PDF chunks for embedding into existing documents
- Pre-built wheels for Windows, Linux, and macOS (Python ≥ 3.9)
Installation
pip install svg2pdf-py
Quick Start
import svg2pdf_py
# Load custom fonts
db = svg2pdf_py.FontDatabase()
db.load_font_file("/path/to/MyFont.ttf")
db.load_font_file("/path/to/MyFont-Bold.ttf")
# Convert SVG string → PDF bytes
svg_str = open("diagram.svg", encoding="utf-8").read()
pdf_bytes = svg2pdf_py.svg_to_pdf(svg_str, db)
open("diagram.pdf", "wb").write(pdf_bytes)
API
FontDatabase
Manages fonts used during SVG rendering.
# Empty database
db = svg2pdf_py.FontDatabase()
# Pre-loaded with system fonts
db = svg2pdf_py.FontDatabase.system()
db.load_font_file("/path/to/font.ttf") # load a single font file
db.load_fonts_dir("/path/to/fonts/") # load all fonts in a directory
db.load_system_fonts() # add system fonts to existing db
len(db) # number of loaded font faces
ConversionOptions
Controls PDF output quality.
opts = svg2pdf_py.ConversionOptions(
compress=True, # deflate-compress streams (default: True)
raster_scale=1.0, # scale factor for rasterized elements (default: 1.0)
embed_text=True, # embed text as real PDF text, not outlines (default: True)
)
svg_to_pdf
Convert a single SVG string to a standalone PDF.
pdf_bytes: bytes = svg2pdf_py.svg_to_pdf(
svg_str, # str — SVG content
db, # FontDatabase
options, # ConversionOptions (optional)
)
svg_to_chunk
Convert a single SVG string to a raw PDF chunk (XObject), suitable for embedding into an existing PDF document.
chunk_bytes: bytes = svg2pdf_py.svg_to_chunk(
svg_str,
db,
options, # optional
)
svg_pages_to_pdfs
Convert a list of SVG strings to individual PDF byte strings — one per page.
pages: list[bytes] = svg2pdf_py.svg_pages_to_pdfs(
svg_strings, # list[str]
db,
options, # optional
)
Multi-Page Example
Combine multiple SVGs into a single PDF using PyMuPDF:
import svg2pdf_py
import pymupdf
db = svg2pdf_py.FontDatabase()
db.load_fonts_dir("/path/to/fonts/")
svg_pages = [open(f"page{i}.svg").read() for i in range(1, 5)]
pdf_pages = svg2pdf_py.svg_pages_to_pdfs(svg_pages, db)
doc = pymupdf.open()
for pdf_bytes in pdf_pages:
part = pymupdf.open("pdf", pdf_bytes)
doc.insert_pdf(part)
part.close()
doc.save("output.pdf", garbage=4, deflate=True)
doc.close()
Credits
This package is a thin PyO3 binding over the excellent svg2pdf crate by Typst, which in turn uses usvg for SVG parsing and fontdb for font resolution.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file svg2pdf_py-1.1.0-cp311-abi3-win_amd64.whl.
File metadata
- Download URL: svg2pdf_py-1.1.0-cp311-abi3-win_amd64.whl
- Upload date:
- Size: 2.1 MB
- Tags: CPython 3.11+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c9052bd9d87a41f7ed2360e83a323b14219f08ef83dcf252f551f65da74e050
|
|
| MD5 |
3ad0e95e407f217a0cea5b5d3bf72185
|
|
| BLAKE2b-256 |
b660f9278872b37ffcfce77c3749ba9fb3440f4a20638e8a38423c73d854d617
|
Provenance
The following attestation bundles were made for svg2pdf_py-1.1.0-cp311-abi3-win_amd64.whl:
Publisher:
release.yml on IFSCM/svg2pdf-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
svg2pdf_py-1.1.0-cp311-abi3-win_amd64.whl -
Subject digest:
8c9052bd9d87a41f7ed2360e83a323b14219f08ef83dcf252f551f65da74e050 - Sigstore transparency entry: 1700985562
- Sigstore integration time:
-
Permalink:
IFSCM/svg2pdf-py@1338db46864222471c0dc844d7dd2d3ff27046b3 -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/IFSCM
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@1338db46864222471c0dc844d7dd2d3ff27046b3 -
Trigger Event:
push
-
Statement type:
File details
Details for the file svg2pdf_py-1.1.0-cp311-abi3-manylinux_2_35_x86_64.whl.
File metadata
- Download URL: svg2pdf_py-1.1.0-cp311-abi3-manylinux_2_35_x86_64.whl
- Upload date:
- Size: 2.6 MB
- Tags: CPython 3.11+, manylinux: glibc 2.35+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1329c86f89cc845db6a9b878fd928465452b366324b6c2d935cfa4e8e7c5eadc
|
|
| MD5 |
267ab133c40847b645983235ae2cea9a
|
|
| BLAKE2b-256 |
fba65dd93e50dcb755af2ac037ffbc065f87f7f1cbe8d9af6300a1956c989a74
|
Provenance
The following attestation bundles were made for svg2pdf_py-1.1.0-cp311-abi3-manylinux_2_35_x86_64.whl:
Publisher:
release.yml on IFSCM/svg2pdf-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
svg2pdf_py-1.1.0-cp311-abi3-manylinux_2_35_x86_64.whl -
Subject digest:
1329c86f89cc845db6a9b878fd928465452b366324b6c2d935cfa4e8e7c5eadc - Sigstore transparency entry: 1700985589
- Sigstore integration time:
-
Permalink:
IFSCM/svg2pdf-py@1338db46864222471c0dc844d7dd2d3ff27046b3 -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/IFSCM
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@1338db46864222471c0dc844d7dd2d3ff27046b3 -
Trigger Event:
push
-
Statement type:
File details
Details for the file svg2pdf_py-1.1.0-cp311-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: svg2pdf_py-1.1.0-cp311-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 2.2 MB
- Tags: CPython 3.11+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb020fc65ad57682f18c27a76549a7740ae135a73f8317a012abc17a9313bc00
|
|
| MD5 |
6196099da9a1e1656c6415a0a6fd7681
|
|
| BLAKE2b-256 |
84604f695218d7a3f77ed593197fcc43dfdbcd22b3bab74b2f1f3ca1f6988119
|
Provenance
The following attestation bundles were made for svg2pdf_py-1.1.0-cp311-abi3-macosx_11_0_arm64.whl:
Publisher:
release.yml on IFSCM/svg2pdf-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
svg2pdf_py-1.1.0-cp311-abi3-macosx_11_0_arm64.whl -
Subject digest:
cb020fc65ad57682f18c27a76549a7740ae135a73f8317a012abc17a9313bc00 - Sigstore transparency entry: 1700985576
- Sigstore integration time:
-
Permalink:
IFSCM/svg2pdf-py@1338db46864222471c0dc844d7dd2d3ff27046b3 -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/IFSCM
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@1338db46864222471c0dc844d7dd2d3ff27046b3 -
Trigger Event:
push
-
Statement type: