Skip to content

Commit 0e75ce0

Browse files
committed
Add examples, test, improve pre-commit hook
1 parent 7c8334a commit 0e75ce0

36 files changed

Lines changed: 735 additions & 20 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ wheels/
88
# Cache
99
/.pytest_cache
1010
/.ruff_cache
11+
.coverage
1112

1213
# venv
1314
/.venv

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,10 @@ repos:
4545
- id: pylint
4646
args:
4747
- --rcfile=.pylintrc
48+
additional_dependencies:
49+
- "cairosvg"
50+
- "click"
51+
- "jinja2"
52+
- "pypdf"
53+
- "pytest"
54+
- "pyyaml"

.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=cairosvg,click,jinja2,pypdf,yaml
4+
reports=no
55

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

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ This will produce your PDF files in a `dist/` directory where your configuration
5757
lives. It will also create a `build/` directory with intermediate files, which is only
5858
kept if you specify `--debug`.
5959

60+
## Examples
61+
62+
For working examples, see the [examples](examples) directory.<br> Create all PDFs with:
63+
64+
```bash
65+
pdfbaker bake examples/examples.yml
66+
```
67+
6068
## Documentation
6169

6270
- [Overview](docs/overview.md)
@@ -65,3 +73,45 @@ kept if you specify `--debug`.
6573
- [Custom Processing](docs/custom_processing.md)
6674

6775
( [on GitHub](https://github.com/pythonnz/pdfbaker/tree/main/docs) )
76+
77+
## Development
78+
79+
This project uses [uv](https://github.com/astral-sh/uv) for dependency management. The
80+
`uv.lock` file ensures reproducible builds.
81+
82+
Create and activate the virtual environment:
83+
84+
```bash
85+
uv venv
86+
source .venv/bin/activate
87+
```
88+
89+
Install development dependencies:
90+
91+
```bash
92+
uv sync --dev
93+
```
94+
95+
### Tests
96+
97+
Run tests:
98+
99+
```bash
100+
pytest
101+
```
102+
103+
View test coverage:
104+
105+
```bash
106+
pytest --cov=pdfbaker --cov-report=term-missing
107+
```
108+
109+
### Pre-commit hook
110+
111+
If you want to commit changes, install [pre-commit](https://pre-commit.com) and run
112+
113+
```bash
114+
pre-commit install
115+
```
116+
117+
This ensures the same checks run locally as in GitHub CI.

docs/configuration.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
```
66
project/
7-
├── kiwipycon.yml # Main configuration
8-
├── material_specs/ # A document
7+
├── kiwipycon.yml # Main configuration
8+
├── material_specs/ # A document
99
│ ├── config.yml # Document configuration
1010
│ ├── images/ # Images
1111
│ ├── pages/ # Page configurations
@@ -82,8 +82,9 @@ Example:
8282
```yaml
8383
# prospectus/config.yml
8484
85-
filename: "Kiwi PyCon {{ conference.year }} - Prospectus"
86-
compress_pdf: true # Override the main config
85+
filename: "Kiwi PyCon {{ conference.year }} - Prospectus" # Use config values in config values!
86+
title: "Sponsorship Prospectus"
87+
compress_pdf: true
8788
8889
pages:
8990
- cover

docs/overview.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,21 @@ documents with minimal effort.
2323

2424
For a quick introduction, see the [README](../README.md).
2525

26+
## Examples
27+
28+
See the [examples](../examples) directory for simple working examples:
29+
30+
- [minimal](../examples/minimal) - Basic single page
31+
- [regular](../examples/regular) - Using various features
32+
- [variants](../examples/variants) - Different PDFs of the same document
33+
- [custom_processing](../examples/custom_processing) - Custom Python code
34+
2635
## Example Project Structure
2736

2837
```
2938
project/
30-
├── kiwipycon.yml # Main configuration
31-
├── material_specs/ # A document
39+
├── kiwipycon.yml # Main configuration
40+
├── material_specs/ # A document
3241
│ ├── config.yml # Document configuration
3342
│ ├── images/ # Images
3443
│ ├── pages/ # Page configurations

examples/custom_processing/bake.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Custom processing example - fetches latest XKCD comic."""
2+
3+
import base64
4+
import json
5+
import urllib.request
6+
from datetime import datetime
7+
8+
from pdfbaker.document import PDFBakerDocument
9+
from pdfbaker.errors import PDFBakeError
10+
11+
12+
def process_document(document: PDFBakerDocument) -> None:
13+
"""Process document with live XKCD comic."""
14+
try:
15+
# Fetch latest XKCD
16+
with urllib.request.urlopen("https://xkcd.com/info.0.json") as response:
17+
data = json.loads(response.read())
18+
19+
# Download and encode the image
20+
with urllib.request.urlopen(data["img"]) as img_response:
21+
img_data = img_response.read()
22+
image_data = (
23+
f"data:image/png;base64,{base64.b64encode(img_data).decode('utf-8')}"
24+
)
25+
26+
# Update config with XKCD info
27+
document.config["xkcd"] = {
28+
"title": data["title"],
29+
"alt_text": data["alt"],
30+
"fetched_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
31+
"image_data": image_data,
32+
}
33+
34+
# Process as usual
35+
document.process()
36+
except Exception as exc:
37+
raise PDFBakeError(f"Failed to process XKCD example: {exc}") from exc
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
filename: xkcd_example
2+
pages:
3+
- main
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
template: "main.svg.j2"
2+
title: "Custom Processing Example"
3+
title2: "pdfbaker data from web request"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<svg width="210mm" height="297mm" viewBox="0 0 210 297" version="1.1"
3+
xmlns="http://www.w3.org/2000/svg"
4+
xmlns:xlink="http://www.w3.org/1999/xlink">
5+
<rect width="210" height="297" fill="white"/>
6+
<!-- Header -->
7+
<text x="105" y="30" font-family="Arial" font-size="12" font-weight="bold" fill="#333" text-anchor="middle">{{ title }}</text>
8+
<text x="105" y="50" font-family="Arial" font-size="10" fill="#666" text-anchor="middle">{{ title2 }}</text>
9+
10+
<!-- XKCD Info -->
11+
<text x="105" y="80" font-family="Arial" font-size="10" font-weight="bold" fill="#333" text-anchor="middle">Latest XKCD:</text>
12+
<text x="105" y="95" font-family="Arial" font-size="10" fill="#333" text-anchor="middle">{{ xkcd.title }}</text>
13+
<text x="105" y="110" font-family="Arial" font-size="6" fill="#666" text-anchor="middle">Fetched at: {{ xkcd.fetched_at }}</text>
14+
15+
<!-- XKCD Image -->
16+
<image x="20" y="120" width="170" height="120" xlink:href="{{ xkcd.image_data }}"/>
17+
18+
<!-- Alt Text -->
19+
<text x="105" y="260" font-family="Arial" font-size="6" fill="#666" text-anchor="middle">Alt text: {{ xkcd.alt_text }}</text>
20+
</svg>

0 commit comments

Comments
 (0)