5

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.

Votes + Comments
Interesting
Thanks for Sharing!
7
Contributors
10
Replies
79
Views
4 Years
Discussion Span
Last Post by b3nt3
0

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? ;)

0

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

0

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)?

0

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.

0

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);

 ?> 
0

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.

0

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

0

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

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.