Improve PDF generation with proper A4 sizing and borders

- Move PDF border size to settings.py
- Use standard A4 dimensions (210x297mm)
- Fix aspect ratio preservation in layout function
- Use img2pdf's built-in layout function with proper border specification
This commit is contained in:
Esa Kataja
2024-12-03 22:53:23 +02:00
parent 0f105880e6
commit a78b9c25d1
2 changed files with 18 additions and 2 deletions
+15 -2
View File
@@ -20,6 +20,7 @@ class Settings:
TRIM_PADDING_PIXELS = 20
MONOCHROME_THRESHOLD = 127
OPTIPNG_OPTIMIZATION_LEVEL = 7
PDF_BORDER_SIZE = 50
settings = Settings()
@@ -379,9 +380,21 @@ def finalize(output_pdf):
print(f"Combining {len(temp_files)} pages into {output_pdf}...")
# Convert to PDF
# A4 size in millimeters
A4_WIDTH_MM = 210
A4_HEIGHT_MM = 297
# Convert to PDF with border
with open(output_pdf, "wb") as f:
f.write(img2pdf.convert(temp_files))
f.write(img2pdf.convert(
temp_files,
with_pdfrw=True,
layout_fun=img2pdf.get_layout_fun(
pagesize=(img2pdf.mm_to_pt(A4_WIDTH_MM), img2pdf.mm_to_pt(A4_HEIGHT_MM)),
border=(settings.PDF_BORDER_SIZE,) * 4, # Same border size for all sides (top, right, bottom, left)
fit=img2pdf.FitMode.into
)
))
# Clean up temporary files
print("Cleaning up temporary files...")