 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Wouther
Joined: 01 May 2007 Posts: 78 Location: The Netherlands
|
Posted: Fri May 02, 2008 1:19 pm Post subject: Printing css/html-formatted text |
|
|
Printing css/html-formatted text
Introduction
First of all, I know there are some (a lot actually) functions for printing on the forum, but I decided to be stobourn and write my own.
The result is printHTA(), a simple function (but I decided to share it anyway) that can print css/html-formatted text. This text can be in a variable, a file or just entered directly into the function. The same applies to css-code. Using css makes it easy to print some text formatted, i.e. body { font-family: verdana; } will make the font 'verdana' instead of the standard 'times new roman'. (If you don't know css you can google for documentation. It's not that hard to understand) Furthermore, you can pick your own icon for the print-preview window and set some other options.
How it works
Browsing the forum I got the idea to use an hta file to print. Using some Javascript, which we place in the <BODY ONLOAD="..."> of the temporary HTA-file, we can make the print-dialog pop up when the preview page has loaded. That's the idea, the rest is just some rather uninteresting code to make printing formatted text easier.
Bugs/issues/planned features
- I'd like to make the function return 1 when the user actually printed the text and 0 when he/she dismissed the print-dialog. Anyone know how?
- Titan's code uses RegRead to read something from the registry and use that to call the HTA-file. Perhaps this is better, because some might not be able to just Run a HTA-file. Any opinions?
- Some of the 7-line pieces of code could be written as one line using the ternary operator, but only if I could somehow make RegexMatch return the subpatern, instead of storing it in an outputvariable. Did I miss this? (is it possible?) It would make the code more compact.
- I'm out of ideas for more options!
Changelog
v0.2 Changed the option-style from numbers to words (semicolon seperated) - thanks Rabiator! - and removed the parameters pTitle and pIcon: they are now options.
v0.1 Initial version
Current version
| Code: | printHTA(pOptions, pText, pStylesheet = "")
{
options := ";" . pOptions . ";"
IfInString, options, `;Text ;Is it text?
text := pText ;Yes
Else ;No
IfExist, %pText% ;Does the file exist?
FileRead, text, %pText% ;Yes, so read it
Else
Return -1
IfInString, options, `;Css ;Is it css-code?
stylesheet := pStylesheet ;Yes
Else ;No
IfExist, %pStylesheet% ;Does the file exist?
FileRead, stylesheet, %pStylesheet% ;Yes, so read it
Else
Return -2
IfInString, options, `;Title= ;Is the title specified?
{ ;Yes
RegexMatch(options, "\Q;Title=\E(.*?)\;", sub)
title := sub1
} ;No, so use the default
Else
title = Print
IfInString, options, `;Icon= ;Is the icon specified?
{ ;Yes
RegexMatch(options, "\Q;Icon=\E(.*?)\;", sub)
icon := sub1
} ;No, so use the default
Else
icon=
IfNotInString, options, `;Html ;Is it a webpage?
StringReplace, text, text, `r`n, <BR>, All ;No, so make it legible
ContextMenu := InStr(options, ";ContextMenu") ? "false" : "true"
tmp = %A_Temp%\printHTA%A_TickCount%.hta
contents =
(
<HTML><HEAD>
<HTA:APPLICATION ID="printHTA%A_TickCount%" APPLICATIONNAME="printHTA%A_TickCount%" ICON="%icon%">
<STYLE TYPE="Text/CSS"> %stylesheet% </STYLE><TITLE> %title% </TITLE></HEAD>
<BODY ONLOAD="window.print();" ONCONTEXTMENU="return %ContextMenu%;"> %text% </BODY></HTML>
)
FileAppend, % contents, % tmp
RunWait, % tmp
FileDelete, % tmp
Return 1
}
|
Documentation
Syntax: printHTA(Options, Text[, Stylesheet])
Returns: one of these numbers (if it's negative, it's an error, you can use that to check after the function returns)
- 1 = No errors
- -1 = The file (parameter 2, Text) does not exist.
- -2 = The stylesheet (parameter 4, Stylesheet) does not exist.
Parameters:
Options: a list of zero or more of these words (not case sensitive), semicolon seperated.
- Text : parameter 2 (Text) contains the text, not the name of the file to print.
- Css : parameter 3 (Stylesheet) contains css-code, not the name of the (css-)file to use as a stylesheet.
- Html : the page to print is a webpage, so do not auto-format with <BR>-s.
- ContextMenu : disable context menu / right click.
- Title=Value : sets the title of the preview-window. It will also appear at the top of the printed page.
- Icon=PathToIcon : sets the icon of the preview window.
Text: the text to print. By default, this is the name of the file to print. Use the option Text to make this parameter the text to print. If this is the only parameter you have to include Css in the Options, because else the function will not be able to find the stylesheet "" (empty).
Stylesheet: the stylesheet to use. By default, this is the name of the (css-)file to use as a stylesheet. Use the option Css to make this parameter accept css-code.
Examples:
Print a file in 'verdana', not the standard 'times new roman'.
| Code: | | printHTA("Title=Example 1;Css", "print_this.txt", "body { font-family: verdana; }") |
Print the text of an edit box in verdana and disable the context menu. Also make a formatted header.
| Code: | | printHTA("Title=Example 2;Text;Css;ContextMenu", "<H3>Text from the edit box:</H3>" . VarContainingEditBoxText, "body { font-family: verdana; } h3 { text-decoration: bold, underline; color: red; }") |
Of course, if you want to make a nice document, the css-code might grow fast, so you can put it in an css-file.
| Code: | | printHTA("Title=Example 3;Text", "<H3>Text from the edit box:</H3>" . VarContainingEditBoxText, "stylesheet.css") |
If you have a part of a webpage (or an HTML-template which you fill in using RegEx or StringReplace) you can use the option Html to not automatically format the text with <BR>-s after every line. This code will also use a custom icon.
| Code: | | printHTA("Title=Example 4;Text;Html;Icon=myCustomIcon.ico", VarContainingHTML, "stylesheet.css") |
I know it's not that fancy and all, but it can be usefull so I posted it anyway. Have fun with it.
Wouther _________________ Printing css/html-formatted text
Last edited by Wouther on Sat May 03, 2008 3:40 am; edited 1 time in total |
|
| Back to top |
|
 |
Rabiator
Joined: 17 Apr 2005 Posts: 265 Location: Sauerland
|
Posted: Fri May 02, 2008 5:51 pm Post subject: |
|
|
Nice idea with interesting options!
I think it will enrich some of my scripts, thanks for sharing!
Just a proposal:
IMO the handling of the first parameter "pOptions" as a pieced number could be made more comfortable, perhaps as a string, "Text Stylesheet ContextMenu". There's no need for a number cause it won't be calculated. Evaluating text won't slow down the function distinctly but give more lucidity. |
|
| Back to top |
|
 |
Wouther
Joined: 01 May 2007 Posts: 78 Location: The Netherlands
|
Posted: Sat May 03, 2008 3:43 am Post subject: |
|
|
| Rabiator wrote: | Just a proposal:
IMO the handling of the first parameter "pOptions" as a pieced number could be made more comfortable, perhaps as a string, "Text Stylesheet ContextMenu". There's no need for a number cause it won't be calculated. Evaluating text won't slow down the function distinctly but give more lucidity. | Thanks for the suggestion, I implemented it in v0.2. I also removed two other parameters and added them as options. Now the only parameters are the options, the text and the style. All extras will be in the options.  _________________ Printing css/html-formatted text |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|