Page 1 of 1

How to print directly to the printer?

Posted: Mon Aug 23, 2010 9:16 am
by jlcsusara
Hi!

I'm an absolute beginner when it comes to PHP and Javascript.

How do I print a report (outputted as a PDF) directly to the printer?

Thanks in advance!

--Jonathan

Re: How to print directly to the printer?

Posted: Tue Aug 24, 2010 3:23 am
by steven
Johnathon,

You can't print directly to the printer from a browser without some kind of 3rd party product. (http://www.meadroid.com/scriptx/)

The best you can do with JavaScript is use window.print().

This will bring up a dialog box, but you still have to click print.

(If you could, people could create malicious web pages that would use up all the paper in your printer)

Steven

Re: How to print directly to the printer?

Posted: Tue Aug 24, 2010 4:05 pm
by jlcsusara
Thanks, Steven... good point.

Re: How to print directly to the printer?

Posted: Mon Aug 01, 2011 8:32 am
by admin
.

Re: How to print directly to the printer?

Posted: Thu Apr 12, 2012 10:04 am
by MichaelD
I just encountered this "problem" and solved it for me. Maybe it is also useful for others.

Printing of PDFs can be automatically started via javascript.

Please note that JavaScript in PDF is much different from Javascript in HTML. The first is executed by (usually) Acrobat while second uses the API of the Webbrowser.

One condition is that the user has not disabled Javascript in Acrobat.

There's a little addon which allows to add Javascript in PDFs when using FPDF:
http://www.fpdf.de/downloads/addons/36/
I downloaded the zip and placed fpdf_js.php in the FPDF subfolder of nubuilder.

Then I changed "run_report_pdf_v2.php": (make a backup copy of this file before altering)
After the line

Code: Select all

require('fpdf/fpdf.php');
I added this line:

Code: Select all

require('fpdf/pdf_js.php');
I changed this line:

Code: Select all

class nuPDF extends FPDF{
To:

Code: Select all

class nuPDF extends PDF_Javascript{
Then I added the following method (can be pasted above "function __construct ...")

Code: Select all

function AutoPrint($dialog=true)
    {
        $param=($dialog ? 'true' : 'false');
        $script="print($param);";
        $this->IncludeJS($script);
    }
Finally I added the following line in the function "build_report" below "$pdf = new nuPDF(..."

Code: Select all

$pdf->Autoprint(true);
This should Acrobat let show automatically the Print Dialog when any PDF is generated via nuBuilder.
To suppress the Print-Dialog pass "false" to the Autoprint function.

Maybe there's a more elegant way to implement this (without patching the source). I would be happy to read about this.

Re: How to print directly to the printer?

Posted: Tue Apr 17, 2012 2:04 am
by admin
Thanks Michael.