Updated 2012-09-21 00:14:53 by LkpPo

GWL A small routine to print a given file under Windows (note it blows up if the extension is not registered or if there is no print command for the extension -- feel free to add error checking):
 proc WindowsPrintFile {fileName {printer {}}} {
     package require csv

     ##
     ## Get the print command for this type
     ##
     set ext [file extension $fileName]
     set app [registry get [format {HKEY_CLASSES_ROOT\%s} $ext] {}]
     set app [format {HKEY_CLASSES_ROOT\%s\shell\print\command} $app]
     set cmdList {}
     foreach cmdElement [::csv::split [registry get $app {}] { }] {
          lappend cmdList [string map {%1 %1$s} $cmdElement]
     }

     ##
     ## Get the current default printer
     ##
     set currentDefaultReg [registry get {HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows} {Device}]
     set currentPrinter [lindex [split $currentDefaultReg {,}] 0]
     if {[string equal $printer {}]} {
         ##
         ## No printer specified, use default
         ##
         set printer $currentPrinter
     }

     ##
     ## Change default printer if need be
     ##
     if {![string equal $printer $currentPrinter]} {
         set tmpReg [join [concat [list $printer] [lrange [split $currentDefaultReg {,}] 1 end] ] {,} ]
         registry set {HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows} {Device} $tmpReg
     }

     ##
     ## Print the file -- catch is needed because return codes are not Unix ones!!!
     ##
     set cmd [format $printCommandArray($ext) $fileName]
     catch {eval exec $cmd} msg

     ##
     ## Restore default printer if need be
     ##
     if {![string equal $printer $currentPrinter]} {
         registry set {HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows} {Device} $currentDefaultReg
     }

     ##
     ## All done, so return 
     ##
     return;

 }

Sarnold: I recently found myself searching for a lpr replacement. IMHO PDF format is the best under Windows. This line prints directly a pdf file via Acrobat Reader:
 eval exec [auto_execok start] AcroRd32.exe /p [list [file normalize ~/Mes\ documents/report.pdf]]

Beware: I've found a quick and easy way of printing a canvas under Windoze. Install IrfanView on your PC, and then copy the exe to your application directory.

Export the canvas (or presumably anything you can capture with Img) to an image, then I do:
catch [exec i_view32.exe image.gif /print]

Which prints to the default printer...

Beware 18/2/2011: I wanted to use HTML to print some reports. The header and footer annoyed me, and since the script needed to run anywhere, I needed a way to turn the header/footer off. Here follows a quick and dirty solution:
proc printhtml {file} {
        # save current settings
        exec regedit /e temp.reg "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\PageSetup"
        
        # load blank header/footer
        exec regedit /s nomargins.reg
        
        # print
        exec rundll32.exe MSHTML.DLL,PrintHTML [file normalize $file]

        # restore original settings
        exec regedit /s temp.reg
}

where nomargins.reg contains:
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup]
"header"=" "
"footer"=" "
"margin_bottom"="0.75000"
"margin_left"="0.75000"
"margin_right"="0.75000"
"margin_top"="0.75000"
"font"=""
"Print_Background"="yes"
"Shrink_To_Fit"="yes"

It pops up the print dialog, which is fine for me.

See also: