Print without confirmation window in PHP, Javascript or Ajax

Asked

Viewed 6,609 times

1

I have a non-tax Zebra TLP 2844 thermal printer, and I’m developing a password generation program to print on that printer.

I’m using the functions printer_.

I tried the following example code:

<?php
   $handle = printer_open();
   printer_write($handle, "Testando...");
   printer_close($handle);
?>

But it is not printing the text of "printer_write". What I might be doing wrong?

I need to print without opening the printer’s choice dialog, so I thought of this method. Could someone tell me another?

  • 1

    Just one observation... Have you ever thought of using any programming language even for this? Hardly languages made for scripts, Like PHP, they will give you the control you want for this type of application (some people do "software" in PHP, but it’s like taking a knife to unscrew a PC cabinet). If you’re familiar with something like C, C++, C#, Delphi, or the like, and you’re starting to do the app yet, it might be worth rethinking from the start. Remember that nothing prevents you from having an executable that interacts with your remote application in PHP, transparently.

  • The printer is set to Default Printer?

  • 1

    Usually to print you need to send the correct command. The plain text, has printers that does not come out. You need to see the printer commands manual.

  • 2

    As @Tony said, it’s critical to read the printer’s manual. Most likely with an application running locally, you’ll have full control over the output, including taking advantage of its native, high print speed formatting. Using printing by normal windows graphics driver, or even plain text, almost everything the printer provides native resource is lost (this if you can print).

3 answers

4

In Firefox it is a matter of setting up print.always_print_silent:

  • place in the URL bar: about:config
  • look for this config and mark as true
  • if it does not exist, right-click and ask for "New" -> "yes/no", adding the config name and value
  • after that, the JS window.print() goes straight to the printer without the confirmation dialog

For the FF, also has this add-on:

JS Print Setup
Client side Javascript Printer Settings. This Extension Implements print setup from CS Javascript, similar of Meadco’s Scriptx Activex control for Internet Explorer.

In Internet Explorer, apparently it is possible to use Visualbasic:

<script language='VBScript'>
Sub Print()
       OLECMDID_PRINT = 6
       OLECMDEXECOPT_DONTPROMPTUSER = 2
       OLECMDEXECOPT_PROMPTUSER = 1
       call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
End Sub
document.write "<object ID='WB' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>"
</script>

References:

  • 1

    Anecdote: the most common comment I’ve seen is "That’s all I needed, I figured those popups jumping out of the printer without warning?". Obviously, for internal use in an organization the question is fully valid.

  • 3

    Imagine a lady going for the password and a vertical banner like "Enlarge your p**s now!" comes out of the printer, just in time.

  • @Bacco interactive advertising. Good thing it was not a 3D printer

2

Looks like you missed the start of the print job.

Example:

<?php    
    //$handle = printer_open("HP Deskjet 930c");  // http://php.net/manual/en/function.printer-open.php
    $handle = printer_open();

    printer_start_doc($handle, "Document name");        
    printer_start_page($handle); // Start page 1
    // here goes the content of page 1 via printer_write
    printer_write($handle, "A37,503,0,1,2,3,N,PRINTED USING PHP\n");
    printer_end_page($handle); // Close page 1

    //printer_start_page($handle); // Start page 2
    // here goes the content of page 2 via printer_write
    //printer_end_page($handle); // Close page 2

    printer_end_doc($handle);
    printer_close($handle);
?>

Source: https://stackoverflow.com/a/24217694/194717

1

Try the jzebra (Qz-print), to send direct commands to the printer.

https://code.google.com/p/jzebra/

Example of use:

<input type=button onClick="print()" value="Print">
<applet id="qz" name="QZ Print Plugin" code="qz.PrintApplet.class" archive="./qz-print.jar" width="100" height="100">
      <param name="printer" value="zebra">
</applet>

<script>
      function print() {
         var qz = document.getElementById('qz');
         qz.append('A37,503,0,1,2,3,N,PRINTED USING QZ-PRINT\n');
         // ZPLII
         // qz.append('^XA^FO50,50^ADN,36,20^FDPRINTED USING QZ-PRINT^FS^XZ');  
         qz.print();
      }
</script> 
  • 2

    Remembering that jzebra depends on a java applet on the machine.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.