Html to pdf正在本地服务器上通过php发生并提供html文件作为pdf供下载工作正常,我传递给wkhtmltopdf的html页面是一个填充用户输入然后我需要转换 它转换为pdf但它转换了我放在我的Web服务器目录中的空白form_1.html。
如何获取当前打开的名为form_1.htm的html页面的pdf。
我需要在这里使用HTTP_REFERER。
<?php
//Passing form_1.htm page prints the black form html page I want to print it after it opens in browser with filled user's input.
$result = shell_exec('"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe" form_1.htm vish.pdf 2>> err3.txt 1>> out3.txt');
echo $result;
$file = "vish.pdf";
$pdf = file_get_contents("vish.pdf");
header('Content-Type: application/pdf');
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Length: '.strlen($pdf));
header('Content-Disposition: inline; filename="'.basename($file).'";');
ob_clean();
flush();
echo $pdf;
?>
<html>
<head>
</head>
<body>
<p>Karna' wife!</p>
</body>
</html>
答案 0 :(得分:2)
您无法通过打印方式捕获用户在表单中输入的信息。
要生成PDF,您需要将数据保存到服务器,然后获取wkhtmltopdf以请求页面填写表单中的详细信息。
之所以这样,是因为用于提供HTML页面的HTTP的无状态特性。
当用户请求form_1.html发送HTML页面时,他们会填写表单详细信息,但所有这些信息都会本地存储在他们的计算机上。
如果其他人请求form_1.html他们会收到另一份HTML页面副本,而没有任何详细信息,因为Web服务器还不知道第一个用户输入的详细信息。
当用户将表单提交给服务器时,服务器仅查找第一个用户表单的内容。然后,服务器应用程序可以决定如何处理它。
如果您认为wkhtmltopdf的工作方式与使用网络浏览器的其他用户一样,您就会明白为什么他们只收到该表单。
如果你想去wkhtmlpdf路线那么你需要
如果您想向USER发送他们输入表单的PDF版本而不保存它,更好的方法可能是使用PHP处理提交的表单数据并使用服务器端PDF生成工具,例如{{ 3}}或http://www.tcpdf.org,创建PDF并将其发送给用户。它看起来不像是他们的形式,但它会为他们制作PDF。
编辑:有关通过wkhtmltopdf执行此操作的方法的更多详细信息(请注意,这不是很安全)
给出这个html表单(form_1.html)
<html>
<body>
<form method="post" action="respond.php">
<input type="text" name="fieldname">
<input type="submit">
</form>
在回复表单帖子的php中,让我们称之为“response.php&#39;
<?php
$form_values = $_POST;
// you should really validate $form_values
$contents = serialize($form_values);
$filename = sha1($contents);
file_put_contents($filename,$contents);
$result = shell_exec('"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe" http://localhost/get.php?filename='+$filename+' vish.pdf 2>> err3.txt 1>> out3.txt');
echo $result;
$file = "vish.pdf";
$pdf = file_get_contents("vish.pdf");
header('Content-Type: application/pdf');
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Length: '.strlen($pdf));
header('Content-Disposition: inline; filename="'.basename($file).'";');
ob_clean();
flush();
echo $pdf;
exit()
get.php中的(需要通过Web服务器访问)
<?php
$filename = $_GET['filename']
// you should really validate $filename
$contents = file_get_contents($filename);
$form_values = unserialize($contents);
?>
<html>
<body>
<form>
<input type="text" name="fieldname" value="<?php print $form_values['fieldname'] ;?>">
</form>
</body>
</html>
关键是你需要让wkhtmltopdf接收一个HTML页面,其中包含由服务器填写的表单字段。因此,当wkhtmltopdf请求http://localhost/get.php?filename='+$filename+'
时,它会从您的localhost接收到HTML页面,其中填写了所有表单详细信息。
请注意,这是一个概念性解决方案,用于说明您如何使用它。我不会在生产中推荐这个,因为它依赖于安全隐患
答案 1 :(得分:0)
你需要传递所有表单参数并重新生成表单,然后再将其传递给wkhtmltopdf,就像DorianFM描述的那样(+1)。
另一种选择可能是使用服务,为您执行此操作,如http://www.pdfmyform.com