How to send text to the printer with PHP and Javascript?

I need to print a text that is not necessarily contained in the html document on a printer installed on the client, I need to open that window to select printers and etc and send the print.

/ / EDIT

In case the print would be sent to a zebra thermal label printer that uses EPL language, so I can not send the html document for printing.

Author: tissei, 2014-02-11

4 answers

I don't think this is as possible as you want.

PHP is a language that stays on the server. She never talks directly to the client; the only way she can "say" something to the client is through an interface (which is usually Javascript and Html). So unless the printer is connected to the PHP server, I can't imagine how you can print something using PHP.

One way would be to have a PHP server installed on the client, which listens to a URL (by example, /imprimir.php?nomeDoDocumento=(...)), and as soon as this URL was called, PHP itself would call the print. you can use this as a reference if this is the case . Note that in this case you don't even need to use Javascript (but you can use it if you want to call this URL via Ajax, for example).

If you don't have a PHP server on the client (which is quite likely), you can't print something out of a document. But you can hide the document so that it does not appear in the browser, only in print. For this you use a hidden iframe, for example.

Now I'm going to adapt a piece of this answer from the international OS.

var iframe = document.createElement('iframe');
iframe.src = urlOfFile;
iframe.style.display = "none";

var iFrameLoaded = function() {
  iframe.contentWindow.print();
  iframe.parentNode.removeChild(iframe);
};

if (iframe.attachEvent) // Internet Explorer
  iframe.attachEvent('onload', iFrameLoaded);
else if (iframe.addEventListener) // Outros navegadores modernos
  iframe.addEventListener('load', iFrameLoaded, false);
else // Outros navegadores
  iframe.onload = iFrameLoaded;

document.body.appendChild(iframe);

Just put the above code in the event of a button or page load, for example.

UPDATE : by your edit in the question, I imagine that neither of the two solutions proposed by me serve you. Then I'll give you one more.

I do not know this EPL language, nor printers Zebra. But I had a curiosity: is there no driver or simulator of this printer that is capable of generating image or PDF files?

If it exists, you can use it to generate a file and send that file via PHP to the client, and then they can print it normally.

UPDATE 2 : apparently, what you're trying to do like this is really impossible.

But it is not impossible otherwise. You need a means to communicate with the printer. Javascript may not even do this, but it can work with files and it can communicate with other applications through AJAX.

Here are my two cents: create a standalone mini-server for clients. A desktop application. I think it is possible with the PHP-GTK , although I have never used it in practice. Newer versions of PHP come with a mini development server; although it is not recommended to use it in production, it should be enough for what you want to achieve.

If you allow me to give an opinion, I think you would do well by creating a package in another language. One option would be Node.js + ExpressJS + AppJS . With these three you can create a package for the client, and if you want to be more perfectionist, you can even create your installer. Another option would be Ruby + Sinatra + Tar2RubyScript + RubyScript2Exe . You choose your language and platform, I just gave examples. You know your customers and so should know their preferences.

Once you have a small stand-alone server, you can make requests to it via AJAX. I will not delve into it here, you can ask another question about how to use AJAX or these mini-servers.

Keep in mind that doing this will not be easy. But it's the way I imagine you can get it.

 5
Author: André Leria, 2017-05-23 12:37:23

To print the current page, the CSS of which does not explicitly say that certain parts should not be printed, use javascript print. documentation in print MDN .

window.print();

To control with CSS what on the current page should be displayed, and what should not, do the following:

/* Tudo que estiver nesse código, será considerado apenas
 quando um documento HTML for exibido pra impressão */
@media print {

 /* ocultar */
 .imprimir-esconde {
   display: none;
  }
  /* Lembre-se que o que por para exibir aqui, por padrão 
    deveria ser ocultado caso você não queira que o texto 
    seja exibido por padrão */
  .imprimir-exibe {
   display: block;
  }
}

As for the 'Don't print something already being seen' part, you have two options.

  1. on the current page, have a link to the page that has the content to print and need to have print.
  2. have the content to be prints in the current, but CSS hide it until you use the print command.

Print without using native print Drivers

Javascript also allows you to download the level and make a communication with WebSockets and directly access the printer, if it is accessible via IP and Port. This is not something trivial, but directly accessing a network protocol is not something trivial anyway and the question it made, if do not use standard drivers installed on the client computer, it is complex in nature.

At MDN you have an introduction to how to write an application that uses this technology https://developer.mozilla.org/en-US/docs/WebSockets/Writing_WebSocket_client_applications

 3
Author: Emerson Rocha, 2014-02-11 12:11:45

As already mentioned, it is not possible to use Javascript to send commands to the printer, and a language with lower-level access to the operating system is required to perform communication directly with the printer.

In this scenario, something that came to mind is the solution that some companies use: Java applets. I don't really like applets, but it seems to be the only way to solve problems like this other than creating specific components for each browser.

I did a brief Google Search and the first result was an open source project called jzebra, which aims to do exactly what you need: communication with laser and postscript printers, as well as being cross browser. Then it would not be necessary for you to spend a lot of time learning Java, you just need to study this or that tool.

 2
Author: utluiz, 2014-02-11 12:33:28

I'm going through the same problem, PHP and direct printing on the client. In my case I solved using the PHP Desktop application, it is a browser (chromium) compiled to run a mini php server on the client, with this I managed to do the printing without problems generating a TXT file and sending the file to the printer by system commands.

If you are interested I will pass you some links that I have saved in a virtual machine.

 1
Author: Stefânio Souza, 2016-01-14 12:59:00