Skip to content

Commit d6382c0

Browse files
committed
Simplify docs for custom_processing
1 parent afa0550 commit d6382c0

1 file changed

Lines changed: 9 additions & 73 deletions

File tree

docs/custom_processing.md

Lines changed: 9 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -10,86 +10,22 @@ customize the document generation process. This allows you to:
1010

1111
## Basic Structure
1212

13-
Your `bake.py` should define a `process_document` function:
13+
As a naming convention, your `bake.py` needs to define a `process_document` function:
1414

1515
```python
1616
from pdfbaker.document import PDFBakerDocument
1717

1818
def process_document(document: PDFBakerDocument) -> None:
1919
# Custom processing logic here
20-
pass
20+
return document.process()
2121
```
2222

23-
## Document Object
23+
You will usually just manipulate the data for your templates, and then call `.process()`
24+
on the document to continue with the built-in stages of combining pages and compressing
25+
the PDF as configured.
2426

25-
The `document` parameter provides access to:
27+
See `examples/custom_processing/bake.py` for a simple example of how to do this.
2628

27-
- Document configuration
28-
- Variant processing
29-
- Page rendering
30-
- File management
31-
32-
### Key Methods and Properties
33-
34-
```python
35-
# Access configuration
36-
config = document.config
37-
38-
# Process variants
39-
for variant in document.config.get('variants', []):
40-
# Process variant...
41-
42-
# Process pages
43-
for page in document.config.get('pages', []):
44-
# Process page...
45-
46-
# File management
47-
build_dir = document.build_dir
48-
dist_dir = document.dist_dir
49-
```
50-
51-
## Example: Dynamic Pricing
52-
53-
Here's an example that calculates dynamic pricing based on features:
54-
55-
```python
56-
def process_document(document):
57-
# Load pricing data
58-
with open('content/pricing_data.yaml') as f:
59-
pricing_data = yaml.safe_load(f)
60-
61-
# Calculate pricing for each variant
62-
for variant in document.config.get('variants', []):
63-
base_price = document.config['content']['base_price']
64-
features = len(variant['content']['features'])
65-
66-
# Adjust price based on features
67-
adjusted_price = base_price * (1 + (features - 1) * 0.1)
68-
final_price = adjusted_price * (1 - variant['content']['discount'])
69-
70-
# Update variant content
71-
variant['content']['final_price'] = round(final_price, 2)
72-
73-
# Process as usual
74-
document.process()
75-
```
76-
77-
## Example: Content Generation
78-
79-
Generate content dynamically based on external data:
80-
81-
```python
82-
def process_document(document):
83-
# Fetch data from API
84-
response = requests.get('https://api.example.com/data')
85-
data = response.json()
86-
87-
# Update document content
88-
document.config['content'].update({
89-
'latest_data': data,
90-
'generated_at': datetime.now().isoformat()
91-
})
92-
93-
# Process as usual
94-
document.process()
95-
```
29+
If you need to fully customise the processing, make sure that your function returns a
30+
list of Path objects (the PDF files that were created) as that is the expected type of
31+
return value for logging.

0 commit comments

Comments
 (0)