Yii 1.1: pdfable

Create PDFs from web pages with PHPWkHtmlToPdf
26 followers

PDFable

PDFable is a Yii extension to create PDFs from web pages with PHPWkHtmlToPdf.

Documentation

For the documentation please have a look at the project page on github.

Requirements

PHPWkHtmlToPdf depends on wkhtmltopdf. So make sure, you have a wkhtmltopdf binary installed on your system.

This extension was developed on Yii 1.1.x. But it should work on other versions, too.

Example

Here's just a quick example:

<?php
class DemoController extends Controller                                                                                      
{                                                                                                                            
    public function behaviors()                                                                                              
    {                                                                                                                        
        return array(                                                                                                        
            'pdfable'=>array(                                                                                                
                'class' => 'ext.pdfable.Pdfable',                                                                            
            ),                                                                                                               
        );                                                                                                                   
    }
 
    public function actionPdfDemo()                                                                                          
    {                                                                                                                        
        // Render this view as PDF and display inline in the browser:                                                        
        $this->renderPdf('pdfDemo');                                                                                         
    }
}

Changelog

1.0.1

1.0.0

  • Initial release

Total 20 comments

#16981 report it
Mike at 2014/04/18 04:11am
Re: Hi! blank page

Can you please open an issue on github?

#16979 report it
sergmoro1 at 2014/04/18 01:16am
Hi! blank page

/protected/runtime/application.log

2014/04/17 16:27:33 [error] [ext.pdfable.Pdfable] Could not create PDF for view invoice in /home/sergmoro1/www/tuz/protected/extensions/pdfable/Pdfable.php (209) in /home/sergmoro1/www/tuz/protected/extensions/pdfable/pdfable/controllers/DemoController.php (53) in /home/sergmoro1/www/tuz/protected/extensions/pdfable/pdfable/controllers/DemoController.php (53)

2014/04/17 16:38:29 [error] [ext.pdfable.Pdfable] Could not create PDF for view invoice in /home/sergmoro1/www/tuz/protected/extensions/pdfable/Pdfable.php (209) in /home/sergmoro1/www/tuz/protected/controllers/OrderController.php (202) in /home/sergmoro1/www/tuz/protected/controllers/OrderController.php (202)

public function actionInvoice($id) {
  $this->layout = '/layouts/pdf';
  $this->renderPdf('invoice',
    array('model'=>$this->loadModel($id)),
    array(), 
    Yii::getPathOfAlias('webroot') . '/images/invoice/invoice_' . $id . '.pdf'
  );
}
#14508 report it
jcsmesquita at 2013/08/19 06:18am
Re: Re: Re: Re: Nice!

Moving to GitHub... thanks!

#14507 report it
Mike at 2013/08/19 03:07am
Re: Re: Re: Nice!

If you do it like in your code, you're not really using any of the extension features at all. You could also have used PHPWkHtmlToPdf directly. The idea of this extension is, to make it easy to integrate it with Yii. So renderPdf() should work.

If you can create a boiled down example that demonstrates your problem with widgets, you could create an issue on the github page and i can have a look.

#14503 report it
jcsmesquita at 2013/08/18 11:33am
Re: Re: Nice!

Hey Mike,

My mistake! To clarify, I got it working using this:

$pdf = new WkHtmlToPdf();
$pdf->addPage("http://localhost/myproject/print/invoice");
$pdf->send();

So that's why I said it was session-less :/ For some when I try the renderPdf approach as shown in the example it doesn't render the widget in my view... The widget has some javascript and requires jQuery, I'm getting the feeling that none of my js is working with renderPdf

#14499 report it
Mike at 2013/08/18 05:40am
Re: Nice!

Thanks jcsmesquita.

I'm not sure though, what you mean by "session-less". As you can see from the example, the extension integrates transparently into Yii. You can use the same access control mechanisms as for any other Yii action. Sessions do work.

#14495 report it
jcsmesquita at 2013/08/17 07:21pm
Nice!

Great work mike thanks for making this extension!

I needed this to produce pdfs for the fullcalendar plugin, works great with wkhtmltopdf-0.10.0_rc2-static-amd64 tested on Ubuntu 10.04.

One note, if anybody is trying to include a query with GET parameters, the url needs to be in quotes. Also this solution is session-less, so for example if you're trying to do this for a backend you'll need to have a controller/action that is publicly accessible, then probably just implement some sort of key (in POST or GET) in order to authenticate those requests.

#12703 report it
02xK at 2013/04/06 11:49am
Re: Re: wkhtmltopdf: cannot connect to X server

It's a wkhtmltopdf problem if you want to run it with x11. As a workaround you have to run it with xvfb-run or do this: https://code.google.com/p/wkhtmltopdf/wiki/compilation

#12702 report it
Mike at 2013/04/06 11:19am
Re: wkhtmltopdf: cannot connect to X server

This rather looks like a problem with wkhtmltopdf. Does it run on the command line? If not and you still think, the problem is caused by the behavior please open a new issue on the github page.

#12701 report it
02xK at 2013/04/06 10:20am
wkhtmltopdf: cannot connect to X server

"wkhtmltopdf: cannot connect to X server" on ubuntu 12.04.2

Fix: apt-get install xvfb

// WkHtmlToPdf.php
public function getCommand($filename)
{
        $command = 'xvfb-run ';
        // ...

Other attempt:

'bin' => 'xvfb-run /usr/bin/wkhtmltopdf',

... but error: Could not execute xvfb-run /usr/bin/wkhtmltopdf

#12523 report it
Mike at 2013/03/25 04:57pm
Re: Blank page

@jhonka I think it's easier to discuss if you open an issue over at github. Please also include the full command call to wkhtmltopdf. The http:// in front of the file paths seem very odd to me.

#12522 report it
jhonka at 2013/03/25 04:26pm
Blank page

I have installed the extension and am running into problems with the demo. I seem to be having similar problems to what report fleuryc ran into. When I try to export the PDF from the demo site, I get a blank page.

I tried installing the stand alone wkhtmltopdf v. 10.0 rc2 (there was a bug in v 11 that prevented it from running) but the standalone didn't seem to help. When I run wkhtmltopdf from the command line in RedHat, it works without a problem.

I did as advised to fleuryc and output the command of the multipage pdf and ran that in my RedHat server and got the following error:

Loading pages (1/6)
Error: Failed loading page http:///var/www/ ... /tmp_WkHtmlToPdf_9RbV2Q.html (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http:///var/www/ ... /runtime/tmp_WkHtmlToPdf_jKGGSW.html (sometimes it will work just to ignore this error with --load-error-handling ignore)
Error: Failed loading page http:///var/www/ ... /tmp_WkHtmlToPdf_fXSwI2.html (sometimes it will work just to ignore this error with --load-error-handling ignore)

Do you guys have any ideas?

#11815 report it
fleuryc at 2013/02/05 11:51am
Options in incorrect location

Hi!

I just created a issue, becaus I get an error when setting page options :

$pdf = $this->createPdf();
$pdf->renderPage('foo', array('model'=>$model), array('orientation'=>'Landscape'));
$pdf->send();
--orientation specified in incorrect location

https://github.com/mikehaertl/pdfable/issues/2

Cheers!

#11712 report it
Mike at 2013/01/29 03:09am
Re: Additional method to setPdfOptions

But you already can override options when you render a page. Look at the signatures of renderPdf() or the advanced renderPage():

<?php
public function renderPdf($view, $data=array(), $options=array(), $filename=null)
public function renderPage($view, $data=array(), $options=array())

You can override also single options there. They will be merged with the options you configured in the behavior.

I also recommend to have a look at the sources to find out about all other possible arguments.

#11674 report it
hoplayann at 2013/01/26 04:51am
Re: Additional method to setPdfOptions

Hey Mike,

Thanks for your comment, Well in my case I have a set of options that I always want to load, and I do it in the behaviors() of the controller when attaching pdfable. But later on, in my controller action, I want to specify additional options depending on the (post) data I am handling.

#11673 report it
Mike at 2013/01/26 04:39am
Re: Additional method to setPdfOptions

Can you give an example, where this would be useful? You actually already have many ways to supply options for your PDF, so i can't really think of a situation where it would be neccessary to set some single global options.

#11661 report it
hoplayann at 2013/01/25 08:43am
Additional method to setPdfOptions

Hey Mike,

here is a suggestion to add to the Pdfable behavior, in order to set options after the behavior was attached to the Controller.

/**
 * Sets options/values to the existing $_options array. 
 * 
 * - A wkthmltopdf option with value is simple overwritting or adding key=>value to the options array.
 * 
 * - For a wkthmltopdf option without argument, we store it as a (int)key => option name, 
 * by simple setting the $value parameter to true or false. 
 * Example: 
 * $this->setPdfOption('header-line', true); gives $this->_options[0] = 'header-line';
 * 
 * @param string $option for wkhtmltopdf. See 'wkthmltopdf -H'.
 * @param string $value of option for wkhtmltopdf. See 'wkthmltopdf -H'.
 */
public function setPdfOption($option, $value) {
    if ($value === true OR $value === false) {
        if (in_array($option, $this->_options) AND $value === false) {
            foreach ($this->_options as $index => &$optionLabel) {
                if ($optionLabel === $option) {
                    // key is found, unset the entry
                    unset($this->_options[$index]);
                }
            }
        } else if (!in_array($option, $this->_options) AND $value) {
            // add to array
            $this->_options[] = $option;
        }
    } else {
        $this->_options[$option] = $value;
    }
}

What do you think about it ?

Cheers,

Yann

#11637 report it
hoplayann at 2013/01/24 06:31am
Windows version of wkhtmlToPdf does not support user-style-sheet

Hello,

Thanks for your work. I am just implenting it right now.

When rendering, I am also experiencing the problem with windows + the user-style-sheet option from wkhtmltopdf mentionned here : http://mikehaertl.github.com/phpwkhtmltopdf/

For now I'll use inline CSS.

#11450 report it
fleuryc at 2013/01/14 10:22am
Re: Re: Re: blank page

Dammit! The 'bin' parameter won't get overridden...

EDIT : OK, gottit : need to be configured in the behavior() part of the controller. The config.php bin parameter doesn't seem to be useful, is it?

EDIT2 : OK, gottit² : the config.pgp pdfable module is just for the demo. To actually be able to use the extension, you must attach the behavior to the controller.

Dumb me...

#11449 report it
fleuryc at 2013/01/14 10:01am
Re : Static wkthmltopdf

Arrgh!

Allright, thanks for your help!

Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: New BSD License
  • Developed by: Mike
  • Category: Others
  • Votes: +15
  • Downloaded: 1,128 times
  • Created on: Nov 16, 2012
  • Last updated: Dec 18, 2012
  • Tags: PDF, behavior