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:
+15
-2
@@ -20,6 +20,7 @@ class Settings:
|
|||||||
TRIM_PADDING_PIXELS = 20
|
TRIM_PADDING_PIXELS = 20
|
||||||
MONOCHROME_THRESHOLD = 127
|
MONOCHROME_THRESHOLD = 127
|
||||||
OPTIPNG_OPTIMIZATION_LEVEL = 7
|
OPTIPNG_OPTIMIZATION_LEVEL = 7
|
||||||
|
PDF_BORDER_SIZE = 50
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
|
|
||||||
@@ -379,9 +380,21 @@ def finalize(output_pdf):
|
|||||||
|
|
||||||
print(f"Combining {len(temp_files)} pages into {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:
|
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
|
# Clean up temporary files
|
||||||
print("Cleaning up temporary files...")
|
print("Cleaning up temporary files...")
|
||||||
|
|||||||
@@ -23,6 +23,9 @@ DEFAULT_PARALLEL_PROCESSES = max(1, multiprocessing.cpu_count() - 1)
|
|||||||
# Can be overridden by environment variable
|
# Can be overridden by environment variable
|
||||||
PARALLEL_PROCESSES = int(os.getenv('NOTES_CLEANER_PARALLEL_PROCESSES', DEFAULT_PARALLEL_PROCESSES))
|
PARALLEL_PROCESSES = int(os.getenv('NOTES_CLEANER_PARALLEL_PROCESSES', DEFAULT_PARALLEL_PROCESSES))
|
||||||
|
|
||||||
|
# Border size in pixels for the final PDF output
|
||||||
|
PDF_BORDER_SIZE = 50
|
||||||
|
|
||||||
# Optimization settings
|
# Optimization settings
|
||||||
OPTIPNG_OPTIMIZATION_LEVEL = int(os.getenv('NOTES_CLEANER_OPTIPNG_LEVEL', '7')) # 0-7, higher = better compression but slower
|
OPTIPNG_OPTIMIZATION_LEVEL = int(os.getenv('NOTES_CLEANER_OPTIPNG_LEVEL', '7')) # 0-7, higher = better compression but slower
|
||||||
TRIM_PADDING_PIXELS = int(os.getenv('NOTES_CLEANER_TRIM_PADDING', '20')) # Padding around content after trimming
|
TRIM_PADDING_PIXELS = int(os.getenv('NOTES_CLEANER_TRIM_PADDING', '20')) # Padding around content after trimming
|
||||||
|
|||||||
Reference in New Issue
Block a user