that’s just an example I thought about and I would like to hear your opinions regarding a solution.
If for example we have a something like an event signup, where users fill out a form with their data and that data is stored in automatic created subpages, what would be the best workaround, to export the data to a pdf or a csv file (or whatever file format we would like to have)?
Using the kirby api? Creating a virtual document that can be downloaded? Create and update a file inside the event pages content folder?
At the moment, the guide and cookbook is extremely good to show examples, how we can migrate data to kirby, but not in the indirect way.
Hi,
is this question still actually?
I have build a K2 site with both exports. CSV as a conference registration and the pdf generation as automatic invoice generating.
depending on your use case, creating content representations of your pages could be an idea. I often do that for pdf files with MPDF and it works quite nicely.
For example, let’s assume your registration is saved as a page with template “ticket”. In the templates folder create a “ticket.pdf.php” file containing this:
<?php
use Mpdf\Mpdf;
use Mpdf\Config\ConfigVariables;
use Mpdf\Config\FontVariables;
use Mpdf\Output\Destination;
//load custom fonts
$assets = kirby()->roots()->assets();
$defaultConfig = (new ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
$myFontDirs = array_filter(glob("$assets/fonts/*"), 'is_dir');
$defaultFontConfig = (new FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];
//init a pdf document
$pdf = new Mpdf([
'mode' => 'utf-8',
'format' => 'A4', //adjust paper format here
'fontDir' => array_merge($fontDirs, $myFontDirs),
'fontdata' => $fontData + [
//here you can define custom fonts from your assets folder (afaik, must be ttf)
'niveaugrotesk' => [
'R' => 'ng-light.ttf', //regular
'I' => 'ng-light-italic.ttf', //italic
'B' => 'ng-bold.ttf', //bold
'BI' => 'ng-bold-italic.ttf', //bold italic
]
]
]);
//define some CSS for your ticket layout
$css = <<<CSS
@page {
margin: 1cm;
}
body {
font-family: niveaugrotesk;
font-size: 10pt;
color: cmyk(0,0,0,100); /* you can use CMYK colors like this */
}
CSS;
// capture some html to be written on the ticket
// do it like normal kirby templates
ob_start(); ?>
<h1>Ticket for <?= $page->name() ?></h1>
<p>Your ticket is valid for the event: <?= $page->parent()->title()->html() ?></p>
<?php
$html = ob_get_clean(); //save the captured html to this variable
//give the document a title and an author name
$pdf->SetTitle($site->title() . ' - ' . $page->title());
$pdf->SetAuthor($site->title());
//write the css to the file
$pdf->WriteHTML($css, 1);
//write content to the file
$pdf->WriteHTML($html);
//you can also write the HTML to a fixed box
// $pdf->WriteFixedPosHTML($html, 10, 10, 100, 100); //the numbers are the bounds of the box in mm
//respond with the pdf file
$pdf->Output($page->uid() . '.pdf', Destination::INLINE);
What happens if you remove everything from the “.pdf.php” template and just have it say “hello world” or something? This just to make sure it’s not something in the template code that causes the error.
Ok, one issue is that in the Plainkit there is no parent page, so you would have to change the line of code where the parent is called to avoid the PHP error:
<p>Your ticket is valid for the event: <?= $page->parent()->title()->html() ?></p>
If you then add a new test page, it should work, however, for some reason it doesn’t work on the homepage.
Make sure that in multilanguage support, your text files have the language code extension (which is added automatically if you set up languages via the Panel, but not if you do it manually).
On a side note: If your main project doesn’t have Kirby set up with composer, make sure to install mpdf in a plugin, not through the main composer file to avoid duplicate Kirby folders.