2

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');

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

I appreciate any suggestions.

Many thanks in advance!

AnchovyLegend
  • 12,139
  • 38
  • 147
  • 231

1 Answers1

4

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

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

doliver
  • 980
  • 6
  • 7
  • Thanks for the reply. Please do. I don't really understand how using an output buffer helps me capture the text a user may have entered into an input field. – AnchovyLegend Apr 13 '13 at 01:01
  • Agh, I missed that part the first time. Php knows nothing about the input values of the fields until they're submitted. What's the actual application of the code? You might be able to pull off something where you submit the page back to php once the values are realized and then output buffer the page. – doliver Apr 13 '13 at 01:12
  • Yes, thats fine, they are going to get submitted so PHP will 'know' of the values. The application of the code is generally to give users the capability to print invoice information to PDF. – AnchovyLegend Apr 13 '13 at 01:26
  • Does wkhtmltopdf even read the value of input fields if they're set manually? Anyway, if you're going to submit the values back to the server then you could recreate the page on the backend setting the values of the text fields manually from the submission. Not the most elegant solution, but it would probably work if wkhtmltopdf is compatible. – doliver Apr 13 '13 at 01:32
  • Yes it does, everything that is part of the initial load page. – AnchovyLegend Apr 13 '13 at 01:41