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

7

I need to print 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 printout.

// EDIT

In case the printout would be sent to a zebra thermal label printer using EPL language, I can not immediately send the html document for printing.

    
asked by anonymous 11.02.2014 / 12:02

4 answers

5

I do not think this is possible as you want.

PHP is a language that stays on the server. She never talks directly to the customer; the only way for her to "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 not figure out 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 (for example, /imprimir.php?nomeDoDocumento=(...) ), and once that URL is called, PHP itself would call the impression. You can use this as a reference, if appropriate . Note that in this case, you do not even need to use Javascript (but you can use it if you want to call this URL via Ajax, for example).

If you do not have a PHP server on the client (which is very likely), you can not print something out of a document. But you can hide the document so it does not appear in the browser, just 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 editing the question, I imagine neither of the two solutions proposed by me serve you. So, I'll give you one more.

I do not know this EPL language, nor Zebra printers. But I was curious: is there no driver or simulator for 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, so he can print it normally.

UPDATE 2 : What you're trying to do, this way, 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 customers. A desktop application. I think it's possible with PHP-GTK , although I've never actually used it in practice. Newer versions of PHP come with a mini-server development; although it is not recommended to use it in production, should be sufficient for what you want to achieve.

If I may give you an opinion, I think you would do well to create 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 a> + Tar2RubyScript + RubyScript2Exe . You choose your language and platform, I just gave examples. You know your customers and so should know your preferences.

Once you have a small stand-alone server, you can make requests to it via AJAX . I will not dig into this 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.

    
11.02.2014 / 12:24
3

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

window.print();

To control with CSS what in 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 'do not print something that is already being seen' part, you have two options.

  • On the current page, have a link to the page that has the content to be printed and set to have print .
  • Have the content to be printed in the current one, but CSS will hide it until you use the print command.
  • Print without using native print drivers

    Javascript also allows you to download the level and communicate with WebSockets and direct access to the printer, case it is accessible via IP and port. This is not trivial, but direct access to a network protocol is not trivial in any way, and the question you asked, if you do not use default drivers installed on the client computer, is complex in nature.

    MDN has an introduction to how to write an application that uses this technology link

        
    11.02.2014 / 12:15
    2

    As already mentioned, you can not use Javascript to send commands to the printer, requiring a language with lower-level access to the operating system to perform communication directly with the printer.

    In this scenario, something that came to my mind is the solution that some companies use: Java applets . I do not really like applets , but it seems to be the only way to solve problems like this in addition to creating specific components for each browser.

    I made a brief search on Google and the first result was a free code project called jzebra , which aims to do exactly what you need: communication with laser printers and postscript, as well as being cross browser . So you would not have to spend a lot of time learning Java, you just need to study this or another tool.

        
    11.02.2014 / 13:33
    1

    I'm experiencing 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, so I was able to do the printing without problems generating a TXT file and sending the file to the printer by system.

    If interested I'll give you some links that I have saved in a virtual machine.

        
    14.01.2016 / 13:59