Exporte php --> html

Résolu
rachidaych Messages postés 8 Date d'inscription mardi 12 février 2013 Statut Membre Dernière intervention 5 février 2014 - 5 févr. 2014 à 00:13
nagaD.scar Messages postés 4272 Date d'inscription samedi 8 septembre 2007 Statut Membre Dernière intervention 4 janvier 2023 - 5 févr. 2014 à 15:18
Bonjour tout l monde,
je suis entrain de développez une application PHP, mnt je fait un rapport pour export de donnée avec des requêtes SQL tout bien passe j'ai comme Résultat de Rapport :
1- Rapport.php qui récupère des paramètres POST ;) ... Traitement ... il donne :
<!DOCTYPE html>
<html>
    <head>
        <title>Titre ici</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">    
    <style>
        th {
            background: greenyellow;
            font-family: "Time New Romman";
        }
        p {
            text-align: justify;
        }
    </style>
    </head>

    <body>

        <h1>Voici un exemple de tableau HTML:</h1>
<table border="1" style="width: 100%;">
<caption style="background: none repeat scroll 0% 0% red;"> Voici le titre du tableau </caption>
<TR>
<th> Titre A1 </th>
<th> Titre A2 </th>
<th> Titre A3 </th>
<th> Titre A4 </th>
</tr>
  <TR> 
 <TD> Valeur B2 </TD>
 <TD> Valeur B2 </TD> 
 <TD> Valeur B3 </TD> 
 <TD> Valeur B4 </TD> 
  </TR>
  <TR> 
 <TD> Valeur B2 </TD>
 <TD> Valeur B2 </TD> 
 <TD> Valeur B3 </TD> 
 <TD> Valeur B4 </TD> 
  </TR>
  <TR> 
 <TD> .... </TD>
 <TD> .... </TD> 
 <TD> .... </TD> 
 <TD> .... </TD> 
  </TR> 
</TABLE>
        <p>
Auctoritate suscipitur licet centuriae certamina sunt reverenda partes Pompiliani set nomen et quotquot sint et temporis et quotquot canities sint tamen terrarum terrarum ut et circumspectum ut suffragiorum Romani regina tribus populique licet per populique canities domina nulla otiosae verecundum.
        </p>
               
    </body>
</html>

* Le tableau générer par une requête SQL (PHP/MySQL).
2- je cherche une solution sur le net pour exporter mon rapport en *.doc MS Word:
j'ai trouver un script qui fonctionne bien :
exportehtmltodoc.php
<?php
/**
     * Convert HTML to MS Word file for PHP 4.2.x or earlier
     * @author Dale Attree
     * @version 1.0.1
     * @name HTML_TO_DOC
     */
     
    /**
     * Convert HTML to MS Word file
     * @author Harish Chauhan
     * @version 1.0.0
     * @name HTML_TO_DOC
     */
     
    class HTML_TO_DOC
    {
        var $docFile="myfic";
        var $title="Facturation";
        var $htmlHead="Head of document";
        var $htmlBody="Body the document";
         
         
        /**
         * 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)
        {
            echo 'setDocFileName Entered.<br>';
            $this->docFile=$docfile;
            if(!preg_match("/\.doc$/i",$this->docFile))
                $this->docFile.=".doc";
            return;        
        }
         
        function setTitle($title)
        {
            echo 'setTitle Entered.<br>';
            $this->title=$title;
        }
         
        /**
         * Return header of MS Doc
         *
         * @return String
         */
        function getHeader()
        {
            echo 'getHeader Entered.<br>';
            $return  = <<<EOH
             <html xmlns:v="urn:schemas-microsoft-com:vml"
            xmlns:o="urn:schemas-microsoft-com:office:office"
            xmlns:w="urn:schemas-microsoft-com:office: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>
              <w:DoNotHyphenateCaps/>
              <w:PunctuationKerning/>
              <w:DrawingGridHorizontalSpacing>9.35 pt</w:DrawingGridHorizontalSpacing>
              <w:DrawingGridVerticalSpacing>9.35 pt</w:DrawingGridVerticalSpacing>
             </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()
        {
            echo 'getFotter Entered.<br>';
            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)
        {
            echo 'createDocFromURL Entered.<br>';
            if(!preg_match("/^http:/",$url))
                //$url="http://".$url;
            $f = fopen($url,'rb');
            while(!feof($f)){
                $html= fread($f,8192);
            }
            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)
        {
            echo 'createDoc Entered.<br>';
            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)
            {
                //$this->write_file($this->docFile,$doc);
                @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)
        {
            echo '_parseHtml Entered.<br>';
            $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")
        {
            echo 'write_file entered!<br>';
            $fp=@fopen($file,$mode);
            if(!is_resource($fp)){
                return false;
            }
            fwrite($fp,$content);
            fclose($fp);
            return true;
        }
 
    }
    ?>

<?php
$word = new HTML_TO_DOC();
$word->createDocFromURL('Fichier.html','fichier.doc',true);
?>

pour ce script si je fait un fichier "Fichier.html" il travail et il export un fichier DOC Parfaitement :)
Mais pour moi je peut que le résultat de "Rapport.php" enregistré dans un fichier comme "Fichier.html" dans mon serveur puis appelé par le script :'(
Merci Pour votre temps et pour votre aide :)

1 réponse

nagaD.scar Messages postés 4272 Date d'inscription samedi 8 septembre 2007 Statut Membre Dernière intervention 4 janvier 2023 17
5 févr. 2014 à 09:07
salut

en fait ce que tu souhaite c'est exporter le contenu qui s'affiche dans un pdf c'est ca ?

Dans ce cas ta question n'a pas vraiment de sens du fait qu'avec php tu génère du contenu html (dynamiquement, certes, mais du contenu html quand même), et donc finalement l'idée est d'enregistrer la page html générée par ton code php (si ca doit être automatique, prévoir sauvegarde, si c'est ponctuel, enregistre ta page)

si j'ai mal compris, redis moi ^^

naga
0
rachidaych Messages postés 8 Date d'inscription mardi 12 février 2013 Statut Membre Dernière intervention 5 février 2014
5 févr. 2014 à 10:16
Merci nagashima,
je peut que le serveur qui enregistre cette page.
0
nagaD.scar Messages postés 4272 Date d'inscription samedi 8 septembre 2007 Statut Membre Dernière intervention 4 janvier 2023 17
5 févr. 2014 à 11:03
je ne comprend pas ta phrase ... mais en gros l'idée serai de mettre le contenu qui normalement est affiché dans une variable, sauvegarde le contenu (qui sera en html) dans un fichier et lancer ton script en lui indiquant le fichier (à mon avis c'est le plus simple)
0
rachidaych Messages postés 8 Date d'inscription mardi 12 février 2013 Statut Membre Dernière intervention 5 février 2014
5 févr. 2014 à 14:46
j'ai trouvé ça qui enregistre une page php a un fichier .html
//Dans le debut de script PHP avant <html> je met :
<?php
ob_start();
?>

/*********************Contenu Et Traitement de mon Script ICI**************************/

//Dans Fin de script aprés </html> je met :
<?php  
$monfichier = fopen("fichier.html", "a+"); 
fwrite($monfichier,ob_get_contents());
// j'écris dans mon fichier tout le flux affiché après la commande ob_start()
fclose($monfichier); 
ob_end_flush(); //je termine le contrôle du flux
?>

Donc Mon problème est Résolu :) Merci nagashima : Mon français est faible désolé
0
nagaD.scar Messages postés 4272 Date d'inscription samedi 8 septembre 2007 Statut Membre Dernière intervention 4 janvier 2023 17
5 févr. 2014 à 15:18
parfais, je viens de voir le descriptif de la fonction et c'est exactement ce que je voulais te faire faire x) (c'est mon problème, je ne m'appuie pas assez sur l'existant =p) .

Bonne continuation donc

naga
0
Rejoignez-nous