Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

submit form on clicking link

Status
Not open for further replies.

dkemas

Programmer
Mar 22, 2012
70
GB
I am using dompdf and php to create a pdf page, this works fine when I pass some html to it i.e.

Code:
<?php 
require_once("dompdf/dompdf_config.inc.php");

$html ='<html><body>'.
'<p>sample pdf</p>'.
'</body></html>';

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>

But I want to pass the html of the page to it, which is where I was hoping javascript (jquery is running on the site) would help.

I was thinking that the user would click a link 'click here to save this page as pdf' which would then fire a form submission back to the same page so I could do something like

Code:
<?php 
require_once("dompdf/dompdf_config.inc.php");

$html = $_POST['pagecode'];

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>

which would output the html of the page.

My question is, how do I do the javascript part to grab the html of the page and submit it if the users click a link to generate the pdf? Is this a good way of going about it?

Thanks
 
Not the most elegant solution, but something like this should give you the html code inside the html tags of the page in a hidden form element.

Code:
function getCode()
{
var Thedoc = document.getElementsByTagName('html')[0].innerHTML;
var TheInput = document.getElementById('pagecontent');
TheInput.value = Thedoc;
document.forms.toPDF.submit();
}


HTML:
<form action = "pdfpage.php" method="POST" name="toPDF">
<a href="#" onclick="getCode(); return false;">Click Me</a>
<input type=hidden id="pagecontent" name="pagecode">
</form>

I typed this directly into the reply box so its possible there are some mistakes, but you should get the basic gist of how its done.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top