Tuesday, 30 July 2013

PDF barcode

The following script uses last post PDF maker to create barcode, here is an example:

do pdf-maker.r
text-string: "item2342"
x-offset: 10 ; millimeters from the left edge of the page
y-offset: 257 ; millimeters from the bottom edge of the page
arcode-layout: copy []
current-barcode-page: copy [page size 215.9 279.4 offset 0 0]
append current-barcode-page create-pdf-barcode text-string x-offset y-offset

; The following block is not necessary. It just adds human readable text
; to the printout:

append current-barcode-page compose/deep [
(x-offset - 9.5) (y-offset - 8)
56 8
center font Helvetica 3
(mold text-string)

append/only barcode-layout current-barcode-page
write/binary %labels.pdf layout-pdf barcode-layout
call %labels.pdf

Here is the result:

Here is the source:

    title: "PDF Bar Code Generator"
    date: 14-Mar-2010
    file: %pdf-barcode.r
    author:   Nick Antonaccio
    purpose: {
        Takes a given string and XxY coordinate (in millimeters), and outputs
        a PDF file containing a printable bar code at the given position.   The bar
        code algorithm is derived directly from Bohdan Lechnowsky's "code39.r",
        and the PDF is generated using Gabriele Santilli's "pdf-maker.r".   This
        script was created because images output by the original code39.r
        script would become blurred when inserted and resized by pdf-maker.r.
        Here, the bars are rendered as lines, directly in pdf-maker dialect.   The
        images generated are crisp and easily scannable.
create-pdf-barcode: func [barcode-string xshift yshift] [
    barcode-width: .3   barcode-height: 12  
    code39: first to-block decompress #{

    convfrom: rejoin ["*" barcode-string "*"]
    pdf-dialect-out: copy []
    x: 0
    foreach char convfrom [
        pattern: select code39 form char
        foreach bit pattern [
            x: x + 1
            if bit = #"1" [
                append pdf-dialect-out compose [
                    line width (barcode-width)
                    ((x * barcode-width) + xshift) (yshift)
                    ((x * barcode-width) + xshift) (yshift + barcode-height)
        x: x + 1
    return pdf-dialect-out

No comments:

Post a Comment