php - wkhtmltopdf converting the current state of html page to pdf? -
html pdf happening on local server through php , offering html file pdf download working fine, html page i'm passing wkhtmltopdf filled user input need convert pdf converts blank form_1.html i've placed in web server directory.
how can pdf of open html page of named form_1.htm.
is there http_referer need use here.
<?php //passing form_1.htm page prints black form html page want print after opens in browser filled user's input. $result = shell_exec('"c:\\program files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe" form_1.htm vish.pdf 2>> err3.txt 1>> out3.txt'); echo $result; $file = "vish.pdf"; $pdf = file_get_contents("vish.pdf"); header('content-type: application/pdf'); header('cache-control: public, must-revalidate, max-age=0'); // http/1.1 header('pragma: public'); header('expires: sat, 26 jul 1997 05:00:00 gmt'); // date in past header('last-modified: '.gmdate('d, d m y h:i:s').' gmt'); header('content-length: '.strlen($pdf)); header('content-disposition: inline; filename="'.basename($file).'";'); ob_clean(); flush(); echo $pdf; ?> <html> <head> </head> <body> <p>karna' wife!</p> </body> </html>
you won't able capture information user has entered in form printing way.
to generate pdf of need save data server, wkhtmltopdf request page had form filled details form.
the reason due stateless nature of http used serve html page.
when user request form_1.html sent html page, fill in form details, information stored locally on computer.
if else request form_1.html sent copy of html page, without of details, web server yet knows nothing of details first user entered.
the server finds out content of first users form when user submits form server. server application can decide it.
if consider wkhtmltopdf works user web browser you'll see why ever receive form.
if want go wkhtmlpdf route need to
- save submitted data locally on server
- set second html page can requested ability load specific date
- set wkhtmltopdf request web page
- send resultant file user
if want send user pdf version of enter form without saving better approach may process submitted form data using php , use server side pdf generation tool, such http://www.tcpdf.org or http://www.fpdf.org/ , create pdf , send user. wouldn't form, produce pdf them.
edit: more detail on approach via wkhtmltopdf (note not secure)
given html form (form_1.html)
<html> <body> <form method="post" action="respond.php"> <input type="text" name="fieldname"> <input type="submit"> </form> in php responding form post, let's call 'respond.php'
<?php $form_values = $_post; // should validate $form_values $contents = serialize($form_values); $filename = sha1($contents); file_put_contents($filename,$contents); $result = shell_exec('"c:\\program files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe" http://localhost/get.php?filename='+$filename+' vish.pdf 2>> err3.txt 1>> out3.txt'); echo $result; $file = "vish.pdf"; $pdf = file_get_contents("vish.pdf"); header('content-type: application/pdf'); header('cache-control: public, must-revalidate, max-age=0'); // http/1.1 header('pragma: public'); header('expires: sat, 26 jul 1997 05:00:00 gmt'); // date in past header('last-modified: '.gmdate('d, d m y h:i:s').' gmt'); header('content-length: '.strlen($pdf)); header('content-disposition: inline; filename="'.basename($file).'";'); ob_clean(); flush(); echo $pdf; exit() in get.php (which needs accessible via web server)
<?php $filename = $_get['filename'] // should validate $filename $contents = file_get_contents($filename); $form_values = unserialize($contents); ?> <html> <body> <form> <input type="text" name="fieldname" value="<?php print $form_values['fieldname'] ;?>"> </form> </body> </html> the key thing being need wkhtmltopdf receive html page has form fields filled out server. when wkhtmltopdf requests http://localhost/get.php?filename='+$filename+'it receives , html page localhost has of form details filled out on it.
please note conceptual solution, illustrate how use it. wouldn't recommend in production, relies on security through obscurity
Comments
Post a Comment