4

I want to export a page to a .doc file but when I open the generated .doc it opens in weblayout-view instead of print-view. This is ugly and confusing. Is there a way to set it to print-view?

The script I use to generate the doc:

<?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>

And now that i'm here is it possible to add word-checkboxes ☒ and word-input fields?

3
  • 1
    You realise that you're not actually creating a word document.... this is HTML markup with headers to pretend it's a Word document, which MS Word is generous enough to open; that's why it opens it in web layout view, because it is a web document.
    – Mark Baker
    Commented Jul 3, 2012 at 14:39
  • then what is the proper way to export html to a doc?
    – Gijs
    Commented Jul 3, 2012 at 14:41
  • 1
    The proper way is to generate an actual doc file (either BIFF or OfficeOpenXML)
    – Mark Baker
    Commented Jul 3, 2012 at 14:42

1 Answer 1

6

This did the trick for me:

<?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
  • 1
    There is only one more problem... "Save as" default is "Save as webpage" instead of .doc(x)
    – Gijs
    Commented Jul 20, 2012 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.