About RockPdf - mPDF helper module
mPDF helper module to easily create PDF files and make debugging easier.
Modules that have more to do with assisting development of a site rather than managing its content.
Should be safe for use in production environments. *
|Date Added||August 11, 2018|
|Last Updated||November 27, 2019|
RockPdf is a wrapper for the mpdf library. Mpdf, as you may already know, is a commonly used PHP library that turns HTML content into PDF, complete with (at least basic) CSS styling, embedded images, and so on. While you could include the mpdf library directly in your code, accessing it through a module has some benefits:
First of all you have access to an instance of mpdf anywhere in your template or module files simply by calling $modules->get('RockPdf') – no need to include any additional files or anything like that.
RockPdf makes debugging layout issues notably easier by embedding parameters passed to its functions as HTML comments into the generated markup, and also by giving you an easy method to fetch the generated markup as-is.
While mpdf by default includes a massive blob of fonts, with RockPdf you get only the ones you really need – and you can still add more if needed, just by dropping the font files into specific directory.
Perhaps the most notable benefit from using RockPdf instead of directly including mpdf is related to the first bullet point above: by using RockPdf you get to keep your template and module files clean, and the API usage is always consistent. As an added bonus you also don't have to worry about keeping the mpdf library manually up to date.
This module's files should be placed in /site/modules/RockPdf/
How to install or uninstall modules
You can do anything that is possible with Mpdf using the mpdf instance:
$pdf = $modules->get('RockPdf'); $mpdf = $pdf->mpdf;
See the mpdf docs: https://mpdf.github.io
Using mpdf directly can be fine, but this module ships with some helpers/shortcuts that make working with mpdf and processwire easier. One example is debugging. When using Mpdf debugging can be tedious because you might get some unwanted results in your pdf and you don't know what's causing this problem. Getting only the generated HTML is also not possible by default and you would need to implement some other output strategy like shown here: https://mpdf.github.io/getting-started/html-or-php.html;
My solution is simple: There are some custom proxy functions that forward your instructions to Mpdf and log this request as HTML comment. You can then output the generated HTML without generating the PDF (wich is quicker and a lot easier to debug - think of wrong filepaths for example). You can even use the tracy console to check your code:
$pdf = $modules->get('RockPdf'); $pdf->set('SetHeader', 'This is my header text'); $pdf->write('Hello World ' . date('H:i:s')); $pdf->write('<!-- my custom comment -->'); d($pdf->html()); // output html in tracy console d($pdf->save()); // generate pdf
You can use these methods to output your pdf files:
- save() to save your file to the file system
- show() to directly show your file in the browser
- download() to force the browser to download the pdf
By default MPdf ships with a lot of fonts making the module over 80MB large. I removed almost all of them and you can place the fonts you need in your sites assets folder
/site/assets/RockPdf/fonts. See https://mpdf.github.io/fonts-languages/fonts-in-mpdf-7-x.html
$pdf = $modules->get('RockPdf'); $pdf->settings([ 'fontdata' => (new Mpdf\Config\FontVariables())->getDefaults()['fontdata'] + [ 'test' => [ 'R' => 'Garuda.ttf', 'I' => 'Garuda.ttf', ] ], ]); $mpdf = $pdf->mpdf; $mpdf->WriteHTML('Hello World ' . date('H:i:s')); $mpdf->WriteHTML('<p style="font-family: test;">Hello World ' . date('H:i:s') . '</p>'); $pdf->save();
Here the code to copy&paste
$pdf = modules('RockPdf'); $mpdf = $pdf->mpdf; // needs to be set before any output! $mpdf->SetImportUse(); $mpdf->SetDocTemplate(config()->paths->assets . 'RockCRM/invoicebackgrounds/background.pdf'); $pdf->write('hello world ' . date('Ymd')); d($pdf->save()->path);
- Admin Helpers
- Core Modules
- Development Tools
- Field Types
- Input Fields
- Language Modules
- Language Packs
- Markup Generation
- Premium Modules
- Process Modules
- Proof of Concept
- Site Profiles
- Social, Feeds, Services
- Text Formatters
- Other Modules
- Users and Access
*Use modules at your own risk. There are no guarantees or warranties. It is recommended that you backup your site and database before installing new modules.