-4

我是php新手。我会尽力解释我的需求。

我想在 php 中填写一些表格,这些表格将放置在文档中的适当位置,然后生成 pdf 文件。

我正在使用 mPDF atm。因为我需要波兰语标志才能正确显示。

如果你能帮助我,我将不胜感激。和平。

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;

那是为了从html生成pdf,但现在我想以某种方式生成带有我可以先定义的表单的html,然后生成pdf。

到目前为止,我正在考虑生成 html 的表单,例如:

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

编辑 感谢@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>

效果很好,谢谢!

4

1 回答 1

0

您正在加载静态 HTML 文件的内容,因此您可以在 html 中放置占位符...

<h1>%title_placeholder%</h1>

然后使用文件 get_contents

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

并用您的表单数据替换占位符

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

然后将您的新字符串写入 mPDF

于 2013-11-04T21:49:06.947 回答