lindm Posted December 28, 2007 Share Posted December 28, 2007 Have a small problem. Here is my scenario: 1. I am on a password protected page (basic auth). 2. I want to pass on the html source to a pdf generator (php file). How do I easiest do this? Currently the pdf generator script has opened the page with file_get_contents($_SERVER['HTTP_REFERER']) but I can't get this to work with basic auth. My hope is that there is a way to pass the html code to the pdf generator in a variable... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/83475-solved-get-html-source-of-current-page/ Share on other sites More sharing options...
Daniel0 Posted December 28, 2007 Share Posted December 28, 2007 If you need the source of the current file you can do file_get_contents(__FILE__); Quote Link to comment https://forums.phpfreaks.com/topic/83475-solved-get-html-source-of-current-page/#findComment-424683 Share on other sites More sharing options...
Psycho Posted December 28, 2007 Share Posted December 28, 2007 The OP wants the HTML output of the page. Using file_get_contents() would get the php files's content. Even if all of the page was built with plain HTML in that PHP file you would at least end up with the file_get_contents() command that would end up in the PDF output. Here is my suggestion. I don't know how your script "builds" the page, but typically you will echo (or print) code to the page as the script is processed. Instead of doing that change your code so that as you construct the page you add the HTML code to a varialbe. Then at the end of the script you echo the contents of that variable. Now all you need to do is add a simple switch on the page to determine if you will echo the content to the browser or send to the PDF generator. Example: <?php if ($GET_['output']!='pdf') { $output = 'page'; } else { $output = 'pdf'; } $content = " <html> <head> <title>Test Page</title> </head> <body>"; $content .= "Hello World!</ br></ br>\n"; $content .= "Today's date is " . date("F j, Y") . "\n"; $content .= "<br><br>\n" //Show the PDF link if the content is outpur to the browser if ($output == 'pdf') { $content .= "<a href=\"".$_SERVER['REQUEST_URI']."?output=pdf\">Output this page to PDF</a>\n" } $content .= " </body>\n"; $content .= "</html>"; if ($output=='pdf') { //Call the PDF creation script createPDF($output); } else { //Echo content to the page echo $output; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/83475-solved-get-html-source-of-current-page/#findComment-424724 Share on other sites More sharing options...
Daniel0 Posted December 28, 2007 Share Posted December 28, 2007 Ah, well... then: Before you output anything: ob_start(); After everything has been outputted: $html = ob_get_contents(); ob_end_clean(); Now the HTML source will be in $html. Quote Link to comment https://forums.phpfreaks.com/topic/83475-solved-get-html-source-of-current-page/#findComment-424725 Share on other sites More sharing options...
lindm Posted December 28, 2007 Author Share Posted December 28, 2007 Ok getting a bit confused now. For this sake we say the php page to be printed is static (it is actually created from another script to a static file...). Here is an example of the page (authentication script reduced): <?php AUTHENTICATION CODE ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Untitled Document</title> </head> <body> test <form id="form1" name="form1" method="post" action=""> <input type="submit" name="button" id="button" value="Create pdf" /> </form> </body> </html> So when the user clicks the Create pdf button, only the html code of the current page is to be sent to another script called createpdf.php for further handling. Not sure which of your suggestions I should continue to work with... Quote Link to comment https://forums.phpfreaks.com/topic/83475-solved-get-html-source-of-current-page/#findComment-424739 Share on other sites More sharing options...
lindm Posted December 28, 2007 Author Share Posted December 28, 2007 Got a working solution now. Stored the output in a variable and called the printpdf function if the printpdf button was clicked. Thanks you all! Quote Link to comment https://forums.phpfreaks.com/topic/83475-solved-get-html-source-of-current-page/#findComment-424763 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.