Generating a Download-able Microsoft Word or Excel Document (on-the-fly) using Object Oriented PHP5.

Category: Software and Systems Engineering
Author: Sean O'Donnell
Sat, Mar. 20th, 2004 @ 2:20:22 (MST)

This example shows a basic approach to generating Microsoft Word and Excel Documents 'On-the-Fly', via Object Oriented PHP5.

Generating Download-able Documents via PHP is rather simple, so to make things more practical (and fun =), we'll use a more advanced (yet still very basic) Object Oriented approach, in PHP5.

fileman.php

<?php
/**
 * @file fileman.php
 * @desc A File Management Object written in PHP5. This Object allows you to Generate a Download-able Document 'on-the-fly'.
 * @author Sean O'Donnell http://code.seanodonnell.com
 */
class FileMan {

    
/** @var string File Name */
    
private $file;

    
/** @var string File Content/Data */
    
private $content;

    
/** @var string File Mime/Type */
    
private $type;

    
/** @var object File Stream */
    
private $f_conn;

    
/** 
     * @desc Object Constructor 
     * @param string File Name
     * @param string File Content
     * @param string File Mime/Type
    */
    
public function FileMan($f_name,$f_content,$f_type) {
        
$this->file $f_name;
        
$this->content $f_content;
        
$this->type $f_type;
        
$this->write_file();
        
$this->download_file();
        return;
    }

    
/** @desc write the $content to the $file */
    
private function write_file() {
        
$this->f_conn = @fopen($this->file,'w+');
        @
fputs($this->f_conn,$this->content,strlen($this->content)); 
        @
fclose($this->f_conn);
        return;
    }

    
/** @desc prompt the user to download the $file */
    
private function download_file() {
        
header("Content-type: ".$this->type);
        
header("Content-Disposition: attachment; filename=".$this->file);
        
header("Content-Length: ".filesize($this->file));
        @
readfile($this->file);
    }
}
?>

Include the PHP5 Object above into your document-generator script (below).

word.php

In this file, we will include the 'FileMan' object (above), and begin to generate our document.

The example below will generate a Microsoft WORD Document, on-the-fly!

<?php
/**
 #
 # file: word.php
 # desc: Generate a Download-able Microsoft Word Document 'on-the-fly'
 # 
 # example by: Sean O'Donnell
*/

/** the 'FileMan' Object is Required */
require 'fileman.php';

/** assign your html content to a variable */
$file['content'] = <<<HTML
    <html>
    <head>
        <title>example word document generated by php</title>
    </head>
    <body>
        <h2>Generating a Microsoft Word Document 'on-the-fly', by Sean O'Donnell</h2>
        <p>This is a basic example of how to generate a Microsoft Word Document, on-the-fly!</p>
        <p>Pretty simple eh? =p</p>
    </body>
    </html>
HTML;

/** generate the download-able document */
new FileMan("example.doc",$file['content'],"application/word");
?>

excel.php

The 'FileMan' object above can also by used to generate Download-able Microsoft Excel Documents, on-the-fly.

<?php
/**
 #
 # file: excel.php
 # desc: Generate a Download-able Microsoft Excel Document 'on-the-fly'
 # 
 # example by: Sean O'Donnell
*/

/** the 'FileMan' Object is Required */
require 'fileman.php';

/** assign your html content to a variable */
$file['content'] = <<<XLS
    <table border=1>
    <tr>
        <td>col1</td>
        <td>col2</td>
        <td>col3</td>
    </tr>
    <tr>
        <td>row1</td>
        <td>row1</td>
        <td>row1</td>
    </tr>
    <tr>
        <td>row2</td>
        <td>row2</td>
        <td>row2</td>
    </tr>
    </table>
XLS;

/** generate the download-able document */
new FileMan("example.xls",$file['content'],"application/excel");
?>

html.php

The 'FileMan' object above can also by used to generate Download-able HTML Documents, on-the-fly.

<?php
/**
 #
 # file: html.php
 # desc: Generate a Download-able HTML Document 'on-the-fly'
 # 
 # example by: Sean O'Donnell
*/

/** the 'FileMan' Object is Required */
require 'fileman.php';

/** assign your html content to a variable */
$file['content'] = <<<HTML
    <html>
    <head>
        <title>example html document generated by php</title>
    </head>
    <body>
        <h2>Generating a Download-able HTML Document 'on-the-fly', by Sean O'Donnell</h2>
        <p>This is a basic example of how to generate a Download-able HTML Document, on-the-fly!</p>
        <p>Pretty simple eh? =p</p>
    </body>
    </html>
HTML;

/** generate the download-able document */
new FileMan("example.html",$file['content'],"text/html");
?>

Copyleft (<) 1998-2019 www.seanodonnell.com