MPDF compatibility with Kirby

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.

Has anybody tried this?

1 Like

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

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?

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.

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://getkirby.com/docs/guide/plugins/plugin-setup-composer

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

anybody notices something strange in this code?

<?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();

            }
        ]
    ]
]);

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

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:

<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

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

<?= 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:


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:

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:

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

Cheers