perf(templates): raise PDF preview render quality

Page/preview images rasterized at 1400px (~165 DPI) and palette-quantized
to as few as 4-256 colours read as a blurry, banded scan in the builder
and preview. Raise the default render width to 2200px (~260 DPI, override
via PAGE_MAX_WIDTH) and write PDF pages as truecolour PNG instead of a
quantized palette. Larger cached blobs, but crisp text and logos.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
pull/697/head
shipeasy-ai 1 month ago
parent b2d7199756
commit 463eeabc5f

@ -12,7 +12,11 @@ module Templates
CONCURRENCY = 2 CONCURRENCY = 2
Q = 95 Q = 95
JPEG_Q = ENV.fetch('PAGE_QUALITY', '35').to_i JPEG_Q = ENV.fetch('PAGE_QUALITY', '35').to_i
MAX_WIDTH = 1400 # Width (px) every page/preview image is rasterized to. US-Letter at 1400px
# is only ~165 DPI, which reads as a blurry scan in the builder/preview;
# 2200px is ~260 DPI. Env-overridable (`PAGE_MAX_WIDTH`) so storage/CPU can
# be dialed back per environment.
MAX_WIDTH = ENV.fetch('PAGE_MAX_WIDTH', '2200').to_i
MAX_NUMBER_OF_PAGES_PROCESSED = 15 MAX_NUMBER_OF_PAGES_PROCESSED = 15
MAX_FLATTEN_FILE_SIZE = 20.megabytes MAX_FLATTEN_FILE_SIZE = 20.megabytes
GENERATE_PREVIEW_SIZE_LIMIT = 50.megabytes GENERATE_PREVIEW_SIZE_LIMIT = 50.megabytes
@ -140,10 +144,12 @@ module Templates
data = data =
if format == FORMAT if format == FORMAT
bitdepth = 2**page.stats.to_a[1..3].pluck(2).uniq.size # Full-colour PNG. The old palette path quantized each page to as few
# as 4256 colours, which bands anti-aliased text and any colour logo
page.write_to_buffer(format, compression: 6, filter: 0, bitdepth:, # — the second cause (with the low render width) of the "low quality
palette: true, Q: Q, dither: 0) # image" look. Truecolour keeps edges crisp at the cost of larger
# blobs, acceptable for the handful of pages a template has.
page.write_to_buffer(format, compression: 6, filter: 0)
else else
page.write_to_buffer(format, interlace: true, Q: JPEG_Q) page.write_to_buffer(format, interlace: true, Q: JPEG_Q)
end end

Loading…
Cancel
Save