Once again I have been busy beavering away with Laravel. I have recently been working with the snappy plugin to create PDF files direct from Laravel pages. I found very limited information with regard to adding headers and footers to PDFs using snappy so thought I’d detail the requirements, our trials and tribulations and most importantly the code!
For this example I’m using some very simple code;
$pdfcontent = "</pre> <h1>This, believe it or not, is content</h1> <pre>"; $pdfname = "PikemereTest.pdf"; $snappy = App::make('snappy.pdf'); $snappy->setOption('page-width', 200); $snappy->setOption('page-height', 287); $snappy->generateFromHtml($pdfcontent , 'C:\xampp\htdocs\pikemere\public\\'.$pdfname);
The code above loads a bite sized chunk of HTML into our $pdfcontent variable and starts the creation of the PDF file and outputs the file to our laravel public folder. To this we need to add the code to put our header into place, shown by the code below, which can easily be added to the existing SetOptions.
That option will then add your HTML header in place. One very important note here is that the HTML file that you stipulate must have its DOCTYPE set at the start of the file.
To add a footer you can simply add the following code to your setOptions
That really is it! Just make sure you have your DOCTYPE in place and all should work perfectly.