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

Asked

Viewed 8,200 times

7

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

// EDIT

In case the printing would be sent to a zebra thermal label printer using EPL language, so I can’t send the html document to the print.

  • Without opening a document it is difficult. Unless you use the PHP-GTK, who would have to side with the customer.

  • Hmn... hard because I need to print on a zebra printer using epl language, so printing the html document does not solve my problem ;s

  • @alleen94, tried to use the extension Printer ?

  • @lost face everywhere I saw people trying to use it so the people only complain, a lot of headache and little result

  • If you know the format of the file the printer reads is not only save the file in the print spooler? The system will read that and print it out itself.

4 answers

5


I don’t think it’s possible the way you want it.

PHP is a server language. She never talks directly to the client; the only way she can "say" anything 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 (for 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 applicable. Note that in this case, you don’t even need to use Javascript (but you can use, if you want to call this URL via Ajax, for example).

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

Now I’ll adapt a piece of that answer international SO.

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 edition in the question, I imagine that none of the two solutions proposed by me serve you. So, I’ll give you one more.

I don’t know that EPL language, or Zebra printers. But a curiosity arose: there is no driver or simulator of this printer that is able to generate 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 it can print it out normally.

UPDATE 2: Apparently, what you’re trying to do, this way, is really impossible.

But it’s 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’s possible with the PHP-GTK, although I’ve never used it in practice. Newer versions of PHP come with a development mini-server; although it is not recommended to use it in production, it should be enough for what you want to achieve.

If I may say so, 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 customer, 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, just give examples. You know your customers and so should know your preferences.

Once you have a small stand-alone server, you can request it via AJAX. I’m not going 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 achieve.

  • 1

    Well, then, unfortunately not. the only mode I can currently print is by sending via shell a command type TYPE C: TAG.TXT >>LPT1 , sending a txt with the commands to the printer, but this only works for the printer installed on com or lpt, and if I open this txt and try to print not printer it does not interpret the codes..

  • Then it gets harder. The only way I see is to have a PHP server in the client, since the PHP server can use the shell.

  • 1

    So André but in case this would be an online service and it is impossible for each client to have a local installed server

  • I understand. But all customers have the printer, right?

  • Yes all have, it is for what I have been seeing I think your solution will be the best even thanks

3

To print the current page, whose CSS does not explain that certain parts should not be printed, use javascript print. Documentation on 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 part 'not printing something that is already being seen', you have two options.

  1. On the current page, have a link to the page that has the content to be printed and don’t have the print.
  2. Have the content printed in the current one, 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 in any way and the question you made, if not using standard drivers installed on the client’s computer, is complex by nature.

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

  • So the problem is that I need to print to a zebra printer using epl language

  • Well, if the browser does not have the drivers to print the page, you will have to send the document to a server (either just the link, or the entire document) and only then will this server print the document. Doing all this is an entire project. If that’s what you want, I will mark in my reply this so that it is considered right.

  • then in case that print server should have the printer locally is not ?

  • Either that, or you must create an extension for the browser, and that extension accesses a Driver. Or you can download the level and make a communication with WebSockets and directly access the printer if it is accessible via IP and http://pt.wikipedia.org/wikiWebSockets

2

As already mentioned, it is not possible to use Javascript to send commands to the printer, requiring a language with lower-level access to the operating system to execute 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 seems to be the only way to solve problems like this besides creating specific components for each browser.

I made a brief google and the first result was a free source project called jzebra, who proposes to do exactly what you need: communication with laser printers and Postscript, besides being cross browser. Then you wouldn’t need to spend a lot of time learning Java, just need to study this or another tool.

  • 3

    So I’ve been taking a look at the jzebra most giving a lot of zebra.. kk

1

I am 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 make the printing smoothly generating a TXT file and sending the file to the printer by system commands.

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

  • 3

    Change your post and put the links and methods you used! Thanks!

  • This does not provide an answer to the question. To criticize or ask for clarification from an author, leave a comment below its publication - you can always comment on your own publications and when you have reputation points enough you will be able comment on any publication. - From Review

  • Stefânio, we answer here for future visitors too. Then you can add anything you think is cool to solve the problem, it may be that the author of the question is not interested, but surely other visitors will find it good ;)

  • @Danielfalbel, resolvi utilizando a aplicação PHP Desktop, Even if it is poor in detail, it is an attempt to answer the question. We should not vote to delete in such cases. Only when it is totally travel or spam or vandalism or someone else’s comment or AP’s attempt to give more details.... Try to give a solution is an answer, can do -1 if you think it convenient.

  • @brasofilo fault mine! sign that I need to pay much more attention in the analyses!

Browser other questions tagged

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