简体   繁体   English

wkhtmltopdf将html页面的当前状态转换为pdf?

[英]wkhtmltopdf converting the current state of html page to pdf?

Html to pdf is happening on local server through php and offering the html file as pdf for download is working fine, the html page I'm passing to wkhtmltopdf is a filled with user input then I need to convert it into pdf but it converts the blank form_1.html which I've placed in my web server directory. HTML转换为pdf的过程是通过php在本地服务器上进行的,并且提供html文件作为pdf进行下载可以正常工作,我传递给wkhtmltopdf的html页面充满了用户输入,然后我需要将其转换为pdf,但是它将转换为pdf我已将其放在Web服务器目录中的空白form_1.html。

How can i get pdf of currently open html page of named form_1.htm. 我如何获取名为form_1.htm的当前打开的html页面的pdf。

Is there anything HTTP_REFERER i need to use here. 我在这里需要使用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>

You won't be able to capture the information the user has entered in the form by printing that way. 通过这种方式打印,您将无法捕获用户在表单中输入的信息。

To generate a PDF of it you would need to save the data to the server, then get wkhtmltopdf to request a page which had the form filled with the details from the form. 要生成它的PDF,您需要将数据保存到服务器,然后获取wkhtmltopdf来请求页面,该页面的表单中填充有表单中的详细信息。

The reason for this is due to the stateless nature of HTTP which is used to serve the HTML page. 其原因是由于用于HTML页面的HTTP的无状态性质。

When the user request form_1.html they get sent the HTML page, they fill in the form details, but all that information is stored locally on their computer. 当用户请求form_1.html时,他们将被发送到HTML页面,他们将填写表单详细信息,但是所有这些信息都存储在本地计算机上。

If someone else request form_1.html they get sent another copy of the HTML page, without any of the details, as the web server as yet knows nothing of the details the first user entered. 如果其他人请求form_1.html,他们将被发送HTML页面的另一份副本,但没有任何详细信息,因为Web服务器尚不了解第一个用户输入的详细信息。

The server only finds out the content of the first users form when the user submits the form to the server. 服务器仅在用户向服务器提交表单时才查找第一个用户表单的内容。 The server application can then decide what to do with it. 然后,服务器应用程序可以决定如何处理它。

If you consider that wkhtmltopdf works just like another user with a web browser you'll see why they only ever receive the form. 如果您认为wkhtmltopdf就像使用网络浏览器的另一个用户一样工作,您将看到为什么他们只收到表格。

If you want to go the wkhtmlpdf route then you will need to 如果您想走wkhtmlpdf路线,则需要

  1. Save the submitted data locally on the server 将提交的数据本地保存在服务器上
  2. Set up a second HTML page that can be requested with the ability to load that specific date 设置第二个HTML页面,可以请求该页面并加载该特定日期
  3. set wkhtmltopdf up to request that web page 设置wkhtmltopdf以请求该网页
  4. send the resultant file to the user 将结果文件发送给用户

If you want to send the USER a PDF version of what they enter into the form without saving it a better approach may be to process the submitted form data using PHP and to use a server side PDF generation tool, such as http://www.tcpdf.org or http://www.fpdf.org/ , to create a PDF and send it to the user. 如果要向用户发送他们在表单中输入的内容的PDF版本而不保存它,则更好的方法可能是使用PHP处理提交的表单数据并使用服务器端PDF生成工具,例如http:// www .tcpdf.orghttp://www.fpdf.org/ ,以创建PDF并将其发送给用户。 It wouldn't look like their form, but it would produce a PDF for them. 它看起来不像它们的形式,但是会为它们生成PDF。

EDIT: more detail on an approach to do this via wkhtmltopdf (note this is not very secure) 编辑:通过wkhtmltopdf进行此操作的方法的更多详细信息(请注意,这不是很安全)

Given this html form (form_1.html) 给定此html表单(form_1.html)

<html>
<body>
<form method="post" action="respond.php">
   <input type="text" name="fieldname">
   <input type="submit">
</form>

In your php that is responding to the form post, let's call it 'respond.php' 在响应表单发布的php中,我们将其称为“ respond.php”

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

in get.php (which needs to be accessible via a web server) 在get.php中(需要通过网络服务器进行访问)

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

The key thing being you need to get wkhtmltopdf to receive an HTML page which has the form fields filled out by the server. 关键是您需要获取wkhtmltopdf才能接收HTML页面,该页面具有服务器填写的表单字段。 So when wkhtmltopdf requests http://localhost/get.php?filename='+$filename+' it receives and HTML page from your localhost which has all of the form details filled out on it. 因此,当wkhtmltopdf请求http://localhost/get.php?filename='+$filename+'它将从您的localhost接收HTML页面,该页面上填写了所有表单详细信息。

Please note that this is a conceptual solution, to illustrate how you use it. 请注意,这是一个概念性的解决方案,以说明您的用法。 I wouldn't recommend this in production, as it relies on security through obscurity 我不建议在生产中使用它,因为它依赖于默默无闻的安全性

You have to pass all the form parameters and regenerate the form before you pass it to wkhtmltopdf, like DorianFM describes (+1). 您必须先传递所有表单参数并重新生成表单,然后再将其传递给wkhtmltopdf,例如DorianFM描述的(+1)。

Another option may be to use a service, that does this for you like http://www.pdfmyform.com 另一种选择是使用服务,例如http://www.pdfmyform.com。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM