HTML / Javascript One Click Print (no dialogs)
I ended up implementing a custom application that works very similar to the Nexus Mod Manager. I wrote a C# application that registers a custom Application URI Scheme. Here's how it works:
- User clicks "Print" on the website.
- Website links user to "CustomURL://Print/{ID}
- Application is launched by windows via the custom uri scheme.
- Application communicates with the pre-configured server to confirm the print request and in my case get the actual print command.
- The application then uses the C# RawPrinterHelper class to send commands directly to the printer.
This approach required an initial download from the user, and a single security prompt from windows when launching the application the first time. I also implemented some Javascript magic to make it detect whether the print job was handled or not. If it wasn't it asks them to download the application.
Browser Print dialog not showing
By default, Print.js will process the computed styles for each html element. When printing large and / or complex html, this process may take a while. Specially on slower machines.
To prevent this, the library has a parameter scanStyles
that can be set to false
.
However, for this to work properly, you may want to style your html with css classes instead of inline style. Since the library isn't scanning the computed styles, it needs to receive the css you want to use when printing.
For example, when printing an element of id myElement
on a page with two attached stylesheets, myStylesheet.css
and vendor.css
:
printJS(
{
printable: 'myElement',
type: 'html',
scanStyles: false,
css: ['myStyleSheet', 'vendor.css']
}
)
This supports css print media query as well.
You can also pass custom inline style if necessary:
printJS(
{
printable: 'myElement',
type: 'html',
scanStyles: false,
css: ['myStyleSheet', 'vendor.css'],
style: 'h1 { color: blue; }, someClass { font-size: 1rem; }'
}
)
http://printjs.crabbly.com/#configuration
javascript print without print dialog box
This is totally possiable. I work in banking and had a webpage that tellers needed to auto print when a transaction was posted. Since they do transactions all day it would slow them down if they had the dialog box display everytime. This code will select your default printer and print directly to it with no dialog box.
<form>
<input type="button" value="Print Page" onClick="window.print()">
</form>
<script language="VBScript">
// THIS VB SCRIP REMOVES THE PRINT DIALOG BOX AND PRINTS TO YOUR DEFAULT PRINTER
Sub window_onunload()
On Error Resume Next
Set WB = nothing
On Error Goto 0
End Sub
Sub Print()
OLECMDID_PRINT = 6
OLECMDEXECOPT_DONTPROMPTUSER = 2
OLECMDEXECOPT_PROMPTUSER = 1
On Error Resume Next
If DA Then
call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
Else
call WB.IOleCommandTarget.Exec(OLECMDID_PRINT ,OLECMDEXECOPT_DONTPROMPTUSER,"","","")
End If
If Err.Number <> 0 Then
If DA Then
Alert("Nothing Printed :" & err.number & " : " & err.description)
Else
HandleError()
End if
End If
On Error Goto 0
End Sub
If DA Then
wbvers="8856F961-340A-11D0-A96B-00C04FD705A2"
Else
wbvers="EAB22AC3-30C1-11CF-A7EB-0000C05BAE0B"
End If
document.write "<object ID=""WB"" WIDTH=0 HEIGHT=0 CLASSID=""CLSID:"
document.write wbvers & """> </object>"
</script>
Related Topics
How to Hide an HTML Table Row <Tr> So That It Takes Up No Space
Two Images, Side by Side Responsive
Div Elements Overlapping Each Other
How to Divide a Page in Three Vertical Sections
How to Remove the Space Between Inline/Inline-Block Elements
Can't Scroll to Top of Flex Item That Is Overflowing Container
Why Is the ≪Center≫ Tag Deprecated in Html
Browsers' Default CSS For HTML Elements
How to Style Selected Option Color Separately from Disabled Option
How to Limit the Maximum Files Chosen When Using Multiple File Input
How to Add HTML Line Break Within an Input Text Placeholder
Woocommerce Change Product Image on Hover With CSS
Angular 2: Get Position of HTML Element