5

I'm don't use PHP that much and right now I'm stuck at a problem. I need to save the site of a webbrowser as a pdf. I'm using right now mPDF (which was suggested in a wiki of stackoverflow) and it seems to work pretty well (I just have simply write a short html code into a php variable and then created the pdf).

But now I must get the html code of the actual site in the browser and save it then into a php variable. How can I do that?

1
  • Okay I need to add more details. The site is generated by a php code. So if I would give that code to the printer, then it will be a mess. Furthermore, it must be user friendly, meaning that a user just can press a save button and then the pdf will be generated. (The user sees a confirm html page and he wants to save that confirm page as a pdf) Commented Sep 24, 2010 at 7:47

4 Answers 4

8

If I understand you correctly, you can store page contents into php variable like this:

ob_start();

// your html code goes here

$contents = ob_get_contents();
ob_end_clean();

// see the contents now
echo $contents;
Sign up to request clarification or add additional context in comments.

1 Comment

$contents = ob_get_clean(); alone will do the job as ob_get_clean() both returns the value of buffer and cleans it.
7

You can probably fetch the remote content via PHPs file_get_contents()-function:

$html = file_get_contents('http://example.org');

If this does not work, make sure that you have allow_url_fopen enabled in your php.ini.

4 Comments

the problem is that the during the process the link does not change. for example your order contains 2 pages. first page = choose product, second page = confirmation. now i just want to save the confirmation page as a pdf. when i use your solution, then i have a pdf file containing the first page, which is also empty (you dont your selection), because with file_get_contents('') it loads the page from new on...
Okay, i get the problem. The problem is, that the server cannot (or should not) use the clients session to get the same view of the website. I think you might need the ob_*-functions to capture the output of the page you want to put into your PDF. Do it like this: If the user invokes the page with a print-parameter (provided by your print-link), you print your page as usual. But you do it like Sarfraz suggested. ob_start() before your page is printed, ob_get_contents() afterwards to recieve the output. ob_end_clean() prevents PHP from sending the output.
What do you mean with "print your page as usual?". The page is written php, meaning that it contains a mix of html and php code. right now the print button just calls another .php file (action='printpdf.php') and that file has a small code which print the content of my provided php variable.
Simply do not link to a new page. Instead, link to the current page and provide a parameter like ?print=true to indicate wether the page should be output as PDF. Invoke the ob_*-functions if that parameter is set: ob_start() at the top (above everything that leads to output), ob_get_contents() and ob_end_clean() at the very bottom.
1

This is probably over-kill, but you can try using SimpleHTML DOM which can load a remote website and extract out its HTML.

Comments

-1

A more clear example of how to get the html in than Sarfraz is

ob_start();

?>
<div>
  Your html code goes here
</div>
<?php

$contents = ob_get_contents();
ob_end_clean();

// see the contents now
echo $contents;

4 Comments

Unsure how this is more clear as you haven't provided any extra information than what already was posted?
?> <div> Your html code goes here </div> <?php
Looks the same to me
If its so hard to see the difference for some reason then delete it i dont care.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.