Question

I'm php newbie. I'll try to explain my needs.

I would like to fill few forms in php that would be placed at appropriate locations in the document and then generate pdf file.

I'm using mPDF atm. because I need polish signs to show up correctly.

If you could help me anyhow I would appreciate that a lot. Peace.

include('mpdf/mpdf.php');
$html = file_get_contents("generated.html");
$mpdf=new mPDF('iso-8859-2');
$mpdf->allow_charset_conversion=true;
$mpdf->charset_in='ISO-8859-2';
$mpdf->WriteHTML($html);
$mpdf->Output("my_file.pdf","F");
exit;

That's for generating pdf from html, but now I would like to somehow generate the html with forms that I could define first, and then generate pdf.

So far I'm thinking about form for generating html like:

<?php
$test1 = $_POST['test1'];
$test2 = $_POST['test2'];
$test3 = $_POST['test3'];
$data = "$test1 | $test2 | $test3\n";
$fh = fopen("generated.html", "a");
fwrite($fh, $data);
fclose($fh);
print "Finished";
?>

<body>
<form name="form1" method="post" action="generate_html.php">
 test1:
 <input type="text" name="test1">   
 <br>
 test2:
 <input type="text" name="test2">   
 <br>   
 test3:
 <select name="test3">
 <option value="test3_1">test3_1</option>
 <option value="test3_2">test3_2</option>
 <option value="test3_3">test3_3</option>
 </select>  
 <br>   
 <input type="submit" name="Submit" value="Generate">
</form>
</body>

EDIT Thanks to @Gavin

<?php
include('mpdf/mpdf.php');
$html = file_get_contents("2.html");
$html = str_replace("%title_placeholder%", $_POST['title'], $html);
$mpdf=new mPDF('iso-8859-2');
$mpdf->allow_charset_conversion=true;
$mpdf->charset_in='ISO-8859-2';
$mpdf->WriteHTML($html);
$mpdf->Output("3.pdf","F");
?>
<body>
<form name="form1" method="post" action="1.php">
Title: <input type="text" name="title">
<br>
<input type="submit" name="Submit" value="Generate">
</form>
</body>

Works great,thanks!

Was it helpful?

Solution

You are loading the contents of a static HTML file so you could put place holders within the html...

<h1>%title_placeholder%</h1>

and then use file get_contents

$html = file_get_contents("my_file.html");

and replace the placeholders with your form data

$html = str_replace("%title_placeholder%", $_POST['title'], $html);

then write your new string to mPDF

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top