# MPDF compatibility with Kirby

**URL:** <https://forum.getkirby.com/t/mpdf-compatibility-with-kirby/17846>\
**Category:** Questions\
**Tags:** v3\
**Created:** [April 10, 2020, 8:32pm UTC](https://forum.getkirby.com/t/mpdf-compatibility-with-kirby/17846 "2020-04-10T20:32:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhampu](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/zhampu/32/7529_2.png) [@zhampu](https://forum.getkirby.com/u/zhampu)\
**Post date:** [April 10, 2020, 8:32pm UTC](https://forum.getkirby.com/t/mpdf-compatibility-with-kirby/17846/1 "2020-04-10T20:32:22Z")

</div>

Hey,

I was wondering if this example with Mpdf will work with Kirby 3 :  
[https://mpdf.github.io/real-life-examples/pdf-from-every-page-of-website.html](https://mpdf.github.io/real-life-examples/pdf-from-every-page-of-website.html).

Has anybody tried this?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 10, 2020, 8:49pm UTC](https://forum.getkirby.com/t/mpdf-compatibility-with-kirby/17846/2 "2020-04-10T20:49:42Z")

</div>

This is not CMS specific, so yes, if your target system meets the requirements of the library.

---

<div class="post-metadata">

**Author:** ![zhampu](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/zhampu/32/7529_2.png) [@zhampu](https://forum.getkirby.com/u/zhampu)\
**Post date:** [April 10, 2020, 9:06pm UTC](https://forum.getkirby.com/t/mpdf-compatibility-with-kirby/17846/3 "2020-04-10T21:06:58Z")

</div>

Thanks for the quick answer!  
Yes I do have everything install but im not sure where to put the custom php in Kirby. as the templates or Snippets creates pages.  
Could you point me where to look for custom PHP files inside Kirby?

---

<div class="post-metadata">

**Author:** ![bruno](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bruno/32/6422_2.png) [@bruno](https://forum.getkirby.com/u/bruno)\
**Post date:** [April 10, 2020, 9:12pm UTC](https://forum.getkirby.com/t/mpdf-compatibility-with-kirby/17846/4 "2020-04-10T21:12:31Z")

</div>

Without looking at the example page: you should be able to just put any php file in the templates folder (ex. `site/templates/myfile.php`) and also create an empty folder with the same name inside the `/content` directory (ex. `content/myfile`) so Kirby knows what to look for.

Edit: Alternatively you could put the code in a route and pass the url to the page you want processed as a parameter. But as @texnixe answered below, if there is a composer plugin, you can easily use it with Kirby.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 10, 2020, 9:18pm UTC](https://forum.getkirby.com/t/mpdf-compatibility-with-kirby/17846/5 "2020-04-10T21:18:48Z")

</div>

I’d load the library in a plugin with composer as described in the installation docs: [https://mpdf.github.io/installation-setup/installation-v7-x.html](https://mpdf.github.io/installation-setup/installation-v7-x.html)

[https://getkirby.com/docs/guide/plugins/plugin-setup-composer](https://getkirby.com/docs/guide/plugins/plugin-setup-composer)

---

<div class="post-metadata">

**Author:** ![zhampu](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/zhampu/32/7529_2.png) [@zhampu](https://forum.getkirby.com/u/zhampu)\
**Post date:** [April 10, 2020, 10:38pm UTC](https://forum.getkirby.com/t/mpdf-compatibility-with-kirby/17846/6 "2020-04-10T22:38:24Z")

</div>

Thanks! yeah, I’m slowly doing my way to a functional plugin based on that example.  
Some help would be great as I’m no expert in php functions.

so basically `www.website.com/makepdf?url=http%3A%2F%2website.com/page%2F` returns a pdf but is making one letter per page( more than a thousand pages 😛 ).

anybody notices something strange in this code?

```auto
<?php

   @include_once __DIR__. '/vendor/autoload.php';

   Kirby::plugin('KirbyPrint', [
    'routes' => [
        [
            'pattern' => 'makepdf',
            'action' => function () {

                $mpdf = new \Mpdf\Mpdf();

                $url = urldecode($_REQUEST['url']);

                if (count($_POST) > 0) {

                    $ch = curl_init($url);
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

                    foreach ($_POST as $name => $post) {
                        $formvars = array($name => $post . " \n");
                    }

                    curl_setopt($ch, CURLOPT_POSTFIELDS, $formvars);
                    $html = curl_exec($ch);
                    curl_close($ch);

                } elseif (ini_get('allow_url_fopen')) {
                    $html = file_get_contents($url);

                } else {
                    $ch = curl_init($url);
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    $html = curl_exec($ch);
                    curl_close($ch);
                }

                $mpdf = new \Mpdf\Mpdf();
                $mpdf->useSubstitutions = true; // optional - just as an example
                $mpdf->SetHeader($url . "\n\n" . 'Page {PAGENO}'); // optional - just as an example
                $mpdf->CSSselectMedia = 'style'; 
                $mpdf->setBasePath($url);
                $mpdf->WriteHTML($html);
                $mpdf->Output();

            }
        ]
    ]
]);

```

---

<div class="post-metadata">

**Author:** ![bruno](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bruno/32/6422_2.png) [@bruno](https://forum.getkirby.com/u/bruno)\
**Post date:** [April 11, 2020, 11:39am UTC](https://forum.getkirby.com/t/mpdf-compatibility-with-kirby/17846/7 "2020-04-11T11:39:07Z")

</div>

just a blind guess without knowing what the generated pdf looks like (maybe it would help if you uploaded a screenshot?), maybe the stylesheet is not being applied to the file?

have you tried setting error messages [https://mpdf.github.io/troubleshooting/error-messages.html](https://mpdf.github.io/troubleshooting/error-messages.html)

---

<div class="post-metadata">

**Author:** ![zhampu](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/zhampu/32/7529_2.png) [@zhampu](https://forum.getkirby.com/u/zhampu)\
**Post date:** [April 11, 2020, 5:15pm UTC](https://forum.getkirby.com/t/mpdf-compatibility-with-kirby/17846/8 "2020-04-11T17:15:58Z")

</div>

Thanks for the help, i’m trying to develop this as a plugin as many of my peers are interested.

So here is the code that is the header of every page of the website:

```auto
<script language="javascript" type="text/javascript">
        /* <![CDATA[ */
        document.write('<a href="makepdf?url=' + encodeURIComponent(location.href) +'">');
        document.write('Create PDF file of this page');
        document.write('</a>');
        /* ]]> */
    </script>

```

the plugin is this code:  
_note that the en/makepdf is because for now is only on the english version_

```auto
<?php

@include_once __DIR__. '/vendor/autoload.php';

Kirby::plugin('getkirby/pluginkit', [
    'routes' => [
        [
            'pattern' => 'en/makepdf',
            'action' => function () {
                $mpdf = new \Mpdf\Mpdf(['debug' => true]);

                $url = urldecode($_REQUEST['url']);
                if (count($_POST) > 0) {

                    $ch = curl_init($url);
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1 );

                    foreach($_POST as $name => $post) {
                        $formvars = array($name => $post . " \n");
                    }

                    curl_setopt($ch, CURLOPT_POSTFIELDS, $formvars);
                    $html = curl_exec($ch);
                    curl_close($ch);

                } elseif (ini_get('allow_url_fopen')) {
                    $html = file_get_contents($url);

                } else {
                    $ch = curl_init($url);
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 );
                    $html = curl_exec($ch);
                    curl_close($ch);
                }

                $mpdf = new \Mpdf\Mpdf(['debug' => true]);

                $mpdf->useSubstitutions = true; // optional - just as an example
                $mpdf->SetHeader($url . "\n\n" . 'Page {PAGENO}'); // optional - just as an example
                $mpdf->CSSselectMedia='mdpf'; // assuming you used this in the document header
// $mpdf->CSSselectMedia='global'; // assuming you used this in the document header
// $mpdf->CSSselectMedia='style'; // assuming you used this in the document header
                $mpdf->setBasePath($url);
                $mpdf->WriteHTML($html);
                $mpdf->Output();

            }
        ]
    ]
]);

```

and this is how im loading my css:

```auto
<?= css('assets/css/global.css') ?>
<?= css('assets/css/style.css') ?>
<?= css('assets/css/responsive.css') ?>
<?= css('assets/css/print.css', ['media' => 'mpdf']) ?>
 <link rel="stylesheet" href="https://webfonts.fontstand.com/WF-016037-0b487f90aeb5e5f8d30879a0e80945e7f.css"
          type="text/css"/>

```

results not very clean so far:

 ![Screenshot 2020-04-11 at 19.11.51](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/8/8f910a70d6079e1f7aee7eff6ecd7fc91de0fd93.png)  
 ![Screenshot 2020-04-11 at 19.12.02](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/2/22ce539c30ebfb31c642e02ed63fab38e8d314b6.png)

when I change `['media' => 'mpdf']` to` ['media' => print']` and `$mpdf->CSSselectMedia='mdpf';` to `$mpdf->CSSselectMedia='print';`I get a funny pdf with one letter per page:

 ![Screenshot 2020-04-11 at 20.02.37](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/2/25c7f39d951ada8639d692d769d54b7a37e5e629.png)

---

<div class="post-metadata">

**Author:** ![JanStieler](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/janstieler/32/15642_2.png) [@JanStieler](https://forum.getkirby.com/u/JanStieler)\
**Post date:** [December 10, 2020, 3:23pm UTC](https://forum.getkirby.com/t/mpdf-compatibility-with-kirby/17846/9 "2020-12-10T15:23:42Z")

</div>

Hi,  
for your problem with the CSS.  
I used MPDF in a K2 installation for PDF generation and declared the CSS-File in this manor:

```auto
$stylesheet = file_get_contents('./assets/css/pdf.css');
$mpdf->WriteHTML($stylesheet, 1);

```

Cheers
