printing.js test

Printer ip :
The print may only start once you reload the page (when it kills the connection).

Note (2020): the local network scanner below is something I quickly made in 2011, nowadays much better scanners exist (i.e. deducing the local network using WebRTC, etc).

printing.js documentation

Printing.js makes printing with javascript super easy.
With a few commands you can print out whatever you want. It doesn't (yet) include all printer functions, but its not hard to use functions that are not in this library.
Example of a printjob:

Methods:

Method Arguments Description Example
add_text(STRING) 1 Print STRING, without a new line at the end print_obj.add_text("Hello World");
add_text_nl(STRING) 1 Print STRING, with a new line at the end print_obj.add_text_nl("Hello World");
end_job() 0 End the job print_obj.end_job();
add_postscript(POSTSCRIPT) 1 Print a Postcript file.
Don't forget to properly escape the postscript data!
print_obj.add_postscript(psfile);
See the postscript section for more detail.
set_duplex(n) 1 Set duplex.
0 = Single Sided
1 = Duplex, Long-Edge binding
2 = Duplex, Short-Edge binding
print_obj.set_duplex(1);
add_PJL(PJL_COMMAND) 1 Add a PJL command.
See PJL section below for more detail.
print_obj.add_PJL("COMMENT Hello world");
set_copies(n) 1 Print n amount of copies print_obj.set_copies(5);
set_duplex(n) 1 Set duplex.
0 = Single Sided
1 = Duplex, Long-Edge binding
2 = Duplex, Short-Edge binding
print_obj.set_duplex(1);
set_page_size(n) 1 Set page size.

1 - Executive (7.25" x 10.5")
2 - Letter (8.5" x 11")
3 - Legal (8.5" x 14")
6 - Ledger (11" x 17")
25 - A5 paper (148mm x 210mm)
26 - A4 paper (210mm x 297mm)
27 - A3 (297mm x 420mm)
45 - JIS B5 paper (182mm x 257mm)
46 - JIS B4 paper (250mm x 354mm)
71 - Hagaki postcard (100mm x 148mm)
72 - Oufuku-Hagaki postcard (200mm x 148mm)
80 - Monarch Envelope (3 7/8" x 7 1/2")
81 - Commercial Envelope 10 (4 1/8" x 9 1/2")
90 - International DL (110mm x 220mm)
91 - International C5 (162mm x 229mm)
100 - International B5 (176mm x 250mm)
101 - Custom (size varies with printer)
print_obj.set_page_size(26);
set_page_source(n) 1 Set page source

0 - Print current page (paper source remains unchanged)
1 - Feed paper from main paper source
2 - Feed paper from manual input
3 - Feed envelope from manual input
4 - Feed paper from alternate paper source
5 - Feed from optional large paper source
6 - Feed envelope from envelope feeder *
7 - Autoselect
8 - Feed paper from Tray 1 (right side tray)
20 - 39 - High Capacity Input (HCI) Trays 2-21

* Must be used in conjunction with Paper Size
print_obj.set_page_source(6);
set_page_orientation(n) 1 Set page orientation.
0 = Portrait
1 = Landscape
2 = Reverse Portrait
3 = Reverse Landscape
print_obj.set_page_orientation(1);
set_left_margin(column) 1 Set left margin (in column number)
print_obj.set_left_margin(3);
set_right_margin(column) 1 Set left margin (in column number)
print_obj.set_right_margin(9);
set_top_margin(row) 1 Set left margin (in row number)
print_obj.set_top_margin(5);
reset_horizontal_margin() 1 Reset left/right margins to default
print_obj.reset_horizontal_margin()
set_cursor_column(column) 1 Set cursor column posistion
print_obj.set_cursor_column(25);
set_cursor_row(row) 1 Set cursor column posistion
print_obj.set_cursor_row(12);
set_box_pattern(n) 1 Set box pattern style.
See box section for more detail
print_obj.set_box_pattern(5);
print_box(height,width,fill) 3 Draw a box, optionally filled with the pattern you set with set_box_pattern()
See box section for more detail
print_obj.print_box(200,200,3);
set_wrap(n) 1 Set end of line wrap, 0 is enable, 1 is disable print_obj.set_wrap(0);
print(ip) 1 Possibly the most important function, print the data you set so far print_obj.print("192.168.2.56");

Postscript

To print Postscript the best thing is to create a separate js file were you apoint the postscript data to a variable (a string).
Then add it to the print data using print_obj.add_postscript(psfile). Don't forget to escape it though! For example, replace / with \/, and " with \".
If you don't do this you will get problems.

Draw a box

If you want to draw a box, first choose your shade/pattern with set_box_pattern (if you need a shade/pattern).
The different kind of patterns that you can choose from (please ignore my cursor :)

Then, draw the box using print_box(height, width, filltype).
filltype can be any of the following:
0 - Solid area fill (black)
1 - Solid white area fill (white)
2 - Shading fill (using the shade you defined with set_box_pattern)
3 - Cross-hatch pattern fill (using the shade you defined with set_box_pattern)
4 - User-defined pattern (For this you need to look in the PCL guides of HP)
5 - Current pattern





















Adobe and PostScript are registered trademarks of Adobe Systems Inc.
PCL is a registered trademark of Hewlett-Packard Company.

Copyright © 2011 Jim Bauwens