2012-07-03 104 views
4

我想將頁面導出到.doc文件,但是當我打開生成的.doc文件時,它會在weblayout-view而不是打印視圖中打開。這是醜陋和混亂。有沒有辦法將其設置爲打印視圖?在PHP中生成的Word文檔的顯示模式

我用它來生成文檔的腳本:

<?php if(isset($_GET['word'])) { 
    header("Content-Type: application/vnd.ms-word"); 
    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("content-disposition: attachment;filename=test.doc"); 
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"> 
    <title>Example</title> 
    <style type="text/css"> /* SOME STYLING */ </style> 
</head> 
<body> 
    <h1>Hello StackOverflow!</h1> 
    <p>Lorem ipsum...</p> 
</body> 
</html> 

而現在,我在這裏是可以添加文字複選框☒和文字輸入字段?

+1

你意識到你不是真正創建一個Word文檔....這是HTML標記與頭假裝這是一個Word文檔,其中MS Word中是慷慨地打開;這就是爲什麼它會在Web佈局視圖中打開它,因爲它是一個Web文檔。 –

+0

那麼將html導出爲文檔的正確方法是什麼? – Gijs

+1

正確的方法是生成一個實際的文檔文件(BIFF或OfficeOpenXML) –

回答

5

這爲我做的伎倆:

<?php 
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=FILENAME.doc"); 
?> 
<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>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> 
</head> 
<body> 
    Yes printview! 
</body> 
</html> 
+1

只有一個問題...「另存爲」默認爲「另存爲網頁」,而不是.doc(x) – Gijs