This guide covers converting SVG files to PDF and other image formats, including handling embedded PDFs and metadata.
cairosvg
, Pillow
, lxml
, python-magic
Extract a PDF embedded within an SVG file:
python examples/svg_pdf_conversion_fixed.py input.svg --extract-pdf output.pdf
Convert an SVG file to PDF:
python examples/svg_pdf_conversion_fixed.py input.svg --to-pdf output.pdf
Convert SVG to PNG:
python examples/svg_pdf_conversion_fixed.py input.svg --to-png output.png
Convert SVG to JPG:
python examples/svg_pdf_conversion_fixed.py input.svg --to-jpg output.jpg
View metadata contained in an SVG file:
python examples/svg_pdf_conversion_fixed.py input.svg --extract-metadata
Convert to A4 size:
python examples/svg_pdf_conversion_fixed.py input.svg --to-pdf output.pdf --page-size A4
Custom page size in points (1/72 inch):
python examples/svg_pdf_conversion_fixed.py input.svg --to-pdf output.pdf --page-size "800x600"
Landscape orientation:
python examples/svg_pdf_conversion_fixed.py input.svg --to-pdf output.pdf --landscape
Convert to grayscale:
python examples/svg_pdf_conversion_fixed.py input.svg --to-png output.png --grayscale
Add a white background (useful for transparent SVGs):
python examples/svg_pdf_conversion_fixed.py input.svg --to-png output.png --background white
Adjust image quality (1-100, higher is better):
python examples/svg_pdf_conversion_fixed.py input.svg --to-jpg output.jpg --quality 95
Extract all embedded images from SVG:
python examples/svg_pdf_conversion_fixed.py input.svg --extract-images output_dir/
Convert SVG with embedded images to PDF (preserves all resources):
python examples/svg_pdf_conversion_fixed.py complex.svg --to-pdf output.pdf --embed-images
Process multiple SVGs in parallel (using GNU parallel):
find . -name "*.svg" | parallel -j 4 "python examples/svg_pdf_conversion_fixed.py {} --to-pdf {.}.pdf"
Only process SVGs modified in the last 24 hours:
find . -name "*.svg" -mtime -1 | while read file; do
python examples/svg_pdf_conversion_fixed.py "$file" --to-pdf "${file%.svg}.pdf"
done
# Convert SVG from a web request to PDF
curl -s https://example.com/diagram.svg | \
python examples/svg_pdf_conversion_fixed.py - --to-pdf output.pdf
from svg_pdf_conversion_fixed import SVGConverter
# Convert SVG to PDF
converter = SVGConverter("input.svg")
converter.convert_to_pdf("output.pdf")
# Extract metadata
metadata = converter.extract_metadata()
print(metadata)
--downgrade
to simplify complex paths--no-metadata
--chunk-size
--cache
option to cache intermediate conversions--chunk-size
For more information, see the Troubleshooting Guide.