Welcome to the nuBuilder forums!

Please register and login to view forums and other content only available to registered users.

How to print directly to the printer?

Post Reply
jlcsusara
Posts: 23
Joined: Wed Apr 07, 2010 10:32 am

How to print directly to the printer?

Unread post 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
steven
Posts: 218
Joined: Mon Jun 15, 2009 10:03 am
Has thanked: 1 time

Re: How to print directly to the printer?

Unread post 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
If you like nuBuilder, how about leaving a nice review on SourceForge?
jlcsusara
Posts: 23
Joined: Wed Apr 07, 2010 10:32 am

Re: How to print directly to the printer?

Unread post by jlcsusara »

Thanks, Steven... good point.
admin
Site Admin
Posts: 2781
Joined: Mon Jun 15, 2009 2:23 am
nuBuilder Version: 4.5
Been thanked: 1 time

Re: How to print directly to the printer?

Unread post by admin »

.
MichaelD
Posts: 2
Joined: Wed Apr 04, 2012 11:08 am

Re: How to print directly to the printer?

Unread post 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.
admin
Site Admin
Posts: 2781
Joined: Mon Jun 15, 2009 2:23 am
nuBuilder Version: 4.5
Been thanked: 1 time

Re: How to print directly to the printer?

Unread post by admin »

Thanks Michael.
Post Reply