xqr

SVG to PDF and Image Conversion

This guide covers converting SVG files to PDF and other image formats, including handling embedded PDFs and metadata.

Prerequisites

Basic Usage

Extracting Embedded PDFs

Extract a PDF embedded within an SVG file:

python examples/svg_pdf_conversion_fixed.py input.svg --extract-pdf output.pdf

Converting SVG to PDF

Convert an SVG file to PDF:

python examples/svg_pdf_conversion_fixed.py input.svg --to-pdf output.pdf

Converting to Image Formats

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

Extracting Metadata

View metadata contained in an SVG file:

python examples/svg_pdf_conversion_fixed.py input.svg --extract-metadata

Advanced Features

Custom Page Sizes and Orientation

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

Image Processing

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

Working with Embedded Resources

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

Batch Processing

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

Integration Examples

Pipe SVG from stdin

# 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

Use as a Python Module

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)

Performance Tips

  1. For large SVGs: Use --downgrade to simplify complex paths
  2. For batch processing: Skip metadata extraction when not needed with --no-metadata
  3. Memory optimization: Process large files with --chunk-size
  4. Caching: Use the --cache option to cache intermediate conversions

Troubleshooting

Common Issues

For more information, see the Troubleshooting Guide.