I have spent a lot of time on this question, namely, how to programatically use a PHP script to send a PDF document to the printer without launching the Adobe print dialog that results from using arcord32.exe (the adobe reader). PHP has some useful classes for creating PDF's, i.e., the FPDF library, but printing the created file is a bear. For Windows XP and Windows 7 servers, the one open -source solution which works is to use the PDFBOX java library. Here are the steps.
1) Download the latest java pdfbox binary: http://www.apache.org/dyn/closer.cgi/pdfbox/1.7.1/pdfbox-app-1.7.1.jar and put it in a directory. I will call my example c:/java/libs
2) Create a PDF file you want to send to the printer. I will call it example.pdf.
3) Configure the printer on the server (where PHP is running) you want to use to have a one-word name, like HPOfficeJET. You can't get away with a name like HP 8600 OfficeJet.
4) Create a command string like this:

     $printcmd = "java -classpath c:/java/libs/pdfbox-app-1.7.1jar org.apache.pdfbox.PrintPDF -silentPrint -printerName HPOfficeJet example.pdf";

5) Use one of the PHP exec commands to launch this command:

       exec($printcmd);

If all goes well, your server's printer will start, the pdf will print, and you will have created a way to have a client page request the server to print a server PDF you select or create.
OK, some notes: Read the PDFBox documentation. It explains the syntax of the above command. Also, try the commnad out at the command line (on the server)until it works. The java executable must be in a $PATH directory,or explicitly specified. You can, of course, park your pdfbox jar file in your system classpath for easier reference. The name of the java class must be fully qualified:org.apache.pdfbox.PrintPDF And, before beginning to implement this, get your head on straight about which code is executing where. This is server-side PHP, usually running under Apache, although it should work anywhere. I have seen questions about a javascript solution - that is client-side code and doesn't help you with a PDF that you want to print at the server. Good luck.

LastMitch commented: Thanks for Sharing! +11
broj1 commented: Interesting +9

Recommended Answers

All 15 Replies

So is this an informative article or a question? :) If it's the first, thanks! And if it's the second, what's the question? ;)

Informative. It cost me hours of time to research and solve so thought I'd pass it on.

Great thanks! :)

This is interesting, thanx for sharing. But what were the requirements for this kind of script? Is this a part of a production application? And did you have to implement any security (so the whole world does not start sending thousands of pages to your printer)?

It is a production client-server application. The script is actually an AJAX script launched from a PRINT button on a secured page. The client has to be logged in to see the page. Moreover, all the clients are (a) either in the local LAN workgroup or (b) firewall IP filtered WAN clients.
AK.

Can Anyone explain why this isn't working for me. I am basically setting up something like a kiosk where a user will not be viewing documents but rather just needing a single paper copy. So I want it where they click the button and the specified pdf prints. This is run from a browser in kiosk mode runing from localhost.
this is windows running wamp
I can execute from a command line and it works perfectly.
the this php code runs I see the java process execute in task manager and moments later end. But nothing is printed.

<?php

$cmd = 'C:\java\jdk1.7.0_51\bin\java -classpath c:/java/libs/pdfbox-app-1.8.4.jar org.apache.pdfbox.PrintPDF -silentPrint -printerName canon c:/wamp/www/1grade.pdf';

exec($cmd);

 ?> 

Figured out my problem. Works fine now. I was setting up and testing this system in a virutal environment. The printers were being shared with the host system through vmware's magic. Used psexec to get a command prompt where I could run commands as the SYSTEM user. Found that it would not print when I ran it from there. So, I connected the printer into the virtual machine and let it install drivers and become a physically attached printer. Works like a charm.

what about a network printer in this ,scenario.how to lacate the usb printer.in this?

This kind of solution is useful where php mysql application is build for internal use like in intranet enviroment.

Some time operator at reception need to print receipts and bills directly for the customer, there this will help

Thanks for posting this! I was stuck for a day with the (failing) arcord32.exe solution and then some hours with a non functional php_print.dll . This works.

<?php
error_reporting(E_all);
$cmd = 'C:\Program Files\Java\jre1.8.0_151\bin\java -classpath E:\lib\pdfbox-app-2.0.8.jar org.apache.pdfbox.tools.PrintPDF -silentPrint -printerName toshiba D:\xampp2\htdocs\print_con\1.pdf';

exec($cmd);

If anyone knows a method to print the attachment also then please comment

As the answer is a go-around way, there is a another mechanism which can be used if the printer is web connected. It's as simple as sending out an email registered with the printer.

The printer can be sent an email (e.g. HP ePrint).

@saurabh_16 Does the command line you modified works?
$cmd = 'C:\Program Files\Java\jre1.8.0_151\bin\java -classpath E:\lib\pdfbox-app-2.0.8.jar org.apache.pdfbox.tools.PrintPDF -silentPrint -printerName toshiba D:\xampp2\htdocs\print_con\1.pdf';

why are the two path different ?

Mine, trying from this example was not working, and then i discovered in this example
$printcmd = "java -classpath c:/java/libs/pdfbox-app-1.7.1jar org.apache.pdfbox.PrintPDF -silentPrint -printerName HPOfficeJet example.pdf";

it is .jar (dot jar) file. The dot is missing.

That really great for sharing this. Thank you sooooo much.

There are multiple ways for doing it:

  1. Use kiosk mode of browser, it will allow you to print directly. Only problem is, it works best with firefox, as chrome new updates always makes life difficult. If you can ensure, printing on default printer and use of firefox, don't go for any other solution.

  2. Go with techsapphire direct print solution, it is very simple and doesn't require any high skill. This support all browsers except Microsoft Edge. Only problem is, it only works on windows platform not on mac or linux. But as most of POS software are used on windows, so it should not be restriction for you. Second, as it is commercial, so they can customize for you. Below is example of techsapphire direct print.
    https://youtu.be/hDJUTdVpmXo

Below is sample implementation techsapphire direct print:

Click to Print PDF

And that's all you need. It will save you tons of hours. Only problem is, it only support windows

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.