Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » How to run HTML2PDF on a generated page

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 759 Views
  • MartinPhone
    Avatar
    Community Member
    51 Posts

    How to run HTML2PDF on a generated page Link to this post

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

    I followed this advice 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?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: How to run HTML2PDF on a generated page Link to this post

    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

  • MartinPhone
    Avatar
    Community Member
    51 Posts

    Re: How to run HTML2PDF on a generated page Link to this post

    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?!

  • Devlin
    Avatar
    Community Member
    186 Posts

    Re: How to run HTML2PDF on a generated page Link to this post

    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());

    759 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.