Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions

How to run HTML2PDF on a generated page


Reply

4 Posts   1104 Views

Avatar
MartinPhone

16 September 2013 at 10:42am Community Member, 57 Posts

I'm a little confused as to where I should call HTML2PDF from.

I followed[url=http://www.ibcscorp.com/work/how-to-html-to-pdf-with-silverstripe/] this advice [/url] and HTML2PDF is working but I'm having trouble getting my head around how I can link to this.

I have made myself a non-page class that pulls the contents of a particular page and formats it in the way I want it (it's a big boring table of the contents of that page). This is fine but I can't work out how to combine the two, ie create a PDF of this HTML file that is generated with a whole load of children and other controls.

It is like I need to generate the HTML first, and then pass all that to my getPDFTimeTable(args) function (that just runs HTML2PDF on the content passed to it in the (args)). I'm just not sure how to do that. What am I missing?

Avatar
swaiba

16 September 2013 at 2:57pm Forum Moderator, 1796 Posts

Having tried many libraries for this I can fully recomend http://www.tcpdf.org/
Full set of examples http://www.tcpdf.org/examples.php
No external dependencies, no need for java and converts mostly all the html fine for me without having to fiddle with it

Avatar
MartinPhone

28 October 2013 at 5:29pm (Last edited: 28 October 2013 5:41pm), Community Member, 57 Posts

Thanks for the reply, sorry for the delay.

I think my problem lies elsewhere - more to do with the basic problem of how to send my HTML to the function. ie how to put what is generated by my .ss file into a variable to pass to the function...

So I have my :    

<table ID="TimeTable">
<caption>$Title</caption>
<% control TimeTable %>
<tr><th colspan='3' class='TimeTableDay'>Mondays</th></tr>
<% control AllChildren %>
<% if Day == Mondays %>
<tr><td>$MenuTitle</td><td>$Time</td><td>$contactName: $contactTelephone</td></tr>
<% end_if %>
<% end_control %>
<% end_control %>
</table>

I can't put getPDFTimeTable( ) around the code above but I don't know what to do so I get all the html into a variable that I can send to the function. Am I being totally stupid?!

Avatar
Devlin

2 November 2013 at 9:14am Community Member, 215 Posts

how to put what is generated by my .ss file into a variable

see:
http://doc.silverstripe.org/framework/en/reference/templates#calling-templates-from-php-code

// parse html
$html = $this->customise($myDataObj)->renderWith($myTmpl);

// create pdf
$pdf = new DOMPDF();
$pdf->set_base_path( BASE_PATH );
$pdf->load_html($html);
$pdf->render();
file_put_contents($folder.$filename, $pdf->output());