diff --git a/README.adoc b/README.adoc index 8931aa7..01c19f3 100644 --- a/README.adoc +++ b/README.adoc @@ -9,7 +9,7 @@ Unified UI for printing labels/receipts: == Routes * `/` -- 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) * `/test-print` -- print the bundled Datamax test label * `/epson` -- receipt printing UI (title/body/QR template, or raw ESC/POS mnemonic notation) diff --git a/app.rb b/app.rb index e93869e..b4c6118 100644 --- a/app.rb +++ b/app.rb @@ -22,6 +22,7 @@ end # 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_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 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, # bypassing CUPS/Gutenprint's raster pipeline which blurs text and 1px-shifts # 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) Dir.mktmpdir do |dir| - pattern = File.join(dir, 'page-%03d.pbm') - ok = system('gs', '-q', '-dNOPAUSE', '-dBATCH', '-dSAFER', - '-sDEVICE=pbmraw', "-r#{dpi}", - '-dTextAlphaBits=4', '-dGraphicsAlphaBits=4', - "-sOutputFile=#{pattern}", pdf_path) - raise 'ghostscript rendering failed' unless ok - - Dir.glob(File.join(dir, 'page-*.pbm')).sort.map { |f| pbm_to_zpl_label(f) }.join + pages = render_pbm(pdf_path, dir, dpi) + if pages.any? { |f| pbm_size(f)[0] > LABEL_DOTS[0] } + pages.each { |f| File.delete(f) } + pages = render_pbm(pdf_path, dir, dpi, "-g#{LABEL_DOTS.join('x')}", '-dFIXEDMEDIA', '-dPDFFitPage') + end + pages.map { |f| pbm_to_zpl_label(f) }.join 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) bytes = File.binread(pbm_path) header = /\AP4\s+(?:#[^\n]*\n\s*)*(\d+)\s+(\d+)\s/.match(bytes)