get html code into a php variable
Asked Answered
E

4

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?

Eolith answered 24/9, 2010 at 7:37 Comment(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)Eolith
C
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.

Cutlor answered 24/9, 2010 at 7:41 Comment(4)
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...Eolith
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.Cutlor
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.Eolith
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.Cutlor
K
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;
Kyla answered 24/9, 2010 at 7:40 Comment(1)
$contents = ob_get_clean(); alone will do the job as ob_get_clean() both returns the value of buffer and cleans it.Concoff
C
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.

Cutlor answered 24/9, 2010 at 7:41 Comment(4)
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...Eolith
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.Cutlor
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.Eolith
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.Cutlor
D
1

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

Dobb answered 24/9, 2010 at 7:39 Comment(0)
G
-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;
Gilmore answered 13/8 at 8:17 Comment(4)
Unsure how this is more clear as you haven't provided any extra information than what already was posted?Emrich
?> <div> Your html code goes here </div> <?phpGilmore
Looks the same to meEmrich
If its so hard to see the difference for some reason then delete it i dont care.Gilmore

© 2022 - 2024 — McMap. All rights reserved.