How can I add an image to a php-created MS Word document?
Here 开发者_C百科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!
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
Create your template on MS WORD(add some images), and save it as doc file. Open this file on notepad++ or smth code editor, copy all code into php file and add your php variables. For adding image, you need to convert an image to base64 format. If you notice word file code, added images are some character-based format.
精彩评论