Yii Framework Forum: Pdfable (Wkhtmltopdf) - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Pdfable (Wkhtmltopdf) Create PDFs with wkhtmltopdf Rate Topic: -----

#1 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 25 April 2012 - 05:50 AM

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

http://mikehaertl.gi...phpwkhtmltopdf/

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/
0

#2 User is offline   acorncom 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 100
  • Joined: 25-February 11
  • Location:CO, USA

Posted 25 April 2012 - 10:56 PM

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?
0

#3 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 26 April 2012 - 01:41 AM

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?
0

#4 User is offline   acorncom 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 100
  • Joined: 25-February 11
  • Location:CO, USA

Posted 26 April 2012 - 08:26 AM

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?
0

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 26 April 2012 - 11:23 AM

You can add header and footer like this:

<?php
$options=array(
    'footer-html'=>'footer.html',
    'header-html'=>'header.html',
));

// Then either set it for every page:
$pdf->setPageOptions($options);

// 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.
0

#6 User is offline   wyginwys 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 15-February 12

Posted 14 May 2012 - 10:51 AM

View PostMike, on 26 April 2012 - 11:23 AM, said:



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.
0

#7 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 15 May 2012 - 01:22 AM

View Postwyginwys, on 14 May 2012 - 10:51 AM, said:

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:


<?php

$pdf->setPageOptions(array(
    'user-style-sheet'  => '/path/to/your/own/pdf.css',
));


0

#8 User is offline   wyginwys 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 15-February 12

Posted 15 May 2012 - 05:48 AM

View PostMike, on 15 May 2012 - 01:22 AM, said:

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:


<?php

$pdf->setPageOptions(array(
    '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
$pdf->setPageOptions(array(
'disable-smart-shrinking',
'user-style-sheet' => '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.
0

#9 User is offline   jiang33 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 17-April 12

Posted 15 May 2012 - 09:10 AM

very nice, thank you!! :)
0

#10 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 18 October 2012 - 08:55 AM

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.
0

#11 User is offline   acorncom 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 100
  • Joined: 25-February 11
  • Location:CO, USA

Posted 20 October 2012 - 08:28 PM

View PostMike, on 18 October 2012 - 08:55 AM, said:

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! :-)
0

#12 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 16 November 2012 - 08:55 AM

I just added this extension now:

http://www.yiiframew...ension/pdfable/
0

#13 User is offline   rifa_dayyan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 27-November 12
  • Location:Indonesia

Posted 30 November 2012 - 03:23 AM

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..
0

#14 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 01 December 2012 - 04:24 AM

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

#15 User is offline   rifa_dayyan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 27-November 12
  • Location:Indonesia

Posted 02 December 2012 - 10:44 PM

View PostMike, on 01 December 2012 - 04:24 AM, said:

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


I use it like this source and I put it in my function:
$this->renderPdf('printRkakl', 
                  array('model'=>$this->loadModel($id)), 
		  array('toc-header-text' => 'test',), 
                 'program_'.$id.'.pdf');


and I get a blank page..

thanks
0

#16 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 03 December 2012 - 03:06 AM

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

#17 User is offline   rifa_dayyan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 27-November 12
  • Location:Indonesia

Posted 04 December 2012 - 04:15 AM

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)...

thanks..
0

#18 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 05 December 2012 - 03:46 AM

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.
0

#19 User is offline   rifa_dayyan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 27-November 12
  • Location:Indonesia

Posted 18 December 2012 - 04:14 AM

this is result from echo $command:
'/usr/local/bin/wkhtmltopdf' 
--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
0

#20 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 18 December 2012 - 04:41 AM

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.
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users