5
votes

I know that there are ways to print a PDF to a network printer located on the same network as the server, but that does not help me as the server is remote. In my situation a user clicks a link to "print labels" which then generates and outputs a PDF file formatted for them. I currently "stream" the file output to the browser such that Adobe Reader automatically opens it using the following code:

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-type: application/pdf");
header('Content-Disposition: attachment; filename="labels.pdf"');
readfile($ServerPathToFile);

Is there something else I can add to this code that will automatically trigger the print dialogue box to open so that they only have to click print? In this case, Google CloudPrint is not an option, nor are other things that require "special setup" on the user end...as this will be used by a variety of users.

2
You wouldn't do this in PHP, since PHP only acts on the server machine. What you want to do happens on the client machine, so you'd use something like Javascript's window.print(). - Ben
@Steve - That won't work because the PDF opens within Adobe Reader (or whatever the user's default PDF software is)...NOT within a web browser window... - techtheatre
I'm fully aware PDFs don't open in the browser window. The "user's default PDF software" is installed on the user's computer. If you want to print, you have to access the print dialog via the user's computer, which cannot and should not be done using PHP. What if all of the websites you visited could just start running stuff on your machine? - Ben
@Steve - The goal here is simply to have the PDF prompt the user print dialog box. I am looking for a way when serving the file (which is done via PHP from the SERVER...not client-side) to set some sort of flag to indicate the the software should print. As others have already suggested, it can be done with JavaScript...but if there is a way to do it in the original file output rather than building extra HTML...that would obviously be a more elegant solution. I am looking for an extra header that i can pass (in PHP) if possible. Please try to keep your postings on-topic and constructive. - techtheatre

2 Answers

3
votes

You could output the PDF to a child window (<iframe>) on the same domain and then call window.print() on that window.

<p>Don't forget to print your document!</p>
<iframe src="/path/to/your/pdfgenerator.php" id="mypdf"></iframe>

<script>
function printIframe(id) {
    var iframe = document.frames ? document.frames[id] : document.getElementById(id);
    var ifWin = iframe.contentWindow || iframe;
    iframe.focus();
    ifWin.printPage();
    return false;
}
</script>

In the iframe page, add this:

function printPage() {
    print();
}
0
votes

I just got off of a project that printed labels from a browser to a dymo label printer on a network. You must do this with javascript. I used the SDK from Dymo and created my own custom interface to auto print name tags once the person registered at the kiosk. The name badges were then waiting for them at the main checkin station.

The srcipt i used only needed to be installed on the server and all others in the network could print to the network printer.