Publish your data from TYPO3 as Website and as PDF with TCPDF
Ever needed to create a PDF document from your data on-the-fly? Web Essentials has created a solution for one of its clients which allows to publish job advertisements
- online on a website and
- as PDFs for print
from a single central source: the TYPO3 Content Management System.
The PDF solution is based on theTCPDF library. It allows to generate all sorts of PDFs as can be seen in theexamples section of TCPDF which comes with PDF and according PHP snippet.
But as usual, the difficulty lies in the detail. In the case of our client, we needed to produce PDF documents to a given width, but with a flexible height. How can the height of a PDF be calculated from a dynamic content without loosing the flexibility of inserting images, headings, paragraphs etc. dynamically? And how can styles be kept flexible for future adjustments without having to re-program and re-calculate all heights?
TCPDF has a neat functionality which consists of creating a virtual document first which will be filled with the content. Once the virtual document is created, its measurements are taken in order to create the real PDF document. This allows to generate the PDF with the exact needed height, but without having to calculate the final height in advance!
Here is the code snippet:
// create virtual page with maximum height
$pdf->startTransaction();
$pdf->AddPage('P', array($totalW, 5*841.890)); // maximum height 5*A4
$h1 = $pdf->GetY();
$pdf->writeHTML($html, false, false, false, false, 'center');
$h2 = $pdf->GetY();
$totalH = $h2-$h1 + TOP_MARGIN + FOOTER_MARGIN;
$pdf = $pdf->rollbackTransaction();
// create page with correct height
$resolution = array($totalW, $totalH);
if ($totalW <= $totalH) {
$pdf->AddPage('P', $resolution);
}
else {
$pdf->AddPage('L', $resolution);
}
The Web Essentials team is happy to provide your custom PDF solution, be it an auto-generated invoice for an online shop, an itemised list from your database, or the styled print-version of your online calendar.
If you are a developer or TYPO3 enthusiast working with TCPDF, do not hesitate to get in touch with us, we like to connect with other developers and discuss solutions.