|
1 | | -"""A simple converter from SVG to PDF. |
| 1 | +"""A tool for converting SVG to PDF. |
2 | 2 |
|
3 | | -For further information please check the file README.txt! |
| 3 | +This module provides a high-level function for converting SVG files to PDF format |
| 4 | +using the ReportLab graphics library. It also serves as the main entry point for |
| 5 | +the svglib package. |
| 6 | +
|
| 7 | +The module includes: |
| 8 | +- svg2pdf(): Convert SVG files to PDF programmatically. |
| 9 | +- main(): Command-line interface for SVG to PDF conversion. |
| 10 | +- Automatic file path handling and output generation. |
4 | 11 | """ |
5 | 12 |
|
6 | 13 | import argparse |
7 | 14 | import sys |
8 | 15 | import textwrap |
9 | 16 | from datetime import datetime |
| 17 | +from importlib.metadata import PackageNotFoundError, version |
10 | 18 | from os.path import basename, dirname, exists, splitext |
11 | 19 | from typing import Optional |
12 | 20 |
|
13 | 21 | from reportlab.graphics import renderPDF |
14 | 22 |
|
15 | 23 | from svglib import svglib |
16 | 24 |
|
| 25 | +try: |
| 26 | + __version__ = version("svglib") |
| 27 | +except PackageNotFoundError: |
| 28 | + __version__ = "unknown" |
| 29 | + |
17 | 30 |
|
18 | 31 | def svg2pdf(path: str, outputPat: Optional[str] = None) -> None: |
19 | | - "Convert an SVG file to a PDF one." |
| 32 | + """Convert an SVG file to PDF format. |
| 33 | +
|
| 34 | + High-level function that loads an SVG file, converts it to a ReportLab drawing, |
| 35 | + and saves it as a PDF file. Supports both .svg and .svgz (compressed SVG) files. |
| 36 | +
|
| 37 | + Args: |
| 38 | + path: Path to the input SVG file (.svg or .svgz extension). |
| 39 | + outputPat: Optional output path pattern. Supports placeholders: |
| 40 | + - %(dirname)s: Directory of input file |
| 41 | + - %(basename)s: Full filename with extension |
| 42 | + - %(base)s: Filename without extension |
| 43 | + - %(ext)s: File extension |
| 44 | + - %(now)s: Current datetime object |
| 45 | + - %(format)s: Output format (always "pdf") |
| 46 | + Also supports {name} format strings. |
| 47 | +
|
| 48 | + Returns: |
| 49 | + None. The PDF file is written to disk. |
| 50 | +
|
| 51 | + Raises: |
| 52 | + FileNotFoundError: If the input SVG file does not exist. |
| 53 | + Exception: If SVG parsing or PDF generation fails. |
| 54 | + OSError: If the output directory is not writable. |
| 55 | +
|
| 56 | + Examples: |
| 57 | + Basic conversion: |
| 58 | + >>> svg2pdf("input.svg") # Creates "input.pdf" |
| 59 | +
|
| 60 | + Custom output location: |
| 61 | + >>> svg2pdf("input.svg", "output.pdf") |
| 62 | +
|
| 63 | + Using placeholders: |
| 64 | + >>> svg2pdf("path/file.svg", "%(dirname)s/converted/%(base)s.pdf") |
| 65 | + # Creates "path/converted/file.pdf" |
| 66 | +
|
| 67 | + Timestamped output: |
| 68 | + >>> svg2pdf("file.svg", "backup/%(now.year)s-%(now.month)s-%(base)s.pdf") |
| 69 | +
|
| 70 | + Note: |
| 71 | + The function will overwrite existing PDF files without warning. |
| 72 | + For compressed SVG files (.svgz), the file is automatically decompressed. |
| 73 | + """ |
20 | 74 |
|
21 | 75 | # derive output filename from output pattern |
22 | 76 | file_info = { |
@@ -52,10 +106,7 @@ def main() -> None: |
52 | 106 | ext_caps = ext.upper() |
53 | 107 | format_args = dict( |
54 | 108 | prog=basename(sys.argv[0]), |
55 | | - version=svglib.__version__, |
56 | | - author=svglib.__author__, |
57 | | - license=svglib.__license__, |
58 | | - copyleft_year=str(datetime.now().year), |
| 109 | + version=__version__, |
59 | 110 | ts_pattern="{{dirname}}/out-" |
60 | 111 | "{{now.hour}}-{{now.minute}}-{{now.second}}-" |
61 | 112 | "%(base)s", |
@@ -87,9 +138,7 @@ def main() -> None: |
87 | 138 |
|
88 | 139 | issues/pull requests: |
89 | 140 | https://github.com/deeplook/svglib |
90 | | -
|
91 | | - Copyleft by {author}, 2008-{copyleft_year} ({license}): |
92 | | - https://www.gnu.org/licenses/lgpl-3.0.html""".format(**format_args) |
| 141 | + """.format(**format_args) |
93 | 142 | ) |
94 | 143 | p = argparse.ArgumentParser( |
95 | 144 | description=desc, |
@@ -119,7 +168,7 @@ def main() -> None: |
119 | 168 | args = p.parse_args() |
120 | 169 |
|
121 | 170 | if args.version: |
122 | | - print(svglib.__version__) |
| 171 | + print(__version__) |
123 | 172 | sys.exit() |
124 | 173 |
|
125 | 174 | if not args.input: |
|
0 commit comments