• twitter image
  • facebook image
  • youtube image
  • linkedin image
Language: CMS Made Simple Czech CMS Made Simple France CMS Made Simple Spain CMS Made Simple Hungary CMS Made Simple Russia CMS Made Simple Netherlands

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: [SOLVED] using mPDF in a page template
PostPosted: Tue Dec 09, 2014 2:40 pm 
Offline
Forum Members
Forum Members

Joined: Sat Aug 29, 2009 3:00 pm
Posts: 221
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:
{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.


Last edited by Guido on Wed Dec 10, 2014 2:15 pm, edited 1 time in total.

Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: using mPDF in a page template
PostPosted: Tue Dec 09, 2014 3:43 pm 
Offline
Dev Team Member
Dev Team Member

Joined: Mon Nov 28, 2011 9:29 am
Posts: 3100
Location: The Netherlands
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


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: using mPDF in a page template
PostPosted: Tue Dec 09, 2014 6:02 pm 
Offline
Forum Members
Forum Members

Joined: Sat Aug 29, 2009 3:00 pm
Posts: 221
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!


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: using mPDF in a page template
PostPosted: Tue Dec 09, 2014 6:11 pm 
Offline
Dev Team Member
Dev Team Member
User avatar

Joined: Mon Jan 29, 2007 4:47 pm
Posts: 1746
On the very top of the UDT use:
Code:
if( isset($CMS_ADMIN_PAGE) ) return
It should prevent the UDT to run further in case it's not a frontend request.

_________________
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).


Code of Conduit | CMSMS Docs
Help Support CMSMS
My developer Page on the Forge | Yet another blog about CMSMS

GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!



Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: using mPDF in a page template
PostPosted: Tue Dec 09, 2014 7:04 pm 
Offline
Forum Members
Forum Members

Joined: Sat Aug 29, 2009 3:00 pm
Posts: 221
Thanks, good tip!


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: using mPDF in a page template
PostPosted: Tue Dec 09, 2014 8:18 pm 
Offline
Forum Members
Forum Members

Joined: Sat Aug 29, 2009 3:00 pm
Posts: 221
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.


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: using mPDF in a page template
PostPosted: Wed Dec 10, 2014 2:13 pm 
Offline
Forum Members
Forum Members

Joined: Sat Aug 29, 2009 3:00 pm
Posts: 221
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:
// 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:
// Include mPDF
include("mpdf/mpdf.php");

You can use a separate stylesheet using the following line (from the UDT above):
Code:
$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:
{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.


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
A2 Hosting