Pdf.draw_text With a Real 0x011 Tab Char

460 views
Skip to first unread message

Randy Parker

unread,
Feb 10, 2012, 11:23:26 AM2/10/12
to prawn-ruby
My output is PDF with several QR codes, and the scanner reading the QR code is attached to a legacy system that interprets Tab as "jump to the next input field".

Is there any way to stick actual escape codes in the text?  Maybe something other than draw_text()? 

Randy Parker

unread,
Feb 10, 2012, 11:35:04 AM2/10/12
to prawn...@googlegroups.com
On Fri, Feb 10, 2012 at 11:25 AM, Ryan  wrote:

> Are you able to add the character to the QR code itself? 

I don't understand the question.
The only way I'm currently providing data to be encoded is via draw_text().
If you know of another technique to add characters, that's exactly what I want to know!

It turns out to be exceedingly convenient in many other ways to use Prawn to produce this qr-coded page.

- Randy

Brad Ediger

unread,
Feb 10, 2012, 11:41:59 AM2/10/12
to prawn...@googlegroups.com
On Fri, Feb 10, 2012 at 10:35 AM, Randy Parker <randy.j...@gmail.com> wrote:
> On Fri, Feb 10, 2012 at 11:25 AM, Ryan  wrote:
>
>> Are you able to add the character to the QR code itself?
>
> I don't understand the question.
> The only way I'm currently providing data to be encoded is via draw_text().
> If you know of another technique to add characters, that's exactly what I
> want to know!

Hmm, I'm not quite sure I understand. Are you asking whether you can
put literal ASCII escape characters (like \t) in the text of your
Prawn document? If so, that's probably not possible, depending on
exactly what it is you're trying to accomplish. PDF is more low-level
than that; the things that get drawn to the page are closer to glyphs,
not characters. (Think of PDF as a way of describing where the ink is
on the page, not what the document "means" or "is".)

Now you could possibly use a Unicode cmap to indicate that a
particular character maps to a tab. (The Unicode character map is the
feature that PDF uses to bridge the aforementioned gap between ink on
a page and actual characters, e.g., for a program that wants to
extract text from a PDF. It indicates the mapping between characters
from a font subset and Unicode code points.) But I'm not sure whether
that would have any relevance to the problem you're actually trying to
solve.

Can you go into a bit more detail about what you're trying to do so we
can see if it's feasible?

-be

Randy Parker

unread,
Feb 10, 2012, 12:04:07 PM2/10/12
to prawn...@googlegroups.com
On Fri, Feb 10, 2012 at 11:41 AM, Brad  wrote:
Are you asking whether you can
put literal ASCII escape characters (like \t) in the text of your
Prawn document? ...
 
 ... possibly use a Unicode cmap to indicate that a
particular character maps to a tab....

Can you go into a bit more detail ...


I print bundles of court documents, sometimes thousands per day.  These bundles are nested several layers deep, starting with the case, continuing with a variety of optional supporting documents, each of which is also nested with addenda, etc.  Each to-be-filed object ends up with "stacks" of various (required!) colors of NCR paper, according to the details of individual cases, which differ in each of several dozen jurisdictions.  I do all this in Prawn.  Rendering the PDFs on court-specific templates takes ~200ms per page.  Then we have a bunch of 6-tray production printers churning 'em out.

Some of the courts want to enter standard fields such as Plaintiff using QR codes.  Typically, the office of the Clerk of Court is not tech savvy at all, so it is a stretch of their ability to just plug in a barcode scanner and blip a half-dozen codes.  Forget about embedding tagged fields or XML in the QR codes - writing code to parse data structures would involve an extremely politicized county bureaucracy of staff IT folks and endless meetings.

So they want encoded data that matches _exactly_ their input screens:  First-name <TAB> Middle-initial <TAB> ...

So I've tried: \t, "0x011", vim's control-v <TAB>, which my terminal font renders as a sideways-pointing triangle, and which vim's ":ascii" command shows really is:  <^I> 9, Hex 09, Octal 011),   and everything else I can think of.  I've also tried passing the :skip-encoding => true  option that I spotted in the prawn source.

- Randy

Brad Ediger

unread,
Feb 10, 2012, 12:07:35 PM2/10/12
to prawn...@googlegroups.com
On Fri, Feb 10, 2012 at 11:04 AM, Randy Parker <randy.j...@gmail.com> wrote:
> I print bundles of court documents, sometimes thousands per day.  These
> bundles are nested several layers deep, starting with the case, continuing
> with a variety of optional supporting documents, each of which is also
> nested with addenda, etc.  Each to-be-filed object ends up with "stacks" of
> various (required!) colors of NCR paper, according to the details of
> individual cases, which differ in each of several dozen jurisdictions.  I do
> all this in Prawn.  Rendering the PDFs on court-specific templates takes
> ~200ms per page.  Then we have a bunch of 6-tray production printers
> churning 'em out.

First, let me say... cool! As a Prawn developer, I love hearing about
interesting uses of Prawn in the wild. And as the owner of a software
consultancy, I also love hearing about systems like this regardless!

> Some of the courts want to enter standard fields such as Plaintiff using QR
> codes.  Typically, the office of the Clerk of Court is not tech savvy at
> all, so it is a stretch of their ability to just plug in a barcode scanner
> and blip a half-dozen codes.  Forget about embedding tagged fields or XML in
> the QR codes - writing code to parse data structures would involve an
> extremely politicized county bureaucracy of staff IT folks and endless
> meetings.
>
> So they want encoded data that matches _exactly_ their input screens:
>  First-name <TAB> Middle-initial <TAB> ...

So unless I'm mistaken, it sounds like you need the tab characters
embedded in the QR codes, not in text in the PDF itself, right? How
are you generating your QR codes?

Ryan Stenhouse

unread,
Feb 10, 2012, 11:25:29 AM2/10/12
to prawn...@googlegroups.com
Hi Randy,

Are you able to add the character to the QR code itself? 


~ Ry

Randy Parker

unread,
Feb 10, 2012, 12:14:48 PM2/10/12
to prawn...@googlegroups.com
On Fri, Feb 10, 2012 at 12:07 PM, Brad wrote:

First, let me say... cool! As a Prawn developer, I love hearing about
interesting uses of Prawn in the wild. And as the owner of a software
consultancy, I also love hearing about systems like this regardless!

Prawn has been one of the nicest parts, honestly.

The worst has been Postscript, and HP's inability to support commercial users of their printers.
HP is a hollowed-out shell of what it once was.  It is sad.
I had to reverse-engineer my own printer drivers, because the CUPS drivers can't provide the page color (tray) flexibility I needed.  Fortunately, an ex-Sun Solaris CUPS developer lent a hand.
HP has tons of SNMP quirks and bugs...


So unless I'm mistaken, it sounds like you need the tab characters
embedded in the QR codes, not in text in the PDF itself, right? How
are you generating your QR codes?

Barby, which is a prawn wrapper for the actual encoding algorithm provided by rqrcode.
Both of those gems are very cleanly written: seacreature would be proud.

Brad Ediger

unread,
Feb 10, 2012, 12:21:03 PM2/10/12
to prawn...@googlegroups.com
On Fri, Feb 10, 2012 at 11:14 AM, Randy Parker <randy.j...@gmail.com> wrote:
> Prawn has been one of the nicest parts, honestly.
>
> The worst has been Postscript, and HP's inability to support commercial
> users of their printers.
> HP is a hollowed-out shell of what it once was.  It is sad.
> I had to reverse-engineer my own printer drivers, because the CUPS drivers
> can't provide the page color (tray) flexibility I needed.  Fortunately, an
> ex-Sun Solaris CUPS developer lent a hand.
> HP has tons of SNMP quirks and bugs...

I hear you there. Thankfully I've been able to avoid most of this mess
personally.

>> So unless I'm mistaken, it sounds like you need the tab characters
>> embedded in the QR codes, not in text in the PDF itself, right? How
>> are you generating your QR codes?
>
> Barby, which is a prawn wrapper for the actual encoding algorithm provided
> by rqrcode.
> Both of those gems are very cleanly written: seacreature would be proud.

OK, I've used Barby and I like it. Can you provide a code snippet that
shows what you're trying to do? I think this is a Barby question, not
a Prawn one.

-be

Randy Parker

unread,
Feb 10, 2012, 12:23:25 PM2/10/12
to prawn...@googlegroups.com
On Fri, Feb 10, 2012 at 12:14 PM, Randy Parker  wrote:

HP is a hollowed-out shell of what it once was.  It is sad.

Feeding jobs into an 535 MHz (the fastest available) HP printer without bogging down the rasterizing is a nightmare.  Perhaps HP should look into those 1 GHz dual-core chips that hundreds of millions of phones have deployed.  And a printer has neither heat nor power nor cost contraints (compared to a phone!!!) 

Randy Parker

unread,
Feb 10, 2012, 12:50:28 PM2/10/12
to prawn...@googlegroups.com
On Fri, Feb 10, 2012 at 12:21 PM, Brad wrote:
.... can you provide a code snippet that
shows what you're trying to do? I think this is a Barby question, not
a Prawn one.

A test program I used to fool with the QR code image size is below.
I still don't really understand the :size & :dim parameters, but the linear one-liner
you see in my little draw() method comes pretty close to producing a
30mm x 30mm QR code.  I also tried several area-based scalings which
didn't turn at as well as the linear one.
Basically, the QR code image size varies with the
number of bytes of data to be encoded, but some scanners want larger images.
So I've scaled my images to be ~ 30mm 
-------------------------------------------------- 

#!/usr/bin/env ruby

# What exactly does the Barby::QrCode#annotate_pdf parameter "size" do?
# Are there parameters that specify the output dimensions of a QR code pdf image?

require 'prawn'
require 'barby'
require 'barby/barcode/qr_code'
po_str =  defined?(Rails) ?  Rails.root.join('app/pdfs/prawn_outputter')  : '../../prawn_outputter'
require po_str
Prawn.debug = true

# The :width parameter to bounding_box is required, but does not change the size of the qr code.
def draw(pdf, str,  xy, dim)
  ww = 300
  dim = 3.0 - (0.015 * str.size)   # override the passed-in 'dim' parameter 
  #pdf.bounding_box xy, :width => ww, :skip_encoding => true do
  pdf.bounding_box xy, :width => ww                          do
    barcode = Barby::QrCode.new str, :level => :h
    barcode.annotate_pdf pdf, :xdim => dim, :ydim => dim
  end
  pdf.draw_text "dlen = #{str.size},  dim = #{dim}", :at => [xy.first + 200, xy.last + 20]
end

str ='aaa<TAB>bbb'    # gmail formatting goes bonkers if I embed the binary <TAB>
puts "encoding #{str.size} byte string"

pdf  = ::Prawn::Document.new :margin => [0,0,0,0]

draw(pdf, str[0,100],  [80, 600],  1.8)
#draw(pdf, str[0, 80],  [80, 450],  1.8)
#draw(pdf, str[0, 60],  [80, 270],  1.8)
#draw(pdf, str[0, 40],  [80, 150],  1.8)
#draw(pdf, str[0, 20],  [80,  50],  1.8)
#draw(pdf,         '',  [80,  50],  1.8)

fname = 'qr_output.pdf'
rc = File.open( fname, "wb") {|f| f.write pdf.render}
puts "PdfDocument.render_file wrote #{rc/1024} KB into a file named #{fname}"

exec "/Applications/Skim.app/Contents/MacOS/Skim  #{fname}"

Brad Ediger

unread,
Feb 10, 2012, 2:27:08 PM2/10/12
to prawn...@googlegroups.com
On Fri, Feb 10, 2012 at 11:50 AM, Randy Parker <randy.j...@gmail.com> wrote:
> A test program I used to fool with the QR code image size is below.
> I still don't really understand the :size & :dim parameters, but the linear
> one-liner
> you see in my little draw() method comes pretty close to producing a
> 30mm x 30mm QR code.

I tried embedding a tab in a QR code, and it worked fine. Here's my sample code:

https://gist.github.com/1791941

Scanning these with my mobile phone, they contained exactly the data I
requested -- the first was a five-byte string "hello", and the second
a six-byte string "hello\t". Pasting this into another application
yielded a hard tab character, just as expected.

If these aren't scanning correctly, I'm guessing it's a problem with
the barcode scanner you're using -- assuming it's a keyboard wedge,
perhaps it's not configured to pass through tab characters as tab
keypresses?

-be

Randy Parker

unread,
Feb 10, 2012, 2:33:30 PM2/10/12
to prawn...@googlegroups.com
On Fri, Feb 10, 2012 at 2:27 PM, Brad  wrote:

I tried embedding a tab in a QR code, and it worked fine....


If these aren't scanning correctly, I'm guessing it's a problem with
the barcode scanner you're using...

That never occurred to me - that the scanner itself could be the problem.

Which phone and scanner app are you using?

- Randy 
Reply all
Reply to author
Forward
0 new messages