pdfable

Create PDFs from web pages with PHPWkHtmlToPdf
27 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

#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!

#11447 report it
Mike at 2013/01/14 09:39am
Static wkthmltopdf

Forgot to post the link to the download page:

http://code.google.com/p/wkhtmltopdf/wiki/static

#11446 report it
Mike at 2013/01/14 09:36am
Re: Re: Re: blank page

Unfortunately, the wkhtmltopdf version which is shipped by some distros (i only know about Ubuntu, but it's probably the same for others), does not use the patched version of the Qt library.

The solution is, to install the statically linked binary version for wkhtmltopdf. You can download it for your architecture on it's Google code page. I can not really help you much, how to do it, but this information here should hopefully help you out:

http://code.google.com/p/wkhtmltopdf/wiki/static

#11445 report it
fleuryc at 2013/01/14 09:22am
Re: Re: blank page

@Mike:

the command is :

'/usr/bin/wkhtmltopdf' --no-outline --encoding 'UTF-8' --margin-top '0' --margin-right '0' --margin-bottom '0' --margin-left '0' /home/cfleury/www-dev/vdg_efr/protected/runtime/tmp_WkHtmlToPdf_qt2ZSZ.html --user-style-sheet '/home/cfleury/www-dev/vdg_efr/protected/extensions/pdfable/pdfable/assets/css/pdf.css' /home/cfleury/www-dev/vdg_efr/protected/runtime/tmp_WkHtmlToPdf_2T2A5u.html --user-style-sheet '/home/cfleury/www-dev/vdg_efr/protected/extensions/pdfable/pdfable/assets/css/pdf.css' /home/cfleury/www-dev/vdg_efr/protected/runtime/tmp_WkHtmlToPdf_sbghi0.html --user-style-sheet '/home/cfleury/www-dev/vdg_efr/protected/extensions/pdfable/pdfable/assets/css/pdf.css' multi.pdf

and the result (executed via CLI) :

Unknown long argument --no-outline
 
Name:
  wkhtmltopdf 0.9.9
 
...

If I remove the --no-outline option, I get this error:

Error: This version of wkhtmltopdf is build against an unpatched version of QT, and does not support more then one input document.

Thaks for your help!

#11441 report it
Mike at 2013/01/14 07:12am
Re: blank page

If you try the advanced example, you can fetch the command line sent to wkthtmltopdf with $pdf->getCommand(). What happens if you echo this command right before sendPdf() is called and call that command manually on the command line?

#11407 report it
fleuryc at 2013/01/11 11:14am
blank page

Hi!

I can't get it to work. I follow the installation instructions, wkhtmltopdf is correctly installed on my Ubuntu workstation.

On the demo page, when I click the "Download" link, I get a empty blank page (nothing is returned). Same when I use it in one of my actions :

public function actionEdit($id)
    {
        $this->layout = 'application.views.layouts.pdf';
 
        $this->renderPdf('edit', array('model'=>$this->loadModel($id)));
    }

Thanks for your help.

#10732 report it
Mike at 2012/11/20 03:10am
Re: pdf-page blank

Sorry, you need wkhtmltopdf installed on your system. I've updated the description to make this more clear.

#10729 report it
gevalia at 2012/11/19 02:30pm
pdf-page blank

When I try out the demo and click on "single page pdf" for example, all I get is a blank screen, not even a border of the pdf. Does that mean I don't have PHPWkHtmlToPdf installed correctly?

Leave a comment

Please to leave your comment.

Create extension