php variables

816 views
Skip to first unread message

petrakid

unread,
May 26, 2009, 10:49:19 PM5/26/09
to dompdf
I am trying to figure this very cool piece of scripting out. So far,
here are my problems:

1) When I attempt to create a very basic pdf and try to open it, I get
an error stating that the pdf is corrupt or not encoded correctly.

2) HOW do I pass php variables into the pdf??

I am using this easy to follow code...

<?php
require_once("dompdf/dompdf_config.inc.php");
$html =
'<h1 align="center">test</h>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("work_order.pdf");
}
?>

...but I have not been able to get a simple variable to pass. All I
get are parse errors. I am SO used to ?>'ing out of php and using
pure html when creating pages, but I don't know what I'm supposed to
do after the $html = in this regard. Do I leave php? Do I stay in
php and type everything in as "echo" or "print"?? There is nothing on
the dompdf website about variables to speak of. I really need this to
work!

Thanks

Ryan Masten

unread,
May 27, 2009, 9:46:19 AM5/27/09
to dom...@googlegroups.com
Sorry you are having issues.

1) Can you show us the code that is not working? Is it the code below?
2) You can insert PHP variables into the HTML as you wish. I do this all the time. The way I do it is to use template'ish type strings.

For example: <h1 align="center">{{{my_header}}}</h1>

I then load the HTML into a variable and do a string replace to replace {{{my_header}}} with the PHP variable that holds the value that I need. This allows me to keep my html in .html templates.

--
Ryan H. Masten
ryan....@gmail.com
www.ryanmasten.com

Andy

unread,
May 27, 2009, 12:56:02 PM5/27/09
to dompdf
Try this:

<?php
require_once("dompdf/dompdf_config.inc.php");

$test = "This is a test string inside of another String";
$test2 = "This is how you break out of a string to insert another
string."; // Works great for $_GET and $_POST. The . (period)
concatinates the strings.
$html =
"<h1 align='center'>$test</h1><p>" . $test2 . "</p>"; // Important to
note, you have to use the double quotes if you want to insert a string
inside of another string. single quotes will make it display $test
instead of the value of $test.
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("work_order.pdf");
}

?>

Reply all
Reply to author
Forward
0 new messages