Javascript print directly to default printer


Product JSPrintManager Published 06/25/2019 Updated 12/11/2019 Author


Overview

In this walkthrough, you'll learn how to print HTML content from Javascript directly to the client printer without displaying a print dialog i.e. very silently. You'll be able to print HTML content to the Default client printer as well as to any other installed printer at the client machine. This solution works with any browser on Windows OS like IE, Edge, Chrome, Firefox, Opera & Safari as well as on Linux, Raspberry Pi & Mac systems!

The HTML content that we'll print is a simple business card made by pure HTML and CSS markup containing an image. The HTML Card looks like follows:

John Doe

Architect & Engineer

The HTML printing is performed by following these tasks:

  • Step 1 The HTML content that needs to be printed is converted to a PNG image by leveraging html2canvas utility from pure Javascript code.

    IMPORTANT! html2canvas is not supported by all browsers. Please refer to Browser Compatibility topic.

    NOTE: It's important that you specify inline style instead of CSS classes to specifying things like Font Family as well as is a good idea to use Data URI to display images in PNG or JPEG formats.

  • Step 2 Once the HTML content was rendered to a PNG image by using html2canvas, we'll invoke JSPrintManager App for starting the printing process.

Follow up these steps

  • Be sure you install in your dev machine JSPrintManager (JSPM) (Available for Windows, Linux, Raspberry Pi & Mac)
    This small app must be installed on each client that will print from your website!
  • By using your favorite Web Development IDE or Text Editor, create a new HTML file like index.html
    Copy/paste the following snipped codes:

    HTML Code

    
    

    Print HTML Card from Javascript

    This card is 300px x 400px ⇔ 3.125in x 4.17in (300/96 and 400/96 respectivelly)

    John Doe

    Architect & Engineer


    or...



    Script References

    • Download JSPrintManager.js
    • Copy all of these *.js files to the same folder where your html page is located and the add the following script references to your page.

    
    
    
    
    
    
    
    
    
    
    
                                

    Script Code

    
    
    
  • That's it! Run your website and test it. Click on Print Now... to print the HTML/CSS Card without print dialog. You can print it to the Default client printer or you can get a list of the installed printers available at the client machine.

Try JSPrintManager Online Demo!

How do I directly print from my browser?

Simple Steps for Printing Out a Web Page.
Open Your Web Page. Have the web page you wish to print open in your browser..
Click Menu. In google chrome this will be represented by three small vertical lines in the top right hand corner of your browser. ... .
Click Print. Once the menu drops down, select “Print”..

Can I set the window print settings with JavaScript?

Answers. In short, under web development, you can't touch the printer using javascript.

How do I allow my printer to print directly?

Open the Printers folder, if it is not already displayed. Right-click the desired printer with a Barr port and select Properties. From the printer's Properties dialog box, select the Advanced tab. In the middle of the dialog box, select Print directly to the printer.

How do I set my printer to print automatically?

Select Start > Settings . Go to Devices > Printers & scanners > select a printer > Manage. Then select Set as default. If you don't see the Set as default option, the Let Windows manage my default printer option may be selected.