Kirby + PrinceXML?

Dear fellow Kirby lovers,

Has anyone tried to plug Kirby with [PrinceXML][1]?

What I want to do is:

  • generate content through Kirby
  • create a beautiful PDF (with pages etc.) from the content

Also, if some of you have other ideas on how to do that, please let me know.

Thank you
[1]: http://www.princexml.com/

You can use the Prince PHP library to access Prince from your PHP code.

Regarding Kirby integration: You can simply drop the prince.php into your plugin directory and then create another plugin that uses the library in the Kirby context. A good way of implementing dynamic PDF generation could be to create a route that serves the PDF of a given page:

// Make sure to load Prince before this plugin
kirby()->plugin('prince');

kirby()->routes(array(
  array(
    'pattern' => 'pdf/(:all)', 
    'action' => function($path) {
      $page = page($path);
      $prince = new Prince('<path>');
      
      // Do stuff with $page and generate HTML code for the PDF
      // You could use an HTML template inside your plugin
    }
  )  
));
1 Like

Thanks @lukasbestle!
This looks really exciting! Have you tried it yourself?
Would you recommend using Prince? Or do you know of a better (cheaper) option?

I have used Prince for a project that required dynamic HTML to PDF conversion. Prince is the only good library for this use-case.

If you only need a static template (the same layout, only different content), I can highly recommend TCPDF. It has an awful API but once you know how to use it, it works pretty well. And it’s free and open source. :smiley:

If you want to use TCPDF, you can swap out the word prince with tcpdf in my previous post. It works with Kirby in the same way.

1 Like

Thanks @lukasbestle
TCPDF: OMG this website…
Would you have a place where I can see it work?
Cheers!

It gets even worse once you take a look at the documentation and the source code. :wink:

I don’t have any public TCPDF examples, but you can learn a lot about how it works in the official examples.

OK I’ll try and dig into it.
Thanks a lot

You are welcome. :smile: