Jump to content

[SOLVED] Get html source of current page?


lindm

Recommended Posts

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

Link to comment
Share on other sites

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;
} 

?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.