使用 wkhtmltopdf 将当前页面打印为 pdf [英] Printing the current page to pdf using wkhtmltopdf

查看:43
本文介绍了使用 wkhtmltopdf 将当前页面打印为 pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Recently installed 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

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

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

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 与您当前的浏览会话无关地点击页面.如果您像这样点击它,您将获得任何人第一次访问您的页面时会看到的内容.可能您想要做的是使用输出缓冲区保存当前页面,然后在保存的页面上运行 wkhtmltopdf.下面是一些示例代码:

wkhtmltopdf hits the page independently of your current browsing session. 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. Here's some sample code:

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

模板.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 的页面时填写用户数据.现在它只是通过并用 value='this_name->value' 替换所有 name='this_name'.

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. Right now it's just going through and replacing all the name='this_name' with value='this_name->value'.

这篇关于使用 wkhtmltopdf 将当前页面打印为 pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆