-5

I have a need have my entire page's HTML passed into a PHP variable, but I have no idea if this is even possible or if it will accomplish what I'm trying to do.

The idea is that the user would click a button (PRINT) and the script would gather the entire HTML of the page and POST it to a PHP file. The PHP file is generating a PDF using a third party service (DocRaptor).

Any thoughts on the best way to do this? Would it even work?

EDIT Some code since you asked. This is as far as I get. This works and I get a PDF of the text submitted in the text input box. But that's obviously not what I need. I'm just not sure how to POST something that not the text input, but rather the entire current HTML of the page. It doesn't have to be a form obviously, a button (Print Page) would be ideal.

HTML:

<form method="POST" action="print_submit.php">
 Innovation Name<input type="text" name="this_data">

    <input type="submit" value="Sumbit my choice"/>

</form>

PHP (print_submit.php):

<?php
$api_key = "XXXXXX";
$url = "https://docraptor.com/docs?user_credentials=$api_key";

$content = $_POST['this_data'];
$name = 'docraptor_sample.pdf';
$post_array = array(
  'doc[name]' => $name,
  'doc[document_type]' => 'pdf',
  'doc[test]' => 'true',
  'doc[document_content]' => $content
 );

 $postdata = http_build_query($post_array);

 $opts = array('http' =>
  array(
      'method'  => 'POST',
      'header'  => 'Content-type: application/x-www-form-urlencoded',
      'content' => $postdata
  )
 );

 $context = stream_context_create($opts);

 $doc = file_get_contents($url, false, $context);

 file_put_contents($name, $doc);
 ?>

Here is a link to your pdf: <a href="<?=$name ?>"><?=$name ?></a>
user229044
  • 232,980
  • 40
  • 330
  • 338
jonmrich
  • 4,233
  • 5
  • 42
  • 94

1 Answers1

0

I have done something similar previously. Below is a link to an example using JavaScript to capture the HTML and pass to a new page for reading by PHP. There are probably better or other ways to perform it but this did the job I wanted it to a while ago so maybe something there you can get some ideas from.

http://jsfiddle.net/0w0yg5ey/

JavaScript

function getPageHTML()
{
  //Get the HTML and add to a form element before submitting the form.
  document.PageContent.HTML.value=document.all[0].innerHTML;
  document.PageContent.submit();
}
Simon
  • 698
  • 7
  • 13
  • Ok...got it working. Thanks. This passes my raw html with no styles is there any way to pass all of this together (HTML and CSS)? – jonmrich Dec 19 '14 at 15:25