標籤:har pre ade type 上傳 base64 log 還需要 pap
利用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的代碼是這樣的:
<?phpif(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;?>
另外,上傳的伺服器後還需要有許可權才能成功.
將html頁面匯出為word