简体   繁体   English

使用wkhtmltopdf将当前页面打印为pdf

[英]Printing the current page to pdf using wkhtmltopdf

Recently installed wkhtmltopdf. 最近安装的wkhtmltopdf。 Was trying to capture the entire page in its current state, however, the below method seems to navigate to the initial state of that page without all the input fields that the user has entered. 试图捕获当前状态下的整个页面,但是,以下方法似乎导航到该页面的初始状态,而无需用户输入所有输入字段。

PHP PHP

shell_exec('wkhtmltopdf http://localhost/www/bolt/invoice.php invoice.pdf');

I was wondering if someone knew of an implementation of wkhtmltopdf that captures the current state of the page including any text entered in the text fields?? 我想知道是否有人知道wkhtmltopdf的实现,该实现捕获页面的当前状态,包括在文本字段中输入的任何文本?

I appreciate any suggestions. 我感谢任何建议。

Many thanks in advance! 提前谢谢了!

wkhtmltopdf hits the page independently of your current browsing session. wkhtmltopdf与您当前的浏览会话无关地点击页面。 If you hit it like that, you're going to get what anyone would see when they first go to your page. 如果您这样点击,您将获得任何人在首次访问您的页面时所看到的内容。 Probably what you want to do is save the current page using an output buffer, and then run wkhtmltopdf on the saved page. 可能要执行的操作是使用输出缓冲区保存当前页面,然后在保存的页面上运行wkhtmltopdf。 Here's some sample code: 这是一些示例代码:

sub.php sub.php

<?php
$documentTemplate = file_get_contents ("template.html");
foreach ($_POST as $key => $postVar)
{
    $documentTemplate = 
    preg_replace ("/name=\"$key\"/", "value=\"$postVar\"", $documentTemplate);
}
file_put_contents ("out.html", $documentTemplate);
shell_exec ("wkhtmltopdf out.html test.pdf");
?>

template.php 的template.php

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
</head>
<body>
    <h1>This is a page</h1>
    <form action="sub.php" method="post" accept-charset="utf-8">
        My Test Field:
        <input type="text" name="test_field" value="">
        <input type="submit" value = "submit">
    </form>
</body>
</html>

Probably in the long run you should have some kind of base template that both pages would use, and one have some markers like value='%valueOfThisVariable%' in your input fields that you can replace with blanks when you present the fields to the user, and fill with the user data when you create the page that you want to write to pdf. 从长远来看,也许您应该拥有两种页面都可以使用的基本模板,并且在输入字段中有一些诸如value ='%valueOfThisVariable%'之类的标记,当您向用户展示字段时可以将其替换为空白,并在创建要写入pdf的页面时填充用户数据。 Right now it's just going through and replacing all the name='this_name' with value='this_name->value'. 现在,它只是通过将所有name ='this_name'替换为value ='this_name-> value'。

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

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