Php send to printer

Printing Directly From PHP

Note: This post is over two years old and so the information contained here might be out of date. If you do spot something please leave a comment and we will endeavour to correct.

Some of you might be surprised that you can print directly to a printer of your choice through PHP. This uses a PECL extension but is only available on Windows. It is possible to print using UNIX systems, but you have to call the ps program using the system function.

To install the printer functions on Windows you need to download the PECL library for Windows, found at pecl4win.php.net. Unzip this file and move the file php_printer.dll to your extensions directory in your PHP directory (usually called ext).

Next, open up your php.ini file and add the following line at the end of the extensions list.

Then add the following lines at the bottom of the file. This tells PHP what printer to use as a default. If the printer is locally installed then PHP only needs the name.

If the printer is installed on another computer then you need to supply the full address. This would look something like \\server\printer.

Save this and restart the web server. You should now have access to the printer functions. Make sure your printer is on, and that you have enough printer ink and you can get started.

Читайте также:  Css ul li свой маркер

To use these function to need to open the printer, start a document, and then start a page. You can have more than one page by ending and starting pages. Here is the basic model you will need to get started.

To create some content you need to call certain functions using the printer handle as a parameter. You can print text, lines, shapes and even images using a number of different functions. To print text you need to use the printer_draw_text() function, the first parameter is the printer handle, the second is the text to be printed and the last two are the x and y coordinates of the text on the page.

Of course, you will probably want to use a font to print with, in which case use the following additions to the code.

Drawing a line is nice and easy

These are just two examples, there are a number of other functions available on the PHP Printer page.

Phil Norton

Phil is the founder and administrator of #! code and is an IT professional working in the North West of the UK. Graduating in 2003 from Aberystwyth University with an MSc in Computer Science Phil has previously worked as a database administrator, on an IT help desk, systems trainer, web architect, usability consultant, blogger and SEO specialist. Phil has lots of experience building and maintaining PHP websites as well as working with associated technologies like JavaScript, HTML, CSS, XML, Flex, Apache, MySQL and Linux.

Want to know more? Need some help?

Let us help! Hire us to provide training, advice, troubleshooting and more.

Support Us!

Please support us and allow us to continue writing articles.

Comments

can i do it on linux? directly printing from PHP just like using php_printer.dll in windows. my webserver is linux, and i have a problem because my script printer doesn’t work. need help. thx

Submitted by Bart on Sat, 10/30/2010 — 18:55

can i do it on linux? directly printing from PHP just like using php_printer.dll in windows. my webserver is linux, and i have a problem because my script printer doesn’t work. need help. thx

Submitted by me 2 on Mon, 05/14/2012 — 06:59

Submitted by me 2 on Mon, 05/14/2012 — 07:01

Hi ! thank for that new but I’ve one question: is it possible to use this php extension control the print jobs which has been made through any windows driver instead ? in such a way that the extension help to save data (printer name, number of printed documents. ) related to any print job automatically.

Submitted by Saint-Cyr on Sat, 07/18/2015 — 08:46

Submitted by Lawrence on Thu, 10/08/2015 — 14:51

So you have no printer_open() function as this is provided by the Printer PECL package (https://pecl.php.net/package/printer). Have you read the post or are you just expecting random code snippets to work out of the box?

Submitted by philipnorton42 on Thu, 10/08/2015 — 15:03

Submitted by lawrence on Thu, 10/08/2015 — 16:39

Submitted by philipnorton42 on Thu, 10/08/2015 — 17:19

Using php printer functions — I am getting the error: Fatal error: Out of memory (allocated 3407872) (tried to allocate 4294967295 bytes) on the line that calls the printer_open() function. The printer exists and is turned on. I am calling the function with the printer’s name as parameter, i.e. printer_open(‘PRINTER 1’). This is the exact name of the printer as it appears in the devices and printers list on the server where the script is running. I’ve tried adding an IP addess in front and various permutations which then result in a ‘printer not found’ error. I can view the printer queue for this printer in the command line. Nothing is actually getting sent to the printer as the script terminates with the error at the printer_open() line. I’ve been googling a lot and tried altering permissions on the printer. Nothing has worked so far. The server is running IIS and windows 2007. The printer function shows up in phpinfo as below: Printer Support enabled Version 0.1.0-dev Default printing device . Module state working RCS Version $Id: printer.c 331404 2013-09-15 10:51:05Z ab $ Can anyone offer any suggestions?

Submitted by Tom on Thu, 11/19/2015 — 12:25

Did any one solve this problem? my web server is a Linux and I need just to print one single line of text on my local printer silently. is there any printer extensions under Linux (*.so files). Any help or sample code will be highly appreciated.

Submitted by Amir on Mon, 06/20/2016 — 21:07

As you are on Linux you could try to use the lpr command to print using shell_exec(). shell_exec(‘lpr file.txt’); Might be worth a shot?

Submitted by philipnorton42 on Tue, 06/21/2016 — 10:51

Submitted by ken chandara on Tue, 07/19/2016 — 04:50

Submitted by Philip Okugbe on Tue, 08/23/2016 — 19:53

I need to print on a custom paper size in PHP. The Custom paper size should be selected when I print, for example an invoice, after the printing is done, this paper size should be deselected. Thanks for the help

Submitted by Pradeep Chavan on Sat, 04/01/2017 — 14:44

Printing directly From PHP make is possible to let the user print a document on his own printer. But PHP is completely server-side. So this would mean using the printer-functions, you can only print on devices connecting to the server.

Submitted by Jhonmilton on Fri, 08/10/2018 — 11:33

Your code is nice and only meant for the developer.

How do you expect me to know the names of the all the printers for all the clients that use my app?

If there was another way, I could eh. have my clients input or select their printer from the list of printers they have on their machines, then pass that value to the php.ini file, that would be fine.

Meanwhile, even with that, writing a code to draw-text and draw lines for every bit of row and column to be printed, is far-too-cumbersome.

Where does the CSS styling come in?

Submitted by Anonymous on Sat, 12/26/2020 — 10:41

Well, yes. I mean I used it for a while back in 2009. Haven’t really had the need to use it since then though. I think the package has been made largely redundant for a good while now, not even sure it will work with PHP8. Since this is Windows only I haven’t had any way of running this code for at least 8 years.

> Your code is nice and only meant for the developer.

Yes. It is only meant for the developer. Sorry if this isn’t clear.

> How do you expect me to know the names of the all the printers for all the clients that use my app?

I think you might be mistaken. Since PHP is a server side language you should absolutely know what printer you are going to print to as it will be connected to your server.

> If there was another way, I could eh. have my clients input or select their printer from the list of printers they have on their machines, then pass that value to the php.ini file, that would be fine.

You could allow your users to select from a list of printers, but the printer would need to be available from the server you are running your PHP on. If the printer is connected to the server via a network then you could potentially use printer_open() with the name of the printer you are trying to open as the paramter.

> Meanwhile, even with that, writing a code to draw-text and draw lines for every bit of row and column to be printed, is far-too-cumbersome.

True, but if you are printing something simple like a shopping receipt then this would be ideal.

> Where does the CSS styling come in?

Well, it doesn’t as CSS is never interpreted by PHP to render the printer output. It sounds like you are trying to print from a browser, in which case this code would never be run as it would be the browser doing the printing.

Submitted by philipnorton42 on Sat, 12/26/2020 — 13:48

Источник

PHP Send data to Printer

My function returns the source string of a picture and I found a helpful article on http://php.net/manual/en/faq.html.php. Despite trying the solutions provided, none of them successfully printed the source string on the HTML page.

PHP Send data to Printer

PHP isn’t capable of performing that task, but it’s quite simple to accomplish using JavaScript.

Upon clicking the print button, carry out the ensuing steps.

This will prompt the print dialog set by the system’s default.

 echo $printerName; $printer = $printerName; if($ph = printer_open($printer)) < printer_start_doc($ph, "Start Doc"); $content = "myfile.php"; $file_handler = fopen($content, 'rw'); $content2 = fread($file_handler, filesize($content)); fclose($file_handler); // Set print mode to RAW and send PDF to printer printer_set_option($ph, PRINTER_MODE, "RAW"); printer_write($ph, $content2); printer_end_doc($ph); printer_close($ph); >else "Couldn't connect. "; 

Print HTML tags in PHP output, If you want to send HTML tags verbatim, don’t use htmlentities().If you want to show the tags in the browser as literal text, you need to escape it as you are doing currently. Please clarify what you actually want to do – Bojangles

Send variable from PHP to HTML

Begin by assigning the action to ? to submit the form to the current page. Next, establish an attribute using method to specify the method for transmitting the data. As a precaution, I suggest utilizing POST as you will be transmitting a password. (For additional information on form methods, please refer to the relevant material.)

Php pass data to html form, if the form and the values does not resides in the same file, you need to pass the value from page1.php to page2.php, you can do so by sending the values through URI and fetching it from $_GET method. assume you have the values in page1.php and want to send the values to page2.php while redirecting …

Is it possible to send data through PHP into a HTML file? [duplicate]

Assuming I have comprehended your inquiry accurately, your objective is to display information from a PHP file, enabling it to be noticeable to the user. To achieve this, you must execute the following steps:

  1. Generate a PHP document with the following code: index.php .
  2. Add your HTML and PHP code
  3. Verify that your web server is operational.
  4. Access the following web address in your browser: http://localhost:8080.

Suppose the contents of your file named index.php are as follows:

The outcome that has been generated appears in this way:

Is it possible to send data through PHP into a HTML, To expand on what @RiggsFolly said, PHP is a templating language (e.g. to create HTML, but not limited to that). All you need to do is change your file extension to .php and write your PHP code inside of this file alongside HTML.

How to pass a string from PHP into an html tag?

After reviewing the above solutions, I discovered the correct answer to the problem. None of the previously suggested codes were effective as they only displayed the original string on the website.

I have a functioning method that returns the source string of a picture, and it has proven effective for me.

This article proved to be useful in discovering and comprehending a solution.

Visit the FAQ section on the official PHP website at http://php.net/manual/en/faq.html.php.

How to print a HTML content using printer in php?, try calling the form inside an iframe then a link or button that will run the js to print the html/php inside the iframe.. Share Follow edited Aug 11, 2014 at 7:06 answered Dec 13, 2012 at 10:44 Vainglory07 Vainglory07 8 Thanks for

Источник

Оцените статью