views:

285

answers:

1

Here is my code, greatly truncated.

<?php
    session_start(); 
    header("Content-Type: application/msword");
    header('Content-Disposition: attachment; filename="Contrat d\'étude.doc"');
?>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>

    <body>
        <p align="right">Version: A</p>
        <img src="img/logoPoly.png" alt="École Polytechnique de Montréal" />

        <h1>CONTRAT D'ÉTUDES HORS QUÉBEC</h1>
        ............

I try here adding the image using the regular img tag, but once on the user's computer, the path doesn't work, obviously.. So how can I go about this?

And btw, is the meta tag the good way of setting the charset? Or is there a better way, perhaps using the php header function?

Thanks for helping out!

+1  A: 

Is there something in your code you have truncated which converts all that HTML to an MS Word file?

Because... er... you know saving an HTML file with a .doc extension doesn't actually make it a Word file, right?

If you've got HTML, just serve it as HTML, and the user can take care of saving images using the browser's own ‘Save web page with images’ feature. Or you could write an MHTML file if you like, including the images inside it.

But HTML-file-hiding-under-DOC-file-extension isn't really a valid format. It's something you shouldn't rely on if Word manages to load/import the thing at all; asking for it to drag in images as well is asking for the moon on a stick.

Yes, meta is the only way to set charset when you're supplying an HTML file that you expect people to save. The Content-Type header is normally a good place to put it as well, but that information will be lost as soon as the file is saved on a filesystem that doesn't support saving type metadata (which is, currently, all popular filesystems).

Incidentally, non-ASCII characters like é are totally unreliable to include in a filename header parameter. They get treated as different encodings depending on the browser (and for IE, also the locale) and there's no standard way of escaping them. The only reliable way to put non-ASCII characters in the filename of a downloaded file is to leave them as trailing URL parts, encoded as UTF-8. eg.:

http://www.example.com/download.php/Contrat%20d%27%C3%A9tude.doc
bobince
Heh, good point. Actually no, nothing in my code makes it a word document. But when I save this page as .doc and open it in word (actually open office) it shows as expected (apart from that image and any css). The reason I do this is because this page is a php-filled form which needs to be sent by e-mail as a word document. So what's MTHML anyways?
Shawn
http://en.wikipedia.org/wiki/MHTML — it's like an archive with both HTML and external resources in it. I don't know who drew up the “must be sent in mail as a Word document” requirement, but I doubt this is what they meant! Real .DOC is annoyingly difficult to create; .DOCX, .RTF and .PDF might be more reasonable.
bobince