将html页面导出为word

利用js和php导出页面到word的代码:

        $('#toWord').click(function(){
                setTimeout(function(){
                    var time='<?php echo $time ?>';
                    var style = document.getElementsByTagName('style')[0].innerHTML;
                    style="<style>"+style+"</style>";
                    //console.log(style);
                    var t = document.getElementById('printtext').innerHTML;//获取要生成的word的内容
                    t = b.encode(style+t);//加密操作
                    $.ajax({
                        url:"get.php",
                        data:{"html":t,"time":time},//用post方式提交到get.php
                        type:'post',
                        success:function(data){
                            console.log(data);//返回过来的是路径,本地和服务器上有所不同
                            
                            var url = "http"+data.substr(data.lastIndexOf(":"));
                            console.log(url);
                            // window.history.go(0);
                            // window.location.href=url;
                        }
                    });

                },2000);


            });

get.php的代码是这样的:

<?php

if(isset($_POST['html']))
{    
    $html=$_POST['html'];
}

if(isset($_POST['time'])){
    $time = $_POST['time'];
}

// echo $html;
$html = base64_decode($html);

class word
{ 

    function save($path, $html)
    {
        $data = '<html 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"/>
             <xml><w:WordDocument><w:View>Print</w:View>              
             </w:WordDocument>
             </xml>
             <style>
                    @page WordSection1//这里可以修改导出word后的一些样式
                    {
                        size:595.3pt 841.9pt;
                        margin:72.0pt 54.0pt 72.0pt 54.0pt;
                        mso-header-margin:42.55pt;
                        mso-footer-margin:49.6pt;
                        mso-paper-source:0;
                    }
                    span{
                        font-size:10.0pt!important;
                    }
                    div.WordSection1
                        {page:WordSection1;}
             </style>
        </head><body><div class="WordSection1">' . $html . '</div></body></html>';
        $this->wirtefile($path,$data);
    }

    function wirtefile ($fn,$data)
    {
        $fp=fopen($fn,"wb");

        fwrite($fp,$data);
        fclose($fp);
    }
}


     $word = new word(); 
    // $html = "aaa".$i; 
    $wordname = 'taiyuanReports/taiyuan'.$time.'.doc'; 
    $word->save($wordname, $html); 
    // ob_flush();//每次执行前刷新缓存 
    // flush(); 

    $url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]; 
    // 127.0.0.1/palm/new2/module/report/taiyuan/get.php;

    echo substr($url,0,-7).$wordname;

    // echo $url;

    // $url = 'http://127.0.0.1/palm/new2/module/report/taiyuan/';
    // echo $url . $wordname;
?>

另外,上传的服务器后还需要有权限才能成功.

 

posted on 2017-02-13 16:20  夜行锦衣  阅读(895)  评论(0编辑  收藏  举报

导航