www.webdeveloper.com
Results 1 to 5 of 5

Thread: Convert HTML to Doc file

  1. #1
    Join Date
    Aug 2012
    Posts
    1

    Convert HTML to Doc file

    Hello friends ,
    Can anybody tell me how to convert HTML to Doc, I needit its very urgent.
    I got the following code, but its not working. Please somebody help me.

    <?php

    include("HTML-To-Doc.php");



    $htmltodoc= new HTML_TO_DOC();



    //$htmltodoc->createDoc("reference1.html","test");

    $htmltodoc->createDocFromURL("http:localhost/navin/html.php","test");




    ?>


    <?php

    /**

    * Convert HTML to MS Word file

    * @author Harish Chauhan

    * @version 1.0.0

    * @name HTML_TO_DOC

    */



    class HTML_TO_DOC

    {

    var $docFile="";

    var $title="";

    var $htmlHead="";

    var $htmlBody="";





    /**

    * Constructor

    *

    * @return void

    */

    function HTML_TO_DOC()

    {

    $this->title="Untitled Document";

    $this->htmlHead="";

    $this->htmlBody="";

    }



    /**

    * Set the document file name

    *

    * @param String $docfile

    */



    function setDocFileName($docfile)

    {

    $this->docFile=$docfile;

    if(!preg_match("/\.doc$/i",$this->docFile))

    $this->docFile.=".doc";

    return;

    }



    function setTitle($title)

    {

    $this->title=$title;

    }



    /**

    * Return header of MS Doc

    *

    * @return String

    */

    function getHeader()

    {

    $return = <<<EOH

    <html xmlns:v="urn:schemas-microsoft-com:vml"

    xmlns="urn:schemas-microsoft-comfficeffice"

    xmlns:w="urn:schemas-microsoft-comffice:word"

    xmlns="http://www.w3.org/TR/REC-html40">



    <head>

    <meta http-equiv=Content-Type content="text/html; charset=utf-8">

    <meta name=ProgId content=Word.Document>

    <meta name=Generator content="Microsoft Word 9">

    <meta name=Originator content="Microsoft Word 9">

    <!--[if !mso]>

    <style>

    v\:* {behavior:url(#default#VML);}

    o\:* {behavior:url(#default#VML);}

    w\:* {behavior:url(#default#VML);}

    .shape {behavior:url(#default#VML);}

    </style>

    <![endif]-->

    <title>$this->title</title>

    <!--[if gte mso 9]><xml>

    <w:WordDocument>

    <w:View>Print</w:View>

    <woNotHyphenateCaps/>

    <w:PunctuationKerning/>

    <wrawingGridHorizontalSpacing>9.35 pt</wrawingGridHorizontalSpacing>

    <wrawingGridVerticalSpacing>9.35 pt</wrawingGridVerticalSpacing>

    </w:WordDocument>

    </xml><![endif]-->

    <style>

    <!--

    /* Font Definitions */

    @font-face

    {font-family:Verdana;

    panose-1:2 11 6 4 3 5 4 4 2 4;

    mso-font-charset:0;

    mso-generic-font-family:swiss;

    mso-font-pitch:variable;

    mso-font-signature:536871559 0 0 0 415 0;}

    /* Style Definitions */

    p.MsoNormal, li.MsoNormal, div.MsoNormal

    {mso-style-parent:"";

    margin:0in;

    margin-bottom:.0001pt;

    mso-pagination:widow-orphan;

    font-size:7.5pt;

    mso-bidi-font-size:8.0pt;

    font-family:"Verdana";

    mso-fareast-font-family:"Verdana";}

    p.small

    {mso-style-parent:"";

    margin:0in;

    margin-bottom:.0001pt;

    mso-pagination:widow-orphan;

    font-size:1.0pt;

    mso-bidi-font-size:1.0pt;

    font-family:"Verdana";

    mso-fareast-font-family:"Verdana";}

    @page Section1

    {size:8.5in 11.0in;

    margin:1.0in 1.25in 1.0in 1.25in;

    mso-header-margin:.5in;

    mso-footer-margin:.5in;

    mso-paper-source:0;}

    div.Section1

    {page:Section1;}

    -->

    </style>

    <!--[if gte mso 9]><xml>

    <o:shapedefaults v:ext="edit" spidmax="1032">

    <o:colormenu v:ext="edit" strokecolor="none"/>

    </o:shapedefaults></xml><![endif]--><!--[if gte mso 9]><xml>

    <o:shapelayout v:ext="edit">

    <o:idmap v:ext="edit" data="1"/>

    </o:shapelayout></xml><![endif]-->

    $this->htmlHead

    </head>

    <body>

    EOH;

    return $return;

    }



    /**

    * Return Document footer

    *

    * @return String

    */

    function getFotter()

    {

    return "</body></html>";

    }



    /**

    * Create The MS Word Document from given HTML

    *

    * @param String $html :: URL Name like http://www.example.com

    * @param String $file :: Document File Name

    * @param Boolean $download :: Wheather to download the file or save the file

    * @return boolean

    */



    function createDocFromURL($url,$file,$download=false)

    {

    if(!preg_match("/^http:/",$url))

    $url="http://".$url;

    $html=@file_get_contents($url);

    return $this->createDoc($html,$file,$download);

    }



    /**

    * Create The MS Word Document from given HTML

    *

    * @param String $html :: HTML Content or HTML File Name like path/to/html/file.html

    * @param String $file :: Document File Name

    * @param Boolean $download :: Wheather to download the file or save the file

    * @return boolean

    */



    function createDoc($html,$file,$download=false)

    {

    if(is_file($html))

    $html=@file_get_contents($html);



    $this->_parseHtml($html);

    $this->setDocFileName($file);

    $doc=$this->getHeader();

    $doc.=$this->htmlBody;

    $doc.=$this->getFotter();



    if($download)

    {

    @header("Cache-Control: ");// leave blank to avoid IE errors

    @header("Pragma: ");// leave blank to avoid IE errors

    @header("Content-type: application/octet-stream");

    @header("Content-Disposition: attachment; filename=\"$this->docFile\"");

    echo $doc;

    return true;

    }

    else

    {

    return $this->write_file($this->docFile,$doc);

    }

    }



    /**

    * Parse the html and remove <head></head> part if present into html

    *

    * @param String $html

    * @return void

    * @access Private

    */



    function _parseHtml($html)

    {

    $html=preg_replace("/<!DOCTYPE((.|\n)*?)>/ims","",$html);

    $html=preg_replace("/<script((.|\n)*?)>((.|\n)*?)<\/script>/ims","",$html);

    preg_match("/<head>((.|\n)*?)<\/head>/ims",$html,$matches);

    $head=$matches[1];

    preg_match("/<title>((.|\n)*?)<\/title>/ims",$head,$matches);

    $this->title = $matches[1];

    $html=preg_replace("/<head>((.|\n)*?)<\/head>/ims","",$html);

    $head=preg_replace("/<title>((.|\n)*?)<\/title>/ims","",$head);

    $head=preg_replace("/<\/?head>/ims","",$head);

    $html=preg_replace("/<\/?body((.|\n)*?)>/ims","",$html);

    $this->htmlHead=$head;

    $this->htmlBody=$html;

    return;

    }



    /**

    * Write the content int file

    *

    * @param String $file :: File name to be save

    * @param String $content :: Content to be write

    * @param [Optional] String $mode :: Write Mode

    * @return void

    * @access boolean True on success else false

    */



    function write_file($file,$content,$mode="w")

    {

    $fp=@fopen($file,$mode);

    if(!is_resource($fp))

    return false;

    fwrite($fp,$content);

    fclose($fp);

    return true;

    }



    }



    ?>

  2. #2
    Join Date
    Jan 2004
    Posts
    484
    you could use the rename() which would be the easiest way
    Natdrip :P
    "water go down the hole" - plucky duck

  3. #3
    Join Date
    Aug 2012
    Posts
    11
    there available so many converter...

  4. #4
    Join Date
    Sep 2012
    Posts
    1
    Hello, I recently also the need to convert some format files to PDF format, for work reasons, I will also often used in some office software PDF format production conversion software which, PDF creation software, I used a lot of versions ofeffects are not very satisfied, there will be some shortcomings.
    Later a very good utility software found on the Internet, a new OX PDF creator tool is very easy to use, easy to operate, do not look at the tutorial shows smooth operation, you need to adapt to all the needs of the user, Come and act now, you deserve!
    You can try this function is very powerful PDF software, there are those functions that you say, you can learn from it and see that you have no help.
    More useful software introduction, please visit the following URL:
    http://www.oxpdf.com/pdf-creator.html
    Browser from Google search elements Keywords: OX PDF creator you can find this powerful software.

  5. #5
    Join Date
    Jun 2013
    Posts
    27
    Besides Word doc file conversion to html page, I still convert Word doument to other commonly used document and image filrs ussually for work reason. So, I am familar with the popular Word document converting products in the current market, if anyone need help, you can let me know.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles