Fit PDF pages wider than the label onto it
deploy / deploy (push) Successful in 9s

A label in landscape or a letter page was cut off: the PDF was rendered
1:1 at 203 dpi and the Datamax printed only what fits its width. Pages
wider than the A6 label (105 x 148 mm, as set up in the printer) are now
rendered again fitted onto the label, which Ghostscript turns to portrait
when that fits better. Pages that fit, like the shipping labels, still
print 1:1.
This commit is contained in:
randy committed 2026-11-01 19:00:25 +01:00
1 parent 0f7c18141b
commit 4d366e8470
2 files changed
+28 -9

No files matched your search

+1 -1
View File
@@ -9,7 +9,7 @@ Unified UI for printing labels/receipts:
== Routes == Routes
* `/` -- home page, links to everything below * `/` -- home page, links to everything below
* `/upload`, `POST /upload` -- upload a PDF/ZPL file and print it (Datamax) * `/upload`, `POST /upload` -- upload a PDF/ZPL file and print it (Datamax). PDF pages wider than the A6 label (e.g. letter pages or labels in landscape) are turned and fitted onto it, others print 1:1.
* `POST /shipping/zpl` -- print raw ZPL text (see "API usage" below) * `POST /shipping/zpl` -- print raw ZPL text (see "API usage" below)
* `/test-print` -- print the bundled Datamax test label * `/test-print` -- print the bundled Datamax test label
* `/epson` -- receipt printing UI (title/body/QR template, or raw ESC/POS mnemonic notation) * `/epson` -- receipt printing UI (title/body/QR template, or raw ESC/POS mnemonic notation)
+27 -8
View File
@@ -22,6 +22,7 @@ end
# controller -- back to the kernel driver via a stable udev symlink instead. # controller -- back to the kernel driver via a stable udev symlink instead.
PRINTER_DEVICE = '/dev/usb/lp_datamax' # see /etc/udev/rules.d/99-datamax.rules PRINTER_DEVICE = '/dev/usb/lp_datamax' # see /etc/udev/rules.d/99-datamax.rules
PRINTER_DPI = 203 # DatamaxONeil E4204B Mark III native head resolution PRINTER_DPI = 203 # DatamaxONeil E4204B Mark III native head resolution
LABEL_DOTS = [840, 1183] # 105 x 148 mm (A6) labels, as set up in the printer
PRINTER_DARKNESS = 8 # lower than factory default to stop thermal dot-gain from fusing barcode bars PRINTER_DARKNESS = 8 # lower than factory default to stop thermal dot-gain from fusing barcode bars
TEST_LABEL_PDF = File.join(File.dirname(__FILE__), 'test-label.pdf') TEST_LABEL_PDF = File.join(File.dirname(__FILE__), 'test-label.pdf')
@@ -61,19 +62,37 @@ helpers do
# Renders a PDF to ZPL ^GFA graphic labels at the printer's native DPI, # Renders a PDF to ZPL ^GFA graphic labels at the printer's native DPI,
# bypassing CUPS/Gutenprint's raster pipeline which blurs text and 1px-shifts # bypassing CUPS/Gutenprint's raster pipeline which blurs text and 1px-shifts
# output compared to native ZPL rendering. # output compared to native ZPL rendering.
# Pages wider than the label (e.g. a letter page, or a label in
# landscape) are fitted onto the label instead, which Ghostscript turns
# to portrait when that fits better. Pages that fit print 1:1.
def pdf_to_zpl(pdf_path, dpi: PRINTER_DPI) def pdf_to_zpl(pdf_path, dpi: PRINTER_DPI)
Dir.mktmpdir do |dir| Dir.mktmpdir do |dir|
pattern = File.join(dir, 'page-%03d.pbm') pages = render_pbm(pdf_path, dir, dpi)
ok = system('gs', '-q', '-dNOPAUSE', '-dBATCH', '-dSAFER', if pages.any? { |f| pbm_size(f)[0] > LABEL_DOTS[0] }
'-sDEVICE=pbmraw', "-r#{dpi}", pages.each { |f| File.delete(f) }
'-dTextAlphaBits=4', '-dGraphicsAlphaBits=4', pages = render_pbm(pdf_path, dir, dpi, "-g#{LABEL_DOTS.join('x')}", '-dFIXEDMEDIA', '-dPDFFitPage')
"-sOutputFile=#{pattern}", pdf_path) end
raise 'ghostscript rendering failed' unless ok pages.map { |f| pbm_to_zpl_label(f) }.join
Dir.glob(File.join(dir, 'page-*.pbm')).sort.map { |f| pbm_to_zpl_label(f) }.join
end end
end end
def render_pbm(pdf_path, dir, dpi, *options)
ok = system('gs', '-q', '-dNOPAUSE', '-dBATCH', '-dSAFER',
'-sDEVICE=pbmraw', "-r#{dpi}", *options,
'-dTextAlphaBits=4', '-dGraphicsAlphaBits=4',
"-sOutputFile=#{File.join(dir, 'page-%03d.pbm')}", pdf_path)
raise 'ghostscript rendering failed' unless ok
Dir.glob(File.join(dir, 'page-*.pbm')).sort
end
def pbm_size(pbm_path)
header = /\AP4\s+(?:#[^\n]*\n\s*)*(\d+)\s+(\d+)\s/.match(File.binread(pbm_path, 512))
raise "unreadable PBM header in #{pbm_path}" unless header
[header[1].to_i, header[2].to_i]
end
def pbm_to_zpl_label(pbm_path) def pbm_to_zpl_label(pbm_path)
bytes = File.binread(pbm_path) bytes = File.binread(pbm_path)
header = /\AP4\s+(?:#[^\n]*\n\s*)*(\d+)\s+(\d+)\s/.match(bytes) header = /\AP4\s+(?:#[^\n]*\n\s*)*(\d+)\s+(\d+)\s/.match(bytes)