How to print from Autohotkey?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

How to print from Autohotkey?

03 Dec 2018, 05:20

What would be the most basic and simple method to print from AHK a formatted page which has to be composed from text and an image?
Last edited by newbieforever on 03 Dec 2018, 16:31, edited 1 time in total.
hymal7
Posts: 66
Joined: 14 Sep 2016, 05:37

Re: How to print from Autohotkey?

03 Dec 2018, 06:28

please be specific on what you are printing

ist it a Word page? an Excel page? a webpage?
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to print from Autohotkey?

03 Dec 2018, 06:42

hymal7 wrote:
03 Dec 2018, 06:28
please be specific ... is it a Word page? an Excel page? a webpage?
With "a formatted page which has to be composed from text and an image" I mean:
I have some text strings inside the AHK script, I have an image file, and I have to generate from these a layouted page and print it. I don't know yet which file format should be used for the page, this is a part of my question.
Last edited by newbieforever on 03 Dec 2018, 16:32, edited 1 time in total.
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: How to print from Autohotkey?

03 Dec 2018, 06:53

Some of the easiest advice to me has been to use built-in functions of any webbrowser a user has. So I found it easier to learn basic HTML and CSS for 13dollars on udemy than to include a couple of 3rd party softwares inside my script...

I am not able to do full html and css currently but am learning to do exactly like you need. I'll use AHK GUI user inputs to insert in placeholder of a HTML formatted layout. This HTML page will be easy to then run with AHK and open the web browser for print

The free model that inspires me is found on github and can be used commercially too. I take it as my example and "teacher" to do it my own way.
https://www.sparksuite.com/open-source/invoice.html
So imagin yourself coding a custom HTML CSS (javascript if you want too) and then just replace values in the HTML fields with your AHK ones
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to print from Autohotkey?

03 Dec 2018, 07:00

Thank you very much, DRocks. That was my idea too, and I am already working on such a HTML template, but it seems to be difficult (for a css newbie) to generate a template which is printed from a browser exactly as intended. That's why I posted my question. What would be alternatives to HTML?
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: How to print from Autohotkey?

03 Dec 2018, 07:14

I will let others reply as I am myself asking this question too. For now I have found that the learning curve to know HTML and CSS basics would be easier and more valuable than learning how to use 3rd party code that I dont own, and that the users would need to install with the script too. :)
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: How to print from Autohotkey?

03 Dec 2018, 10:21

Two alternatives to a HTML/CSS solution that I've used would be:

1) generate a PDF using the libHaru dll, but you are dependent on the end user's pdf reader to print:
https://autohotkey.com/board/topic/4065 ... -11/page-2

2) print directly from ahk using GDI+ via SGDIPrint library:
https://autohotkey.com/board/topic/8860 ... ermission/

alternative # 2 would be the least dependent on third party software
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to print from Autohotkey?

03 Dec 2018, 14:06

Thank you, gwarble & DRocks!

In the meantime I discovered another simple way. It is from the same kind as the method discussed with DRocks where a HTML template would be used:

WordPad, which is included in all Windows versions, supports the RTF format. RTF files are plain text too like HTML, so placeholder can be used. With RTF and WordPad it is much more easiere (than with CSS/HTML) to layout a template which will be printed correctly. A basic version of the template could be created in OO/LO Writer or Word and then tested and edited in WordPad to eliminate possible inaccuracies and create the final template. WordPad even supports command-line usage: a document can be printed to the default printer simply by 'write.exe /p filename'.

DRocks: Seems to be a promising way, doesn't it?
Last edited by newbieforever on 04 Dec 2018, 11:21, edited 1 time in total.
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
Contact:

Re: How to print from Autohotkey?

03 Dec 2018, 15:19

Hi, I remember seeing print functions inside this RichEdit Class. I never used it myself, but it's suppose to do the whole WYSIWYG thing.
https://autohotkey.com/boards/viewtopic ... t=richedit
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: How to print from Autohotkey?

03 Dec 2018, 16:22

newbieforever wrote:
03 Dec 2018, 14:06
Thank you, gwarble & DRocks!

In the meantime I discovered another simple way. It is from the same kind as the method discussed with DRocks where a HTML template would be used:

WordPad, which is included in all Windows versions, supports the RTF format. RTF files are plain text too like HTML, so placeholder can be used. With RTF and WordPad it is much more easiere (than with CSS/HTML) to layout a template which will be printed correctly. A basic version of the template could be created in OO/LO Writer or Word and then tested and edited in WordPad to eliminate possible inaccuracies and create the final template. WordPad even supports command-line usage: a document can be printed to the default printer simply by 'writer.exe /p filename'.

DRocks: Seems to be a promising way, doesn't it?
Thats a new idea that I had no clue about! Ill see if I can use that too :D thanks for sharing
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: How to print from Autohotkey?

03 Dec 2018, 16:25

newbieforever wrote:
03 Dec 2018, 05:20
What would be the most basic and simple method to print from AHK a formated page which has to be composed from text and an image?
A very simple way of doing printing, is to take the content of a variable and send to a text file of some type.

If this is a program for personal use, then one can easily automate the printing steps with Send or ControlSend. If this is a script for others to use, it can simply run the file associated with text files, and then they can decide to print or not. But, you could program all the steps for them, so it prints out a document.

If you rather it create a RTF, than just simply a .txt file, then just change the file extension of .txt to .rtf (PrintMe.txt to PrintMe.rtf). You will also have to fix the WinWait and WinActivate commands to associate with the appropriate Windows or just omit those steps.

You can also have this type of code assigned to a button on a GUI.

Code: Select all

Info =
(
I want this on paper.
I want this printed now.
)
TestFile = %a_scriptdir%\PrintMe.txt
FileAppend, %Info%, %TestFile% ; FileAppend will take your variable and put it into a text file.
Run, %TestFile%
WinWait, PrintMe.txt - Notepad
WinActivate, PrintMe.txt - Notepad
Send, ^p ; opens the print menu
WinWait, Print
WinActivate, Print
Send, !p ; presses print button
Last edited by SOTE on 03 Dec 2018, 16:30, edited 1 time in total.
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to print from Autohotkey?

03 Dec 2018, 16:29

@SOTE: But your answer does not include "formatted pages" containing images, right?
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: How to print from Autohotkey?

03 Dec 2018, 16:35

newbieforever wrote:
03 Dec 2018, 16:29
@SOTE: But your answer does not include "formatted pages" containing images, right?
Oh, you responded while I was modifying to address that. To create a RTF, just change the file extension of .txt to .rtf (PrintMe.txt to PrintMe.rtf). You will also have to fix the WinWait and WinActivate commands to associate with the appropriate Windows or just omit those steps.

If you use
Run, PrintMe.rtf
, Windows will run whatever program is associated with the .rtf extension.
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: How to print from Autohotkey?

03 Dec 2018, 19:05

Good idea using rtf/wordpad, I hadn't thought of that

I like the elegance of printing directly using GDI(+) with SGDIPrint(), i will post a simple script that shows how simple it can be in case you want to play with it
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to print from Autohotkey?

04 Dec 2018, 02:35

@DRocks, gwarble & others:

My experience (so far) with the WordPad/RTF method for printing from AHK:

- It is not difficult to create a perfect RTF template which will be printed exactly as intended. Placeholders can be used to be replaced from the AHK script by desired text. The final document can be printed using the command-line option of WordPad (write.exe /p filename).

- If pictures should be embedded, a few tricks could be necessary:

If a picture (e.g. a PNG image) is inserted in the RTF document, the picture data seems to be stored as wmetafile8; in the RTF plain text this looks like this:

{\pict{\*\picprop}\wmetafile8\picw776\pich776\picwgoal440\pichgoal440
... WMF8 hex data ...
}


To be able to use PNG image data* inside the RTF file the word wmetafile8 must be replaced by the word pngblip:

{\pict{\*\picprop}\pngblip\picw776\pich776\picwgoal440\pichgoal440
... PNG hex data ...
}


Additionally the picture size can be adapted in the RTF plain text: the picwgoal and pichgoal values define the final size of the picture. (The picw and pich values seems not to be necessary.)

{\pict{\*\picprop}\pngblip\picwgoal660\pichgoal660
... PNG hex data ...
}


Have fun experimenting!


* See RRUZ's answer on
https://stackoverflow.com/questions/149 ... f-document
Last edited by newbieforever on 04 Dec 2018, 11:21, edited 1 time in total.
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: How to print from Autohotkey?

04 Dec 2018, 03:50

newbieforever wrote:
04 Dec 2018, 02:35
@DRocks, gwarble & others:

My experience (so far) with the WordPad/RTF method for printing from AHK:

- It is not difficult to create a perfect RTF template which will be printed exactly as intended. Placeholders can be used to be replaced from the AHK script by desired text. The final document can be printed using the command-line option of WordPad (writer.exe /p filename).

- If pictures should be embedded, a few tricks could be necessary:

If a picture (e.g. a PNG image) is inserted in the RTF document, the picture data seems to be stored as wmetafile8; in the RTF plain text this looks like this:

{\pict{\*\picprop}\wmetafile8\picw776\pich776\picwgoal440\pichgoal440
... WMF8 hex data ...
}


To be able to use PNG image data* inside the RTF file the word wmetafile8 must be replaced by the word pngblip:

{\pict{\*\picprop}\pngblip\picw776\pich776\picwgoal440\pichgoal440
... PNG hex data ...
}


Additionally the picture size can be adapted in the RTF plain text: the picwgoal and pichgoal values define the final size of the picture. (The picw and pich values seems not to be necessary.)

{\pict{\*\picprop}\pngblip\picwgoal660\pichgoal660
... PNG hex data ...
}


Have fun experimenting!


* See RRUZ's answer on
https://stackoverflow.com/questions/149 ... f-document
You can also merge RTF files, by making them .txt files first. However, you need to strip out the headers for any text files after the first one. To see what RTF headers look like for each page. Create a document in Wordpad, as a RTF. Then rename the .rtf to .txt to see what it looks like.

If you merge 2 RTF formatted files that still contain their original headers, Wordpad will only recognize and show one page, instead of multiple pages. To strip the headers off, after the 1st page, looks like you can use RegExReplace() or InStr(). Those good with regular expressions would likely be able to cook up something to remove the header in a few lines.

If you are adding RTFs with pictures, the pictures also need to be separated by the below code, while still a .txt. You could have this added in your script. To see how Wordpad separates pictures in document, again create a document in Wordpad as a .rtf, then rename it to a .txt to check it out. Looks like Wordpad adds the below code for any additional block of text or pictures that are added.

Code: Select all

\par
possible you may also need to add something like. going from f0\, f1\, f2\, etc... before each picture. lang can be 17 or whatever the number in the header file, and keep it the same for the following pages.

Code: Select all

\f1\lang17
Just before the header code for the picture.

Code: Select all

{\pict
Once the .txt files are merged manually, though obviously by script would be much better, you can then change the file extension from .txt to .rtf and run it. You should get a document with both text and pictures, opened up in Wordpad or any program that can open RTFs. It' looks quite possible for AutoHotkey scripts to create, merge, edit, and print RTFs without needing to open Wordpad, MS Word, or even GDip.
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to print from Autohotkey?

04 Dec 2018, 04:04

SOTE wrote:
04 Dec 2018, 03:50
It' looks quite possible for AutoHotkey scripts to create, merge, edit, and print RTFs without needing to open Wordpad or MS Word.
Sorry, SOTE, but I don't understand what relevance all your suggestions might have on my actual question...

Of course, in principle you could generate a complete string in AHK which would then serve as the content of an RTF file. But when it comes to documents that should be formatted in a more complex layout, it would be an extremely difficult way to generate such strings. So you need a program like WordPad to create such layouted templates.
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: How to print from Autohotkey?

04 Dec 2018, 04:33

newbieforever wrote:
04 Dec 2018, 04:04
SOTE wrote:
04 Dec 2018, 03:50
It' looks quite possible for AutoHotkey scripts to create, merge, edit, and print RTFs without needing to open Wordpad or MS Word.
Sorry, SOTE, but I don't understand what you're trying to do with all your proposals...

Of course, in principle you could generate a complete string in AHK which would then serve as the content of an RTF file. But when it comes to documents that should be formatted in a more complex layout, it would be an extremely difficult way to generate such strings. So you need a program like WordPad to create such layouted templates.
With "a formatted page which has to be composed from text and an image" I mean:
I have some text strings inside the AHK script, I have an image file, and I have to generate from these a layouted page and print it. I don't know yet which file format should be used for the page, this is a part of my question.
Creating the script would present some difficulty, but once created it's likely to be easy to use.

From earlier posts, I interpreted that (and maybe mistakenly), you were looking to "compose" a document with AutoHotkey without initially using Wordpad or a word processor. I could see some scenarios for wanting that, where a programmer needs to wait for and collects user input into a AutoHotkey GUI script, that he collects data and pictures from various different sources, or even over various time periods. And then possibly chooses a certain day or time to assemble it all together, to then put into a document for printing. Because otherwise, I'm not quite sure why wouldn't the person just simply open up and use Wordpad, MS Word, or Open Office in the first place.
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to print from Autohotkey?

04 Dec 2018, 04:44

SOTE wrote:
04 Dec 2018, 04:33
Creating the script would present some difficulty, but...
It would be practically impossible when layouted templates with some complexity are intended (or you wuold be forced to do extensive studies about the structure and coding of RTF files). But OK, it is all clear now...
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: How to print from Autohotkey?

04 Dec 2018, 04:46

thank you for examples
example with old rghdruck.exe combined with ahk
www.profan.de/download/rghdruck.zip ( RGH-DRUCK 2.0 )
Image

Code: Select all

#warn
#NoEnv
setworkingdir,%a_scriptdir%
settitlematchmode,2
transform,s,chr,32
  F1=test21.txt
  PICT=profan.bmp
  myDate:=A_Now
  FormatTime, Date_a, %myDate%, yyyy.MM.dd HH:mm:ss
  FormatTime, Date_b, %myDate%, dddd
  TDS=%Date_a% %Date_b%
  ifexist,%f1%
  Filedelete,%F1%

  w  := 40
  d  := 10
  y2 := 430   
  
  xa1:= 50
  xa2:= xa1+w
  TV :=(600//4)
  tvy:=(y2-tv)
  
  
  xb1:= xa2+d
  xb2:= xb1+w
  RA :=(300//4)
  ray:=(y2-ra)
  
  xc1:= xb2+d
  xc2:= xc1+w
  CA :=(500//4)
  cay:=(y2-ca)
  
  YL := y2+10  ;- LastLine
;----------------  
  e4x=
  (Ltrim,join`r`n
  $ANSI
  $DO LOADSIZEDBMP %PICT% ,50,0,300,100                     ~* picture X Y width height
  $DO MOVETO 0,110                                          ~* move    X Y
  ~S~~1~          http://www.profan.de  RGHDRUCK   ~D.xx.xxx~ - ~Z:xx~       - ~#~ -
  ~>006~         ~* -left border                                                                                        
  ~1~            ~* -distance between lines         
  $DO USEFONT Lucida Console,15,8      ~* Font height width                
  File=%F1%      Printed=%TDS%
  ----------------------------------------------------------
  DATE        PRODUCT            IN        OUT      SALDO   
  ----------------------------------------------------------
  2006-12-06  Television                   600      600     
  2006-12-06  Radio                        300      900     
  2006-12-06  Calculator                   500     1400     
  $DO USEBRUSH 5,2                                          ~* fill diagonal green            
  $DO RECTANGLE %xa1% ,%tvy%,%xa2% ,%y2%                    ~* rectangle   
  $DO USEBRUSH 4,12                                         ~* fill diagonal             
  $DO RECTANGLE %xb1% ,%ray%,%xb2% ,%y2%                    ~* rectangle   
  $DO USEBRUSH 1,4                                          ~* fill              
  $DO RECTANGLE %xc1% ,%cay%,%xc2% ,%y2%                    ~* rectangle 
  $DO USEPEN 2,1,4                                          ~* lineart thick color                
  $DO LINE %xa1%,%yl%,530,%yl%                              ~* LINE from to
  )
Fileappend,%e4x%,%f1%  
;--------------------
 ;.......... RGH-LIST must closed before RGH-DRUCK start ...........
run,rghlist "%F1%"
;return

WinWait,RGH-LIST ,,6
IfWinexist,RGH-LIST
    {
    sleep,2000
    msgbox,262147,PRINT,Want you PRINT ?
    ifmsgbox,NO
    return
    ifmsgbox,Cancel
    return
    }
Winclose,RGH-LIST 3.1
sleep,1000
run,rghdruck "%F1%"
ExitApp
;=================== END script ====================================

Last edited by garry on 08 Dec 2018, 15:35, edited 1 time in total.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput and 118 guests