best way to populate PDF with form data?

725 views
Skip to first unread message

Ian Sebryk

unread,
Oct 26, 2011, 4:18:16 PM10/26/11
to dompdf
hey guys:

this is what i think will work to get the results i want, but i'm
missing a critical bit:

have a form. it pushes results into $_POST and hits buildPDF.php
which has all the markup i need for the document. i populate the
document's variables via PHP like so: <?php echo $_POST['varName']; ?>

now i get lost. :( i do not know how to get from this stage with the
'completed document' to the part where i pass it to dompdf.php and
then on to emailer.

can anyone lend a hand? it's always this last minute 'i-sawt-this-
thing-on-the-web' stuff from the client that would normally be fun to
learn, but isn't because it has to be done yesterday. :P

and thanks in advance. this class rocks the kasbah!

WR!

psychoactive

unread,
Oct 26, 2011, 4:59:09 PM10/26/11
to dompdf
Hello buddy, ok so what you need to do is create an HTML document with
all of your form data in it. Then what you need to do is pass that
HTML code into dompdf, and it will output a pdf. Here is a little
sample to do that with. Just store your HTML into a variable instead
of displaying it to the user. Check out this code...

require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf -> load_html($html);
$dompdf->render();
$pdfoutput = $dompdf->output();
file_put_contents('test.pdf',$pdfoutput);

psychoactive

unread,
Oct 26, 2011, 5:04:42 PM10/26/11
to dompdf
And just in case you are not sure of the step before this, you would
do something like...

$html = "<html><table><tr><td>Here is my var: ".echo
$_POST['varName']."</td></tr></table></html>";

and then use this $html variable in the code I just posted.

Ian Sebryk

unread,
Oct 26, 2011, 5:10:08 PM10/26/11
to dompdf
i understand the concept, but i'm fuzzy on the mechanics of it.
sorry. :P

what i'm fuzzy on is how i push the entire HTML page into the
variable. maybe more info on the application of this process: client
enters data into a form. form populates 8 (yup, eight) pages of HTML
which is then supposed to crank out a PDF that client can save to
desktop while a copy shows up in their email. so on paper, the flow
looks like this to me:

form ---> PHP generated HTML ---> dompdf.php ---> PDF file.

based on what i see in your response, you're saying i should SAVE the
generated HTML to a variable (...??? no sure how...) and THEN load it
into dompdf? (sounds almost like a place for AJAX...)

i dunno. maybe i'm just over-thinking it. looong day. :P

and thanks, @psychoactive. very, very much appreciated. :)

WR!

psychoactive

unread,
Oct 26, 2011, 5:13:11 PM10/26/11
to dompdf
Yes you have the idea. That 8 pages of HTML code, you just store it
into a variable, then load it into dompdf.

Ian Sebryk

unread,
Oct 26, 2011, 5:20:45 PM10/26/11
to dompdf
okay! i'll get cracking and see what pops out the other end! :D

thank you very much. you may not hear the results until tomorrow.
we're about to wrap up for the day. 14hrs is long enough,ya? :D

so nice to find a forum that actually get's questions answered!! :)

WR!

psychoactive

unread,
Oct 26, 2011, 5:24:12 PM10/26/11
to dompdf
Sure! Please write back if you have any other questions at all. A lot
of people here have been using this and developing it for a long time
and will answer any questions you have. Also write if it works!

Yes 14 hours is long enough for one day haha!

Ian Sebryk

unread,
Oct 27, 2011, 5:59:21 PM10/27/11
to dompdf
okay. here's what i've constructed. and i'm getting an error from
the PDF reader: "...could not open because it is either not a
supported file type or because the file has been damaged."

this is the code that the 'submit' button loads from the form that
populates the $_SESSION variables. i set them here for convenience in
testing.

<?php
session_start();
$_SESSION['someTitle'] = "BIG FAT TITLE";
$_SESSION['someName'] = "HandomeLu";

echo "someTitle: " . $_SESSION['someTitle'];
echo "someName: " . $_SESSION['someName'];

$theTemplate = 'template.php';

function renderToPDF($theTemplate)
{
require_once("_dox/dompdf/dompdf_config.inc.php");
ob_start();
include $theTemplate;
$contents = ob_get_clean();

if ($contents !== false)
{
$dompdf = new DOMPDF();
$dompdf->load_html($contents);
$dompdf->render();
$dompdf->stream("kapow_ItWorks.pdf");
}
}

renderToPDF();

?>

this is the code from the 'template.php' file that get's passed to the
function:

<!DOCTYPE HTML>
<html>
<meta>
<head>
<meta charset="utf-8">
<link href="thisPage.css" type="text/css" rel="stylesheet">
</head>
<body>
<h1><?php echo $_SESSION['someTitle'] ?></h1>
<p>wouldn't it be nice, <?php echo $_SESSION['someName'] ?></p>
</body>
</html>

okay. so what the heck am i doing wrong? it seems like i'm sooooo
close... blargh!! :)
and thanks so much for your help.

WR!

Ian Sebryk

unread,
Oct 27, 2011, 6:04:56 PM10/27/11
to dompdf
i should probably note that i'm using the latest-greatest incarnation:
6.0 beta 2.

WR!

psychoactive

unread,
Oct 27, 2011, 6:08:24 PM10/27/11
to dompdf
hello!

just for a test, instead of include $theTemplate can you try something
like echo 'test';

just to see if it produces a test pdf then you can see if the error is
in the template or in the function?

psychoactive

unread,
Oct 27, 2011, 6:17:57 PM10/27/11
to dompdf
I am sorry, I was not thinking clearly!! This is a common issue.

In your template.php file please make sure there is NOTHING!!!!!!!!
after the closing php tag. No spaces, no carriage returns, NOTHING!!
Then it should work.

Ian Sebryk

unread,
Oct 27, 2011, 7:03:22 PM10/27/11
to dompdf
not a problem. i actually WAS getting readable PDF output (yay!) BUT
it wasn't parsing the variables in template.php, thus the ob_start();
section.

in the template.php file, there is no 'closing php tag'. all the code
posted above is ALL the code. (but i will keep it in mind for the
final release as it will have footers included.)

to recap, this is THE ENTIRE file:

<!DOCTYPE HTML>
<html>
<meta>
<head>
<meta charset="utf-8">
<link href="thisPage.css" type="text/css" rel="stylesheet">
</head>
<body>
<h1><?php echo $_SESSION['someTitle'] ?></h1>
<p>wouldn't it be nice, <?php echo $_SESSION['someName'] ?></
p>
</body>
</html>

and i checked, there is nothing at the end of the file after the
closing </html> tag. :P nuts. was hoping that was it.

WR!

ps: off to grab a bite. be back in an hour or so. another long day,
but gotta get this nailed down. :D you're helping more than you
know. thanks.

BrianS

unread,
Oct 27, 2011, 11:30:15 PM10/27/11
to dom...@googlegroups.com
The problem you're experiencing with an unreadable PDF is typically due to some non-PDF content making its way into the document. The most common cause is output buffering. Warnings/notices get cause in the stream and can usually be found at the top of the PDF if you open it with a normal text editor.

You could try clearing the buffer prior to passing the HTML to dompdf.

Ian Sebryk

unread,
Oct 28, 2011, 9:50:06 AM10/28/11
to dompdf
i took a look @ the PDF in a text editor... not sure what i'm looking
for there. you can get it here: http://hotfile.com/dl/133557830/59aa5cc/fileOut.pdf.html

i didn't see any errors in the text editor, i also tried chopping some
stuff out of there to see what would need to come out to make it
valid... no luck. nothing i did seemed to work. :(

how do i 'clean' the output buffer?

WR!

Ian Sebryk

unread,
Oct 28, 2011, 9:51:57 AM10/28/11
to dompdf
whoops! wrong file. :P go here insted:

http://hotfile.com/dl/133557976/d539289/kapow_ItWorks.pdf.html

sorry!

WR!

BrianS

unread,
Oct 28, 2011, 10:30:59 AM10/28/11
to dom...@googlegroups.com
It's as I suspected. To recap, drop the file into your favorite text editor (Notepad, TextEdit, vi, etc.) and take a look. At the top of the file you'll see a bunch of HTML that should not be there. The actual PDF starts with the characters "%PDF-1.3" ... everything before that should not be there. This is the error:

Warning: Missing argument 1 for renderToPDF(), called in ...\WORD_TEST.php on line 40 and defined in ...\WORD_TEST.php on line 24

I'm not sure where buffering is enabled that's capturing this error because it occurs before you turn it on in the code. Looking at your code above, you are missing the argument specified. Here's a rewrite that addresses the error and attempts to disable buffering (changes highlighted):

<?php
session_start();
$_SESSION['someTitle'] = "BIG FAT TITLE";
$_SESSION['someName'] = "HandomeLu";

// echo "someTitle: " . $_SESSION['someTitle'];
// echo "someName: " . $_SESSION['someName'];


$theTemplate = 'template.php';

function renderToPDF($theTemplate) {
  require_once("_dox/dompdf/dompdf_config.inc.php");
  ob_start();
  include $theTemplate;
  $contents = ob_get_clean();

  if ($contents !== false) {
    ob_end_clean();

    $dompdf = new DOMPDF();
    $dompdf->load_html($contents);
    $dompdf->render();
    $dompdf->stream("kapow_ItWorks.pdf");
  }
}

renderToPDF($theTemplate);

?>

Ian Sebryk

unread,
Oct 28, 2011, 10:32:13 AM10/28/11
to dompdf
before you tell me about the HTML above the %PDF1.3 line, i saw that
and tried taking it out. it only returned a 'file damaged or
corrupted and cannot be repaired' error. so i left it in since i
wasn't sure what else was making it fail. this is frustrating. so
close, but so far. i don't know how to clean the HTML code out before
it goes to the dompdf, if that is what actually needs doing; which it
seems like.

argh! i feel like an idiot!

WR!

BrianS

unread,
Oct 28, 2011, 10:36:03 AM10/28/11
to dom...@googlegroups.com
You shouldn't need to clean out the HTML. Under normal circumstances it won't be there. But content buffering can cause this type of problem to occur. If you make the few modifications I specified the PDF should be ok. If not, post the resulting PDF and we can figure out the next problem.

Ian Sebryk

unread,
Oct 28, 2011, 6:09:27 PM10/28/11
to dompdf
@Brian: i did exactly what you suggested. ...and screwed it up
myself. i had a tracking comment and it somehow wound up in the
header of the PDF file BEFORE the %PDF1.3. i now understand why,
which took me all day to see it. and when i did...i wanted to choke
myself.

anyway. thank you all so much for pointing me in the right direxion
and feeding me enough to get me where i needed to be.

this class is the best damn thing i've ever seen for generating PDFs.
simple and clean and, once head is screwed on right, easy. i'd like
to contribute to better dox, if possible. make it easier to dive in
for noobs. :) [cuz i'm such a dab-hand now... pffft.]

thanks everyone!

WR!

BrianS

unread,
Oct 28, 2011, 8:56:32 PM10/28/11
to dom...@googlegroups.com
Glad you were able to get there. That's what the forums are for.

If you have any suggestions for the documentation let us know. We plan to spend some time overhauling them for the final 0.6.0 release.
Reply all
Reply to author
Forward
0 new messages