簡體   English   中英

使用wkhtmltopdf將當前頁面打印為pdf

[英]Printing the current page to pdf using wkhtmltopdf

最近安裝的wkhtmltopdf。 試圖捕獲當前狀態下的整個頁面,但是,以下方法似乎導航到該頁面的初始狀態,而無需用戶輸入所有輸入字段。

PHP

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

我想知道是否有人知道wkhtmltopdf的實現,該實現捕獲頁面的當前狀態,包括在文本字段中輸入的任何文本?

我感謝任何建議。

提前謝謝了!

wkhtmltopdf與您當前的瀏覽會話無關地點擊頁面。 如果您這樣點擊,您將獲得任何人在首次訪問您的頁面時所看到的內容。 可能要執行的操作是使用輸出緩沖區保存當前頁面,然后在保存的頁面上運行wkhtmltopdf。 這是一些示例代碼:

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

<!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'。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM