Updated 2012-06-28 01:19:45 by RLE

SVH If you use the postscript export functionality from the canvas together with ghostscript, you can print your forms easily using following code. This code is based on Printing a canvas under Windows (bottom of page). Your form must be created first on a canvas scale 1:1 (path for the canvas is in $WIN(FRM)). This code is written for Windows environment, but should be easily modified for other environments, as ghostscript is available for multiple platforms. On other platforms the options -sDEVICE=mswinpr2 and -sOutputFile must be modified, see GhostScript documentation.
 Usage : (prefix commands with Print:: if you're working from a different namespace)

 OpenPrinter
 PrintForm (1 or multiple times)
 ClosePrinter

sx and sy are shifts to get the form on the right place on paper, this values are obtained by calibration. These values may not be the same for different printers. fwdt and fhgt are form width and height, the canvas must have the same size.
 #
 #
 proc Print::OpenPrinter {} {

    variable WIN
    variable FLD
    variable CUR

    # Preprinted form is shown in the canvas as an image,
    # This image must be removed temporarily during the printing
    $WIN(FRM) itemconfigure img -state hidden
    update

    set gspath {C:\\gs\\gs8.51\\bin\\gswin32c.exe}
    set opt1 "-q"
    if {$FLD(Printer) != "Select from Printer Dialog"} {
        set opt2 "-sDEVICE=mswinpr2 -sOutputFile=\"%printer%$FLD(Printer)\""
    }\
    else {
        set opt2 "-sDEVICE=mswinpr2"
    }
    set opt3 "-dNOPAUSE -dBATCH"
    set CUR(gs) [open "|\"$gspath\" $opt1 $opt2 $opt3 -" w]
 }
 
 #
 #
 proc Print::PrintForm {} {

    variable WIN
    variable FLD
    variable CUR
    variable Msg ""

    # Form definition is stored in a BLT tree
    # Values are obtained by procedure GetValues var1 var2 ...
    GetValues sx sy fwdt fhgt unit rot
    if {$rot} {
        set sftx [expr {$sx + $fwdt / 2}]
        set sfty [expr {$sy + $fhgt / 2}]
    }\
    else {
        set sftx [expr {-1 * $sy + $fwdt / 2}]
        set sfty [expr {$sx + $fhgt / 2}]
    }

    $WIN(FRM) yview moveto 0
    $WIN(FRM) xview moveto 0

    $WIN(FRM) postscript -channel $CUR(gs) -pagey $sftx$unit -pagex $sfty$unit -rotate $rot\
        -width $fwdt$unit -height $fhgt$unit -pageheight $fhgt$unit -pagewidth $fwdt$unit -x 0 -y 0
 }
 
 #
 #
 proc Print::ClosePrinter {} {

    variable WIN
    variable CUR
    variable Msg "" ;# variable attached to status bar

    catch {close $CUR(gs)} Msg
    array unset CUR gs

    $WIN(FRM) itemconfigure img -state normal
    update
 }