0

I need a little help converting a html page into a PDF document using html2pdf

Their site gives a small example

$content = "
<page>
<h1>Exemple d'utilisation</h1>
<br>
Ceci est un <b>exemple d'utilisation</b>
de <a href='http://html2pdf.fr/'>HTML2PDF</a>.<br>
</page>";

require_once(dirname(__FILE__).'/html2pdf/html2pdf.class.php');
$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');`

What I dont know how to do most effectively is make my html page that has a few PHP variables in it into a variable I can use here $html2pdf->WriteHTML($content);

is there a way to pull it from its own html file and have the server replace the variables and then make it a variable?

JpaytonWPD
  • 485
  • 1
  • 7
  • 22
  • In the above example the pdf content is just coming from a variable. Are you saying that in your real world usage that `$content` will hold content from a live page somewhere loaded via `curl` or are you trying to load the actual html file from disk? ... or is this just a one off thing where you are trying to convert one single html page? – Mark Hayden Aug 23 '14 at 23:01
  • Take a look at: http://stackoverflow.com/a/171327/3610351 – Tyler Marien Aug 24 '14 at 01:03
  • if curl would work better. Its for a voucher generator. I have the voucher made to pull some variables from a DB based on the ID passed with GET so I need the content to be the output of the page – JpaytonWPD Aug 24 '14 at 15:30

1 Answers1

0

I found that curl was the best answer to my problem. By using CURL the server processes the page and outputs static HTML that works perfect for HTML2PDF

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://example.com/voucher.php?txnid=" .    $txnid);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'My App (http://pexample.com/voucher.php?txnid=' . $txnid . ')');
    $output = curl_exec($ch);
    curl_close($ch);

I can then use $output where I need it. Works exactly as I needed. Thanks!

JpaytonWPD
  • 485
  • 1
  • 7
  • 22