Page 1 of 1

[SOLVED] using mPDF in a page template

Posted: Tue Dec 09, 2014 2:40 pm
by Guido
Hi,

I trying to accomplish the following:

I created a one-page site using the method in this post for a company document.

Now I'd like to use the mPDF library to output a PDF document of this page using '@media print {}' styles to style an actual document, TOC etc.

My idea was to create a separate template for this and a separate page, so you could create the PDF by going to this page.

I tried multiple things, like:

Code: Select all

{php}

ini_set("memory_limit","64M");

include("/path/to/mpdf/mpdf.php");

$mpdf=new mPDF(''); 

$html = '{/php}{menu template="scrollinhoud"}{php}';

';

$mpdf->h2toc = array('H3'=>0, 'H4'=>1);
$mpdf->h2bookmarks = array('H3'=>0, 'H4'=>1);

$mpdf->open_layer_pane = false;
$mpdf->layerDetails[1]['state']='hidden';	// Set initial state of layer - "hidden" or nothing
$mpdf->layerDetails[1]['name']='Correct Answers';
$mpdf->layerDetails[2]['state']='hidden';	// Set initial state of layer - "hidden" or nothing
$mpdf->layerDetails[2]['name']='Wrong Answers';

//==============================================================
if ($_REQUEST['html']) { echo $html; exit; }
if ($_REQUEST['source']) { 
	$file = __FILE__;
	header("Content-Type: text/plain");
	header("Content-Length: ". filesize($file));
	header("Content-Disposition: attachment; filename='".$file."'");
	readfile($file);
	exit; 
}

//==============================================================

$mpdf->WriteHTML($html);

// OUTPUT
$mpdf->Output(); exit;


//==============================================================
//==============================================================
//==============================================================
//==============================================================
{/php}
Using the {menu} outside of the {php} tags to parse it correctly. This doesn't work, as I later found out you can't use the {php} tags anymore.

I also tried creating a UDT, but saving a template that contains this UDT will cause it to run and create an empty PDF.

Problem is, I have to do this from somewhere within the system, since the content is also all secured content by the FrontEndUsers module. So creating a separate file and calling that will give me a PDF of the login form, even when I'm logged in as a user.

Re: using mPDF in a page template

Posted: Tue Dec 09, 2014 3:43 pm
by velden
No experience but thinking of:

- Checking whether in admin or not to prevent creation of empty pdf
- running menu tag first and assign to variable. Then pass the value of variable to parameter of UDT

Re: using mPDF in a page template

Posted: Tue Dec 09, 2014 6:02 pm
by Guido
Thanks,

Those were my two directions also. I would prefer the first one. mPDF has quite a clean option where a couple of lines of PHP from a separate file can retrieve a URL and 'PDF' it, where you could even use separate CSS only for the mPDF engine. I just have to figure out how to tell FrontEndUsers that mPDF is one of the good guys. Any help in that direction would be greatly appreciated.

The second option won't work when you use the UDT in yout template, because saving the template would result in the the PDF itself. It might be possible to use the UDT in the page content of a separate 'mPDF' page?

I'll experiment some more, but anyone who has some ideas: you're welcome!

Re: using mPDF in a page template

Posted: Tue Dec 09, 2014 6:11 pm
by Jo Morg
On the very top of the UDT use:

Code: Select all

if( isset($CMS_ADMIN_PAGE) ) return; 
It should prevent the UDT to run further in case it's not a frontend request.

Re: using mPDF in a page template

Posted: Tue Dec 09, 2014 7:04 pm
by Guido
Thanks, good tip!

Re: using mPDF in a page template

Posted: Tue Dec 09, 2014 8:18 pm
by Guido
I had found this module, but was afraid it might be too stale. I installed it and it works so far. I can use smarty logic in the PDF template and it's outputting the page.

Now to experiment with styling it.

Re: using mPDF in a page template

Posted: Wed Dec 10, 2014 2:13 pm
by Guido
I found the PDF Generator worked well as it was intended, but was too limited in it's styling options. In stead Cerulean helped me get on my way with a great UDT. What you need to do is:

Download mPDF and upload it to a folder on your server. Google 'mpdf' for the source and documentation.

Create the following UDT:

Code: Select all

// Check if required parameters exist
if( !isset($params["html"]) || !isset($params["name"]) ){
   return "Missing required parameters";
}

// Set the MIME type
cmsms()->variables["content-type"] = "application/pdf";

// Parameters from the UDT call
$name = $params["name"];
$html = $params["html"];

// Include mPDF
include("mpdf/mpdf.php");

// Make PDF
$mpdf = new mPDF(   '',   // mode - default ''
               '',   // format - A4, for example, default ''
               0,   // font size - default 0
               '',   // default font family
               0,   // margin_left
               0,   // margin right
               0,   // margin top
               0,   // margin bottom
               0,   // margin header
               0,   // margin footer
               'P');   // L - landscape, P - portrait

$stylesheet = file_get_contents("mpdf/css/stijl.css");
$mpdf->ignore_invalid_utf8 = true;
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html,2);
$mpdf->Output($name . ".pdf", "D");
Make sure this points to your mpdf.php location (from the UDT above):

Code: Select all

// Include mPDF
include("mpdf/mpdf.php");
You can use a separate stylesheet using the following line (from the UDT above):

Code: Select all

$stylesheet = file_get_contents("mpdf/css/stijl.css");
To separately style your PDF content. Make sure you check the mPDF docs on what it'll take CSS-wise.

Let's say you call this 'mPDF'. Now you create a new page, turn off the WYSIWYG, turn off 'available in menu' and create some content. In my case I used a modified menu-template to create a one-page site/document, so I called this in the page content like so:

Code: Select all

{capture assign="html"}{menu template="TOC"}{menu template="scrollinhoud"}{/capture} {mPDF name="filename" html=$html}
So first you need to capture everything you want to send to a PDF, (you could also use 'assign' if you make only one smarty call) and send it to a variable. In this case I used {$html}. After that you call the UDT and use the 'html' parameter to send the content to mPDF (which is in this case also named 'html'), and you use the 'name' option to predefine your filename.

Last but not least, you need to point your browser to the address of you 'PDF-page' you just created, which will fire the code. Of course you could also make a link to this page somewhere else on your site that serves as a 'download as PDF' link.