Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to convert a PHP page to static HTML page

I want to convert a web pages which is heavily CSS styled is written in PHP to static html so that I can embed it in an email.

I have managed to do this but for achieving this I had to convert the whole page into a string and then assign that string as email body. The layout of the page does not look as good as original as I have not been able embed CSS.

This approach has a few problems like if any changes have to be made to the layout of the page I have to redo the whole process for generating an embeddable page.

I want an easier way which will allow me to keep the original page editable in Dreamweaver visually.

like image 657
Yogi Yang 007 Avatar asked May 30 '09 09:05

Yogi Yang 007


Video Answer


3 Answers

You can use output buffers. If you have an html page, such as:

   <html>
      <head>
         <title>Blah</title>
      </head>
      <body>
        Some text here
      </body>
   </html>

Then if you put, at the top of the html file:

<?php ob_start(); ?>

And right at the bottom, after the last tag, put:

<?php 
   $string = ob_get_contents(); 

   //do whatever you need to do to the html, save it to a seperate file, email it, etc

   ob_flush();
?>

What this basically means is that the $string variable will end up with the entire static html of the page, after it has been dynamically generated. You can then use that string in an email. Although really, html pages don't work exactly the same in emails, so you may want to rethink the approach.

like image 124
Alistair Evans Avatar answered Sep 20 '22 22:09

Alistair Evans


This is a hard thing to do automatically for several reasons:

  1. If your thought HTML support in browsers was bad, email programs are an order of magnitude worse. Basically you should write HTML like it was 1999, so HTML 3.2/4.0, no CSS;
  2. You have a choice of including images as external links or embed them directly in the email. External links take up less space but many mailers block them as they're used by spammers to tag live addresses (by making each image URL they send unique and thus they can figure out which email was opened). Embedded images use a slightly different reference format;
  3. CSS support should basically be treated as nonexistant. All CSS must be internal;
  4. It is best practice, when sending HTML email, to also send a plain-text version for clients that either don't have HTML support or the user has disabled it (yes this does happen). And there is no good way of turning a complex HTML page into a plain text equivalent. It basically has to be done by hand; and
  5. Email content is different to Webpage content. Webpages typically have search boxes, menus, sidebars, headers, footers and so on. These are all things you're not interested in emailing. You're only interested in the content of the page. So blogs work quite well for this because the content of a blog post can easily be extracted. It's harder with arbitrary pages.

So there are various ways you could do this like using cURL to get the Webpage, using output buffering to capture the page by using require/include, etc but all these methods suffer from one or more of the above problems. I've generally found the only way to do HTML email is to hand-roll it.

like image 20
cletus Avatar answered Sep 21 '22 22:09

cletus


just do this in php page

Put this on top for where you want to start capturing output:

ob_start();

put this on the bottom of the php

$HtmlCode= ob_get_contents(); ob_end_flush();

$fh=fopen('index.html','w'); fwrite($fh,$HtmlCode);

fclose($fh);

then redirect to html page

like image 26
Vincy Oommen Avatar answered Sep 21 '22 22:09

Vincy Oommen