Jump to content

Send a raw file to a printer?


Recommended Posts

I've searched the forums, but can't find any solution to this problem.

I would like a script to be able to send a raw file to a specific printer (typically a Postscript or PCL file). If anyone has already figured out how to do this, I would be grateful for any advice.

Link to comment
Share on other sites

I've searched the forums, but can't find any solution to this problem.

I would like a script to be able to send a raw file to a specific printer (typically a Postscript or PCL file). If anyone has already figured out how to do this, I would be grateful for any advice.

$file = "C:/Test.txt" ; or whatever your file is


$printfile = _FilePrint($file)

im a newbie here but that would send it to the default printer i guess

Edited by SparkSoft

[center]First Ever Script/App[/center][center]Simple Battery Meter[/center]

Link to comment
Share on other sites

$file = "C:/Test.txt" ; or whatever your file is

$printfile = _FilePrint($file)

im a newbie here but that would send it to the default printer i guess

Thank you. Unfortunately, this doesn't send the file directly to the default printer. Instead, it uses the default Windows program for whatever type of file the file happens to be. If it's a Word file, the file opens in Word and gets sent to the printer by Word. This doesn't work for a raw PostScript of PCL file. What I was hoping Autoit could do was something like what this does:

http://www.perilous.com/

I suppose I could include a program of that kind in the compiled script, but it would be more efficient to have the script do the job directly.

Link to comment
Share on other sites

Back in the DOS days, you could send a text file to the default printer with the command:

copy [path]\[filename] > PRN

example:

copy c:\Documents and Settings\Owner\My Documents\2010 Documents\store.txt > PRN

I think this should work for you using the hidden command shell.

Link to comment
Share on other sites

Back in the DOS days, you could send a text file to the default printer with the command:

copy [path]\[filename] > PRN

example:

copy c:\Documents and Settings\Owner\My Documents\2010 Documents\store.txt > PRN

I think this should work for you using the hidden command shell.

Thank you for that. Unfortunately, it doesn't work in modern Windows systems, and doesn't let you select the printer you want to send it to, which is fairly important in many current systems.
Link to comment
Share on other sites

Thank you for that. Unfortunately, it doesn't work in modern Windows systems, and doesn't let you select the printer you want to send it to, which is fairly important in many current systems.

Maybe you could do this.

Note the default printer. Change it to the printer you want. Send the file to the printer. Set the default printer back.

There are functions around to set the default printer so all this could be made into a script. The link in my signature to my print udf could do this but there are simpler examples, perhaps this would help.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Maybe you could do this.

Note the default printer. Change it to the printer you want. Send the file to the printer. Set the default printer back.

There are functions around to set the default printer so all this could be made into a script. The link in my signature to my print udf could do this but there are simpler examples, perhaps this would help.

I've been working with your very impressive UDF and with the techniques in the thread that you linked to. Many thanks for these. You made it easy for me to ask the user which printer to use for direct printing; also (if necessary) how to switch to a printer and then back again.

The one problem that I still haven't solved is: how to send a raw PCL or PostScript file directly to the printer, without using ShellExecute (which wouldn't work on most systems anyway with raw PCL or PostScript code). I know that I can use a third-party utility and include it in a compiled script, then copy it from the compiled script to the %temp% folder and execute it from that (I've tried this, and it works), but it would be far more elegant and efficient to use AutoIt for this purpose.

Is there any chance that your UDF might include a "raw print" function someday?? I realize that it's selfish of me to ask for something more after you've already given us so much!

Link to comment
Share on other sites

I've been working with your very impressive UDF and with the techniques in the thread that you linked to. Many thanks for these. You made it easy for me to ask the user which printer to use for direct printing; also (if necessary) how to switch to a printer and then back again.

The one problem that I still haven't solved is: how to send a raw PCL or PostScript file directly to the printer, without using ShellExecute (which wouldn't work on most systems anyway with raw PCL or PostScript code). I know that I can use a third-party utility and include it in a compiled script, then copy it from the compiled script to the %temp% folder and execute it from that (I've tried this, and it works), but it would be far more elegant and efficient to use AutoIt for this purpose.

Is there any chance that your UDF might include a "raw print" function someday?? I realize that it's selfish of me to ask for something more after you've already given us so much!

printing a raw file should be the same as sending a binary string/array to the printer I would guess in which case may UDF can already do it.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Ah - I see! I think I need to use something like this:

#include <file.au3>
#include 'printMGv2.au3'
_FileReadToArray($mypath & "\OUTPUT.PS", $aData) 
_PrintBytesDirect($hDll, $sPrinter, $aData)

But unfortunately I'm not expert enough to figure out how to get this to work in my script. I realize this is an annoying and ignorant question, but I would be very grateful for any help in implementing this. (How do I get a value into $hDll, for example? I see how to get $sPrinter, but not $hDll.) Many thanks again. This is clearly very close to the solution I'm looking for.

Link to comment
Share on other sites

Ah - I see! I think I need to use something like this:

#include <file.au3>
#include 'printMGv2.au3'
_FileReadToArray($mypath & "\OUTPUT.PS", $aData) 
_PrintBytesDirect($hDll, $sPrinter, $aData)

But unfortunately I'm not expert enough to figure out how to get this to work in my script. I realize this is an annoying and ignorant question, but I would be very grateful for any help in implementing this. (How do I get a value into $hDll, for example? I see how to get $sPrinter, but not $hDll.) Many thanks again. This is clearly very close to the solution I'm looking for.

$hDll is returned by _PrintDllStart which should be the first function used. If the return is not -1 (failed) then the value must be usd in all later printer functions.

On reflection, the _PrintBytesDirect function was written just for something someone wanted and I'm not sure if it worked as required and I actually don't know if it will do what you want. You are welcome to try of course and this is the sequence I think you should use.

_PrintDllStart
_PrintSetPrinter/_PrintSelectPrinter
_PrintStartPrint
_PrintBytesDirect($hDll, $sPrinter, $aData)
_PrintEndPrint

The size of the array could be reduced by reading part o fthe file at a time. Have a look at the _WinAPI_CreateFile, _WinAPI_SetFilePointer etc.

I'll have a play with this during the next day or two anyway because I'm not confident about it at all.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Thanks for the detailed reply. I've tried what you suggested, but I'm obviously doing something wrong, because I get an error message saying "Could not open printer 1" and then a blank page prints.

I know this looks very ignorant, but here's the code I used (it's an embarrassing mess, but I'm trying to learn...)

#include <file.au3>
#include 'printMGv2.au3'
Global $hDll, $mmssgg, $sPrinter, $aData, $aFile
$aFile = "K:\gswp\wptemp.ps"
$hDll = _PrintDllStart($mmssgg)
$sPrinter = _PrintSetPrinter($hDll)
_FileReadToArray($aFile, $aData)
_PrintStartPrint($hDll, $sPrinter)
_PrintBytesDirect($hDll, $sPrinter, $aData)
_PrintEndPrint($hDll)
Exit

Thanks again for any help with this.

Link to comment
Share on other sites

Thanks for the detailed reply. I've tried what you suggested, but I'm obviously doing something wrong, because I get an error message saying "Could not open printer 1" and then a blank page prints.

I know this looks very ignorant, but here's the code I used (it's an embarrassing mess, but I'm trying to learn...)

#include <file.au3>
#include 'printMGv2.au3'
Global $hDll, $mmssgg, $sPrinter, $aData, $aFile
$aFile = "K:\gswp\wptemp.ps"
$hDll = _PrintDllStart($mmssgg)
$sPrinter = _PrintSetPrinter($hDll)
_FileReadToArray($aFile, $aData)
_PrintStartPrint($hDll, $sPrinter)
_PrintBytesDirect($hDll, $sPrinter, $aData)
_PrintEndPrint($hDll)
Exit

Thanks again for any help with this.

_FileReadToArray is not the function to use and I should have thought about that when I replied before.

Here is a sample script which worked for me.

#include 'printMGv2.au3'

Global $hDll, $mmssgg, $sPrinter, $aData, $aFile, $bData, $str1, $pstr1, $n

$aFile = "C:\temp\testbinaryprint";"K:\gswp\wptemp.ps"

$isize = FileGetSize($aFile)
Dim $aData[$isize]
$f = fileopen($aFile,16);open in binary
$bData = FileRead($f)
fileclose($f)

$str1 = DllStructCreate("byte[" & $isize & "]")
$pstr1 = DllStructGetPtr($str1)

 
 DllStructSetData($str1,1,$bData);thanks to Ascend4nt for pointing out the obvious to me.

$hDll = _PrintDllStart($mmssgg)
if @error <> 0 Then
    MsgBox(262144,"ERROR starting print dll",$mmssgg)
    Exit
EndIf

switch _PrintSetPrinter($hDll)
    case 0
        MsgBox(262144,"Error","Printer Cancelled by User")
        exit
    case -1
        MsgBox(262144,"Error", "Could not set printer")
        Exit
    case 1
        ;succeess, do nothing
EndSwitch


$sPrinter = _PrintGetPrinter($hDll)
if $sPrinter = "" then Exit

;if _PrintStartPrint($hDll) <> 1 then Exit

DllCall($hDll, 'int', 'PrintBytesDirect', 'str',$sPrinter, 'ptr', $pstr1, 'int', $isize)

;_PrintEndPrint($hDll)
Exit

It might be worth posting somethng in general help to get someone to tell you how to get a binary string into an array because I can't think how to do it at the moment and the way I did it is too slow, but I can't spend any more time on it now.

You need to read the functions more carefully, perhaps more slowly. For example, you thought that _PrintSetPrinter returned the printer name but it doesn't. You will see I've used _PrintGetPrinter for that.

I discovered that when you print this way you don't need the _PrintStartPrint and _PrintEndPrint. In fact if you use them it will still work but an extra blank page is churned out.

It was interesting to see that my binary function actually works!

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

_FileReadToArray is not the function to use and I should have thought about that when I replied before.

Here is a sample script which worked for me.

[...]

It might be worth posting somethng in general help to get someone to tell you how to get a binary string into an array because I can't think how to do it at the moment and the way I did it is too slow, but I can't spend any more time on it now.

You need to read the functions more carefully, perhaps more slowly. For example, you thought that _PrintSetPrinter returned the printer name but it doesn't. You will see I've used _PrintGetPrinter for that.

I discovered that when you print this way you don't need the _PrintStartPrint and _PrintEndPrint. In fact if you use them it will still work but an extra blank page is churned out.

It was interesting to see that my binary function actually works!

Martin,

That works absolutely perfectly, and it's deeply generous of you to take the time to sort it out. You're right that I hadn't paid enough attention to the functions - I'm still working my way through the extraordinary list of things that this UDF can accomplish.

I'll ask in a separate thread about getting a binary string into an array.

(Off-topic: one possible addition to your UDF for some time in the future - some way of querying a printer directly to see if it can handle PCL or PostScript. I know that there are ways to query a PostScript printer for its capabilities, but I've never seen it done in a script.)

Thank you again for generosity well beyond any call of duty!

Link to comment
Share on other sites

That works absolutely perfectly, and it's deeply generous of you to take the time to sort it out. You're right that I hadn't paid enough attention to the functions - I'm still working my way through the extraordinary list of things that this UDF can accomplish.

Martin,

May I bother you with one more question? How do I prevent the "debug info:" message from appearing after the data was sent to the printer?

This should be obvious to me, but it isn't, unfortunately.

Thanks again.

Link to comment
Share on other sites

Martin,

May I bother you with one more question? How do I prevent the "debug info:" message from appearing after the data was sent to the printer?

This should be obvious to me, but it isn't, unfortunately.

Thanks again.

That's an error because I shouldn't have left that in the dll. I have removed that from the dll and uploaded the correction. You can get it from the link in the first post in my example scripts thread for printmg.

It also needs a new function in the UDF to wrap the dll cal so you can use a struct or an array. I'll do that during the next few weeks.

I see that my dll function for printbytesdirect handles the startdoc and enddoc itself which is why I didn't need the _PrintStartPrint and _PrintEndPrint lines. I'm not sure that is a good idea because if you have a very large file you might want to send it a few Kb at a time, so I think the function needs an extra parameter or two to set whether the startdoc or enddoc are used.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

OK, thanks again.

And my guaranteed last question in this thread.

Now that there's an answer to the question "how to get a binary string into a DLLstruct which is a byte array" in another thread, could you possibly spell out how to use that answer with PrintBytesDirect? I tried fitting the answer in the other thread into the code here, but nothing printed. Again, this should probably be obvious, but I don't see what I'm missing.

Thanks again for all your work on this extraordinary UDF.

Link to comment
Share on other sites

OK, thanks again.

And my guaranteed last question in this thread.

Now that there's an answer to the question "how to get a binary string into a DLLstruct which is a byte array" in another thread, could you possibly spell out how to use that answer with PrintBytesDirect? I tried fitting the answer in the other thread into the code here, but nothing printed. Again, this should probably be obvious, but I don't see what I'm missing.

Thanks again for all your work on this extraordinary UDF.

I modified my script in post #12 but the strike through didn't work inside the code tags so I've deleted the original for/next loop I used and left the dllstructSetDataline. I think it works but let me know if you have a problem with it.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I modified my script in post #12 but the strike through didn't work inside the code tags so I've deleted the original for/next loop I used and left the dllstructSetDataline. I think it works but let me know if you have a problem with it.

Martin,

That works absolutely perfectly. And I'm also grateful for the education in array handling. Thank you again for this solution, which I think will be useful to other people also.

Link to comment
Share on other sites

  • 2 years later...

Martin,

I know that this post's thread was from several years ago but I am hopeful of some help.

I have a need to print raw data to a specialized printer which is set up in Windows 7 as a Generic Text Printer either as a RS-232 device or an IP networked printer.  Your script above, along with your DLL and UDP work flawlessly when the printer is set up as a RS-232 connected device, but while the printer is getting data as apparent by the activity light on the printer, it does not print when set up as an IP based device.

Not sure what the difference would be and why it works one way but not the other.  Any thoughts?

Thanks in advance.

Link to comment
Share on other sites

I regret that I don't know anything about IP printing.

If the IP printer can be set up simply as another printer and printing to that doesn't work then I have no idea.

If you can find out how to open the printer as a file then you should only need to write the binary data to that file and you wouldn't need a printer UDF.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...