简体   繁体   中英

PHP + generate .html file for download

I'm not sure what to even be searching for here, so I'll explain what I am attempting to do.

I current have a form (several dropdowns, input fields..etc) When the form is submitted, I am currently generating an 'output page' for print that has all user entered data on the initial form state.. with a 'print document' button at the bottom.

In which the end user would print out and write in any custom/specific comments..etc..

The above all works as intended.

However.. there has been a change in direction.. and instead of printing it out. (and hand writing any custom/unique comments by hand)..

...they want to be able to 'DOWNLOAD' the output/display page as a .html file

(so I'd need to make all assets in-file..etc. like styles, and probably based64 encode any/all images..etc)

As I have never done this before.. I'm not even surer what Im look for?

Should this be a separate (physical) .html file? (how do I get the submitted data/values into it before downloading?)

Should is just be a huge 'string' that I pull in the post values into?

But once that is complete.. how do I prompt the user to save it as a downloadable .html file?

You could use "download" atribute in "a" html tag:

<a href="http://example.com/some.html" download>Download</a> 

Unfortunately this feature is not supported by IE browser .

Suppose you have your $html_string handling the contents of the page you want to download. It will be usefull to include CSS references.

<?php    
    $html_string.="<h1>This is your form</h1>";
    foreach ($_POST as $p=>$value) $html_string.="{$p} has {$value}";

    header('Content-Disposition: attachment; filename="readme.html"');
    // other header() calls you need
    die($html_string);    
?>

This should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM