Skip to content

Commit 7dabf96

Browse files
committed
Make CairoSVG a required dependency
This way pdfbaker works out-of-the-box. Update README accordingly.
1 parent 3488f7e commit 7dabf96

File tree

4 files changed

+38
-70
lines changed

4 files changed

+38
-70
lines changed

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[MASTER]
22
score=n
33
generated-members=PyPDF2.*
4-
ignored-modules=jinja2,pypdf,yaml
4+
ignored-modules=cairosvg,jinja2,pypdf,yaml
55

66
[REPORTS]
77
msg-template={path}:{line}: [{msg_id}({symbol}), {obj}] {msg}

README.md

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,6 @@
22

33
Creates PDF documents from YAML-configured SVG templates.
44

5-
## Requirements
6-
7-
For the conversion from SVG to PDF you need to install either
8-
9-
- [CairoSVG](https://cairosvg.org/)<br>
10-
```
11-
sudo apt install python3-cairosvg
12-
```
13-
14-
or
15-
16-
- [Inkscape](https://inkscape.org/)<br>
17-
```
18-
sudo apt install inkscape
19-
```
20-
21-
If you want to compress your PDFs, you will also need to install
22-
23-
- [Ghostscript](https://www.ghostscript.com/)
24-
```
25-
sudo apt install ghostscript
26-
```
27-
285
## Installation
296

307
pdfbaker is on [PyPI](https://pypi.org/project/pdfbaker/) and we reommend installing it
@@ -35,13 +12,29 @@ pipx install pdfbaker
3512
```
3613

3714
If you don't yet have pipx,
38-
[install it first](https://pipx.pypa.io/latest/installation/):
15+
[install pipx first](https://pipx.pypa.io/latest/installation/):
3916

4017
```
4118
sudo apt install pipx
4219
pipx ensurepath
4320
```
4421

22+
## Optional Dependencies
23+
24+
- SVG to PDF conversion uses [CairoSVG](https://cairosvg.org/) by default. For documents
25+
requiring [Inkscape](https://inkscape.org/) instead, install it first:
26+
27+
```
28+
sudo apt install inkscape
29+
```
30+
31+
- If you want to compress your PDFs, you will need to install
32+
[Ghostscript](https://www.ghostscript.com/):
33+
34+
```
35+
sudo apt install ghostscript
36+
```
37+
4538
## Usage
4639

4740
Generate your documents with:

pyproject.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@ authors = [
66
{ name = "Danny W. Adair", email = "danny.adair@unfold.nz" }
77
]
88
dependencies = [
9-
"pyyaml",
9+
"cairosvg",
1010
"jinja2",
1111
"pypdf",
12+
"pyyaml",
1213
]
1314
readme = "README.md"
1415
requires-python = ">= 3.11"
1516

16-
[project.optional-dependencies]
17-
cairosvg = ["cairosvg"]
18-
all = ["cairosvg"]
19-
2017
[project.scripts]
2118
pdfbaker = "pdfbaker.__main__:main"
2219

src/pdfbaker/common.py

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@
44

55
import pypdf
66
import yaml
7-
8-
try:
9-
from cairosvg import svg2pdf
10-
11-
CAIROSVG_AVAILABLE = True
12-
except ImportError:
13-
CAIROSVG_AVAILABLE = False
7+
from cairosvg import svg2pdf
148

159

1610
def deep_merge(base, update):
@@ -100,38 +94,22 @@ def convert_svg_to_pdf(svg_path, pdf_path, backend="cairosvg"):
10094
Path to the generated PDF file
10195
"""
10296
if backend == "inkscape":
103-
return _convert_with_inkscape(svg_path, pdf_path)
104-
return _convert_with_cairosvg(svg_path, pdf_path)
105-
106-
107-
def _convert_with_cairosvg(svg_path, pdf_path):
108-
"""Convert SVG to PDF using CairoSVG."""
109-
if not CAIROSVG_AVAILABLE:
110-
raise ImportError(
111-
"CairoSVG is not installed. Please install it with 'pip install cairosvg' "
112-
"or set svg2pdf_backend to 'inkscape' in your config."
113-
)
114-
115-
with open(svg_path, "rb") as svg_file:
116-
svg2pdf(file_obj=svg_file, write_to=pdf_path)
97+
try:
98+
subprocess.run(
99+
[
100+
"inkscape",
101+
f"--export-filename={pdf_path}",
102+
svg_path,
103+
],
104+
check=True,
105+
)
106+
except (subprocess.SubprocessError, FileNotFoundError) as exc:
107+
raise RuntimeError(
108+
"Inkscape command failed. Please ensure Inkscape is installed "
109+
'and in your PATH or set svg2pdf_backend to "cairosvg" in your config.'
110+
) from exc
111+
else:
112+
with open(svg_path, "rb") as svg_file:
113+
svg2pdf(file_obj=svg_file, write_to=pdf_path)
117114

118115
return pdf_path
119-
120-
121-
def _convert_with_inkscape(svg_path, pdf_path):
122-
"""Convert SVG to PDF using Inkscape."""
123-
try:
124-
subprocess.run(
125-
[
126-
"inkscape",
127-
f"--export-filename={pdf_path}",
128-
svg_path,
129-
],
130-
check=True,
131-
)
132-
return pdf_path
133-
except (subprocess.SubprocessError, FileNotFoundError) as exc:
134-
raise RuntimeError(
135-
"Inkscape command failed. Please ensure Inkscape is installed "
136-
"and in your PATH or set svg2pdf_backend to 'cairosvg' in your config."
137-
) from exc

0 commit comments

Comments
 (0)