Friday, January 31, 2014

PHP - Export Content to MS Word document



The following PHP code to export content in MS Word document.
 <?php
 $doc_body ="
 <h1>PHP - Export Content to MS Word document</h1>
 <p>This is a test.<p>
 <p>This is a test.<p>
 ";
 ?>
 <form name="proposal_form" action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post"">
  <input type="submit" name="submit_docs" value="Export as MS Word" class="input-button" />
</form>
<?php
  if(isset($_POST['submit_docs'])){
          header("Content-Type: application/vnd.msword");
          header("Expires: 0");//no-cache
          header("Cache-Control: must-revalidate, post-check=0, pre-check=0");//no-cache
          header("content-disposition: attachment;filename=sampleword.doc");
  }         
          echo "<html>";
          echo "$doc_body";
          echo "</html>";      
?>


4 comments:

  1. Thanks for this code which is going to save my life. Please, how do you create a page break

    ReplyDelete
  2. thanks for the code, but i gonna to how some sql query in the doc_body, how is the syntax to write php code inside the doc_body?

    ReplyDelete
  3. when i click export button word doc generated and this should be attached to email. is it possible ?

    ReplyDelete