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 ¶
- Update to PHPWkHtmlToPdf 1.1.4
- Include
composer.json
1.0.0 ¶
- Initial release
Good idea, please format the code
It's a good idea, i've not tried yet, but it's a good idea.
Please, format the page and include an example of rendered PDF.
Is it able to rendere the entire view ? a partial view ? what yes, what no?
Add PDF tag
Re: ...
Thanks.
What do you mean by "format the page"? I think it already is.
I'm not so sure about an example PDF file: You can render any PDF you want, so why include an example?
Basically you can create a PDF from almost everything. As long as you can get the HTML as string (e.g. from
renderPartial()
) you can pass it to the PDF file.The tricky part is to tweak your CSS for PDFs. I've given some hints here.
Thanks
Ok, now page is well-formatted ... probably I've written my comment too fast... sorry
this is not clear for me.
What is 'pdfDemo' ? A view name ?
Can I pass a view name to renderPdf(); ?
Can I do renderPdf(_admin) ? I really dont' understand...
Thanks!
Nice work Mike!
Re:
"this is not clear for me. Can I passa a view name to renderPdf();"
Yes.
renderPdf()
is very similar torender()
. Here's the full doc block:<?php /** * Render a single view as PDF and send it to the browser * * @param string $view name of the view to be rendered. See CController::render. * @param array $data view data. See CController::render. * @param array $options options for this page. See 'wkhtml -H' for available page options * @param mixed $filename if provided, a download dialog will open instead of displaying the PDF inline. */ public function renderPdf($view, $data=array(), $options=array(), $filename=null)
About the CSS:
To render a view as PDF, the class first renders the view as usual then stores this rendered view into a temporary file. This file is then passed to the
wkhtmltopdf
binary. It will not copy any CSS files for you. So all the CSS<link>
tags in this file will not work, because usually they will point to relative URLs. So either you use absolute URLs to point to your CSS files, or you create a standalone CSS file for your PDFs.The latter is my preferred method. I pass it as extra argument with
user-style-sheet
to my PDFs.So this class is not really meant to create PDFs from any Yii page. It's more a tool that helps you to create a dedicated PDF using HTML and CSS.
Error on Windows
Thanks Make
wkhtmltopdf instant ways convert to PDF, I had installed on Windows XP but can not execute $this->_options['bin'] = '/usr/bin/wkhtmltopdf'; i try change path to : 'C:\Program Files\wkhtmltopdf'; but stil got same error, anyone give a solution about this?
Re: Error on Windows
I don't use
wkhtmltopdf
on windows, so can't really help much. But you can try to disable theenableEscaping
feature and surround yourbin
path in additional double quotes.Something like this:
<?php array( 'bin' =>'"C:\...."', 'enableEscaping' => false, )
Also read the note for windows users in the PHPWkHtmlToPdf docs and this issue here.
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?
Re: pdf-page blank
Sorry, you need
wkhtmltopdf
installed on your system. I've updated the description to make this more clear.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.
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 beforesendPdf()
is called and call that command manually on the command line?Re: Re: blank page
@Mike:
the command is :
~~~
[sh]
'/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) :
~~~
[sh]
Unknown long argument --no-outline
Name:
wkhtmltopdf 0.9.9
...
~~~
If I remove the
--no-outline
option, I get this error:~~~
[sh]
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!
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
Static wkthmltopdf
Forgot to post the link to the download page:
http://code.google.com/p/wkhtmltopdf/wiki/static
Re : Static wkthmltopdf
Arrgh!
Allright, thanks for your help!
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. Theconfig.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...
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.
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
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.
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.
Re: Additional method to setPdfOptions
But you already can override options when you render a page. Look at the signatures of
renderPdf()
or the advancedrenderPage()
:<?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.
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!
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?
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
. Thehttp://
in front of the file paths seem very odd to me.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
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.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
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.
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.
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
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. SorenderPdf()
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.
Re: Re: Re: Re: Nice!
Moving to GitHub... thanks!
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' ); }
Re: Hi! blank page
Can you please open an issue on github?
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.