Reputation: 39
I have the html code as below:
file.html
<body>
<h1>TEST</h1>
<p>this is test</p>
<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td><img src="../../../wamp/www/html2doc/SGEPP.jpg"></td>
</tr>
</table>
html2doc.php
<?php
$handle = fopen("doc2html.html","r");
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=html2word.doc");
echo $contents;
?>
The problems:
when I convert it,I get html2word.doc but I can get only all texts from html file.For the images in html file I can not get it, it is missing images.So I want to get all data from html and images also.How do I fix this?Anyone help me please,Thanks.
Upvotes: 1
Views: 9231
Reputation: 435
I found that Word support data uri, you may convert your image into base64.
// A few settings
$image = 'cricci.jpg';
// Read image path, convert to base64 encoding
$imageData = base64_encode(file_get_contents($image));
// Format the image SRC: data:{mime};base64,{data};
$src = 'data: '.mime_content_type($image).';base64,'.$imageData;
// Echo out a sample image
echo '<img src="', $src, '">';
Upvotes: 0
Reputation: 1
Try adding a random query after .jpg
or .png
like for example :
example.com/photo.jpg?ts=12345
.
Upvotes: 0
Reputation: 11
Generally, header() function only redirect and force to download specific application but word file with images does not work properly it just read from source not a permanent doc file...
Upvotes: 0
Reputation: 1810
I deal with this script (html2doc) a week ago.
So notice that u dont save images inside *.doc
file. They are inserted only like links to your server. So solution is to write absolute path in src
tag.
U read line-by-line your HTML page. So try to find in each line your img tag and replace src with new one.
$handle = fopen("html2doc.html","r");
$contents = '';
while (!feof($handle)) {
$str = fread($handle, 8192);
$str = str_replace('src="../../../','src="http://'.$_SERVER['SERVER_NAME'].'/path/to/imgages/',$str);
$contents .= $str;
}
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=html2word.doc");
echo $contents;
// Output:
<body>
<h1>TEST</h1>
<p>this is test</p>
<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td><img src="http://www.temp.com/path/to/imgages/wamp/www/html2doc/SGEPP.jpg" /></td>
</tr>
</table>
So now images have path, which MS Word can read well and display images. But remember:
1. u need internet connection to display image
2. deleting (or unavailable server) image will make them unavailable in all generated documents
3. there is no image in doc file included
Upvotes: 3