Skip to content

Commit f3831ea

Browse files
committed
Improve examples, use wordwrap
1 parent 4e50ef0 commit f3831ea

File tree

12 files changed

+77
-15
lines changed

12 files changed

+77
-15
lines changed

examples/custom_locations/other_pages/custom_page.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@ title: "Custom Location Example"
22
description: "This page uses custom directory structure"
33
template:
44
# If you just wrote this directly it would be relative to the templates directory
5-
# We want it to be relative to the config file:
5+
# We want it to be relative to the config file, so use path:
66
path: "../other_templates/custom_page.svg.j2"
7+
detailed_description:
8+
"This example demonstrates custom file locations in pdfbaker. The template file is in
9+
a custom directory (other_templates/), the page configuration file is in another
10+
custom directory (other_pages/), and the document configuration uses a path-based page
11+
reference with custom location."
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
22
<svg width="210mm" height="297mm" viewBox="0 0 210 297" version="1.1">
3-
<text x="105" y="50" text-anchor="middle" font-size="20">{{ title }}</text>
4-
<text x="105" y="80" text-anchor="middle" font-size="14">{{ description }}</text>
3+
<!-- Background -->
4+
<rect width="210" height="297" fill="#ffffff"/>
5+
6+
<!-- Light blue header background -->
7+
<rect x="0" y="0" width="210" height="70" fill="#e0f0ff"/>
8+
9+
<!-- Title with smaller font -->
10+
<text x="105" y="40" text-anchor="middle" font-family="Arial" font-size="14" font-weight="bold">{{ title }}</text>
11+
12+
<!-- Description with smaller font -->
13+
<text x="105" y="60" text-anchor="middle" font-family="Arial" font-size="8">{{ description }}</text>
14+
15+
<!-- Detailed description using wordwrap filter -->
16+
{% for line in detailed_description | wordwrap(45) %}
17+
<text x="20" y="{{ 100 + loop.index0 * 15 }}" font-family="Arial" font-size="8">{{ line }}</text>
18+
{% endfor %}
519
</svg>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
title: "Standard Location Example"
22
subtitle: "This page uses conventional directory structure"
33
template: "standard_page.svg.j2"
4+
description:
5+
"This example demonstrates how pdfbaker organizes files with standard locations.
6+
Templates are in the standard templates/ directory, page configuration files are in
7+
the standard pages/ directory, and the document uses simple page references."
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
22
<svg width="210mm" height="297mm" viewBox="0 0 210 297" version="1.1">
3-
<text x="105" y="50" text-anchor="middle" font-size="20">{{ title }}</text>
4-
<text x="105" y="80" text-anchor="middle" font-size="14">{{ subtitle }}</text>
3+
<!-- Background -->
4+
<rect width="210" height="297" fill="#ffffff"/>
5+
6+
<!-- Gray header background -->
7+
<rect x="0" y="0" width="210" height="70" fill="#f0f0f0"/>
8+
9+
<!-- Title with smaller font -->
10+
<text x="105" y="40" text-anchor="middle" font-family="Arial" font-size="14" font-weight="bold">{{ title }}</text>
11+
12+
<!-- Subtitle with smaller font -->
13+
<text x="105" y="60" text-anchor="middle" font-family="Arial" font-size="8">{{ subtitle }}</text>
14+
15+
<!-- Description text using wordwrap filter -->
16+
{% for line in description | wordwrap(45) %}
17+
<text x="20" y="{{ 100 + loop.index0 * 15 }}" font-family="Arial" font-size="8">{{ line }}</text>
18+
{% endfor %}
519
</svg>

examples/custom_processing/bake.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from pdfbaker.document import PDFBakerDocument
99
from pdfbaker.errors import PDFBakerError
10+
from pdfbaker.processing import wordwrap
1011

1112

1213
def process_document(document: PDFBakerDocument) -> None:
@@ -23,10 +24,15 @@ def process_document(document: PDFBakerDocument) -> None:
2324
f"data:image/png;base64,{base64.b64encode(img_data).decode('utf-8')}"
2425
)
2526

26-
# Update config with XKCD info
27+
# Get the alt text and split it into lines using the wordwrap function
28+
# Note: This is for demonstration. Could use the wordwrap filter in template.
29+
wrapped_alt_text = wordwrap(data["alt"], max_chars=60)
30+
31+
# Update config/template context with XKCD info
2732
document.config["xkcd"] = {
2833
"title": data["title"],
2934
"alt_text": data["alt"],
35+
"alt_text_lines": wrapped_alt_text,
3036
"fetched_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
3137
"image_data": image_data,
3238
}

examples/custom_processing/templates/main.svg.j2

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<!-- XKCD Image -->
1616
<image x="20" y="120" width="170" height="120" xlink:href="{{ xkcd.image_data }}"/>
1717

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>
18+
<!-- Centered Alt Text (Pre-wrapped in Python) -->
19+
{% for line in xkcd.alt_text_lines %}
20+
<text x="105" y="{{ 260 + loop.index0 * 8 }}" font-family="Arial" font-size="5" fill="#666" text-anchor="middle">{{ line }}</text>
21+
{% endfor %}
2022
</svg>

examples/examples.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ documents:
22
- minimal
33
- regular
44
- variants
5-
- "./custom_locations/your_directory"
5+
- ./custom_locations/your_directory
66
- custom_processing

examples/minimal/pages/main.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ content: |-
44
This is a minimal example of pdfbaker usage.
55
- Single page document
66
- One custom variable (text_color)
7-
- Basic SVG template using variable for text color
7+
- Basic SVG template uses variable for styling

examples/regular/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ pages:
66
- benefits
77
style:
88
highlight_color: "#30987c"
9+
header_title: "pdfbaker Example"

examples/regular/pages/features.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@ items:
1010
desc: "Same template used by multiple pages"
1111
- title: "Consistent Styling"
1212
desc: "Shared styles across pages"
13+
- title: "Wrapping Long Text"
14+
desc:
15+
"SVG doesn't have text wrapping containers, but pdfbaker lets you store long text
16+
in config and use the wordwrap filter to split it into lines. Adjust max_chars to
17+
fit narrower spaces."
1318
images:
1419
- name: pythonnz.png

0 commit comments

Comments
 (0)