Pdfable (Wkhtmltopdf)

If you want a simple to use PHP class to use wkhtmltopdf, check out my project here:


It provides a nice OO interface to the command line utility.

UPDATE (2012-11-16):

This is also available as Yii extension now: http://www.yiiframew…ension/pdfable/

Nice (and thanks!). Have you considered writing a Yii extension wrapper for it at some point or should I put that on my list of things to do when I get the chance?

I tried to make this class independent of Yii. It is so simple that i don’t think, we need another wrapper around it to use it in Yii. Actually i’m already using it in my Yii projects. In my opinion not every third party library needs to be wrapped up in an extension.

Or let me ask the other way round: What should a wrapper class add what the original class doesn’t already provide?

From what I remember from when I looked at WkHtmlToPdf a few years ago, there was a way to set headers/footers. Maybe we could set that on an app basis in an extension?

Good point on the library stuff. A counter point though is that people who don’t know that much about PDF creation from HTML may look through the extension section and never end up using your gem, because they don’t find it. Wouldn’t it help raise the visibility of your library (and thus help people more often) to make it an extension?

You can add header and footer like this:






// Then either set it for every page:


// Or only for a specific page:

$pdf->addPage('page.html', $options);

See wkhtmltopdf -H for more options.

I agree that an extension might help to increase popularity. I’m just not sure if this really makes sense: This class is not related to Yii at all. It’s rather a standalone class which you can easily integrate into Yii. And for now the home is the github page. I don’t want to maintain two downloads for this class. But let me think about it, maybe i change my mind … :)

What a wrapper class could do: Add getter/setter support through CComponent and wrap up errors in exceptions. Let’s see.

Thanks for the class. WkHtmltoPdf seems to be a nice,quick pdf solution.

It would be nicer if there is a wiki and/or extension of it.

I try to use it, but because of safe mode restrictions I couldnt find a way of using it so far.

Edit: Ok I did it.

I made safe_mode_exec_dir =/tmp/wkm/

and put binary there. Modified class accordingly. It works now except css formatting.

I search for howto include a css file.

If you add a page from URL you don’t have to include a CSS file - wkhtmltopdf will fetch the HTML and whatever CSS file is defined inside the page. But you can also set a static CSS file like this:



    'user-style-sheet'  => '/path/to/your/own/pdf.css',


I tried both.

I am using your class seeding it with $html by $pdf->addPage($page); where $page is a local html file.

A css link inside html file like this <head><link rel=\"stylesheet\" href=\"css/detailview.css\" type=\"text/css\"/> dont work.

Maybe because wkhtmltopdf dont understand where css/detailview.css file is.

For the second I have used



'user-style-sheet' =&gt; 'detailview.css', ('css/detailview.css' neither works) 


that didnt work neither. Maybe because I didnt use absolute paths. I will try the absolute paths.

Your class and wkhtmltopdf deserves an extension and a wiki.

very nice, thank you!! :)

Quick update: V1.1.0 is out, which adds support to render a PDF page from a HTML string.

BTW i’m preparing a Yii extension now, which should make it very easy to render PDFs from one/several Yii pages.

Looking forward to it, thanks! :slight_smile:

I just added this extension now:


thanks mike its make easy to create pdf!!

but I have a question about TOC,

how to use it? because I get an error when I try to use it…thanks…

How do you use it and what error do you get?

I use it like this source and I put it in my function:



		  array('toc-header-text' => 'test',), 


and I get a blank page…


Can you set YII_DEBUG to true and configure a log route, which logs the "ext.*" category? You should see some error message (hopefully).

I didn’t see some error message mike…hehe…

when I use header option or page option,its all work, but when I try to use TOC option it didn’t work (blank page)…


Then you need to do some manual debugging: You could echo the $command in line 287 in WkHtmlToPdf.php, in the createPdf() method right after the $command = $this->getCommand($fileName); line.

Personally i’ve never used a TOC, so it’s very well possible that there’s still a bug here.

this is result from echo $command:


--outline --encoding 'UTF-8' 

--margin-top '65' --margin-right '10' 

--margin-bottom '20' --margin-left '10' 

--title 'RKAKL' 

--page-size 'A4' 

--dpi '600' /home/Documents/work/protected/runtime/tmp_WkHtmlToPdf_1mbnkb.html 

--user-style-sheet '/var/www/work/css/main.css' 

--header-html '/var/www/work/header.html' 

--header-spacing '1' 

--toc-header-text 'test' /home/Documents/work/protected/runtime/tmp_WkHtmlToPdf_Y7JFBp

sorry mike I’m late for reply…hehe

Hmm. I think “toc-header-text” is a toc option. So you can’t add this as a page option as you did.

Can you try to use addToc($tocOptions) instead? You need to create the PDF a little different in this case. See the multi page example.