2

最近安装的 wkhtmltopdf。试图以当前状态捕获整个页面,但是,下面的方法似乎导航到该页面的初始状态,而没有用户输入的所有输入字段。

PHP

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

我想知道是否有人知道wkhtmltopdf捕获页面当前状态的实现,包括在文本字段中输入的任何文本?

我很感激任何建议。

提前谢谢了!

4

1 回答 1

4

wkhtmltopdf 独立于您当前的浏览会话访问页面。如果你这样点击它,你会得到任何人第一次访问你的页面时会看到的内容。可能您想要做的是使用输出缓冲区保存当前页面,然后在保存的页面上运行 wkhtmltopdf。这是一些示例代码:

子.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");
?>

模板.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>

从长远来看,您可能应该有一些两个页面都会使用的基本模板,并且一个在您的输入字段中有一些标记,例如 value='%valueOfThisVariable%' ,当您将字段呈现给用户时,您可以将其替换为空白,并在创建要写入 pdf 的页面时填写用户数据。现在它只是通过并将所有 name='this_name' 替换为 value='this_name->value'。

于 2013-04-13T00:57:46.820 回答