Starting with my previous question , I would like to know how I can export an image of html to Word, or at least know if it is possible or not.
I have the following table:
<table id="test" class="table table-striped table-custom table-responsive">
<tbody id="tablabody">
<tr>
<td style="width:33.333333%">
<table class="table table-striped table-custom table-responsive">
<tbody>
<tr>
<td><img src="/html/images/chico64.png" /></td>
</tr>
<tr>
<td>Nombre: IKER</td>
<td>Apellido: IKER</td>
</tr>
</tbody>
</table>
</td>
<td style="width:33.333333%">
<table class="table table-striped table-custom table-responsive">
<tbody>
<tr>
<td><img src="/html/images/chico64.png" /></td>
</tr>
<tr>
<td>Nombre: ANDONI</td>
<td>Apellido: ANDONI</td>
</tr>
</tbody>
</table>
</td>
<td style="width:33.333333%">
<table class="table table-striped table-custom table-responsive">
<tbody>
<tr>
<td><img src="/html/images/chica64.png" /></td>
</tr>
<tr>
<td>Nombre: ERIKA</td>
<td>Apellido: ERIKA</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
I export it with a button that calls the following method:
function creaWord(){
var html = document.getElementById("test").outerHTML;
var blob = new Blob(['\ufeff', html], {
type: 'application/msword'
});
var href = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = href;
a.download = "documento.doc";
document.body.appendChild(a);
if (navigator.msSaveOrOpenBlob) {
navigator.msSaveOrOpenBlob(blob, 'documento.doc');
} else {
a.click();
}
document.body.removeChild(a);
}
This correctly exports to me the format of the table, but the images are exported to me in this way. I do not even know if you can export images to Word in this way. But at least clarify whether it is possible or not, and if possible as it should be.
Edition
There is a jsfiddle that uses an external library and exports images. At first it may be a valid solution but I would like to do it without using external libraries.
On the other hand, I have seen that in this jsfiddle it converts the image to base64 but I have tried to do the same (without using the library) to see if it was coding but it still does not work.
Edition 2
After seeing the low quality of my results with javascript I'm trying to do it with php.
The first thing I've done is that the button calls the next php page.
<?php
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=documento.doc");
echo "<html>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">
<style>
@page
{
size:21cm 29.7cmt; /* A4 */
margin:1cm 1cm 1cm 1cm; /* Margins: 2.5 cm on each side */
mso-page-orientation: portrait;
}
@page Section1 { }
div.Section1 { page:Section1; }
p.MsoHeader, p.MsoFooter { border: 1px solid black; }
</style>
<body>
<div class=Section1>
<table id='test' style=' border: 1px solid black;'>
<tr>
<td class='col-4' style=' border: 1px solid black;'>
<div class='row' style=' border: 1px solid black;'>
<div class='col-6' style=' border: 1px solid black;'>
<img id='img-0' src='/html/images/chico64.png' />
</div>
<div class='col-6' style=' border: 1px solid black;'>
<h1>1</h1>
</div>
</div>
<div class='row' style=' border: 1px solid black;'>
<div class='col-12' style=' border: 1px solid black;'>
Carrero Gomez, Iker
</div>
</div>
</td>
</tr>
</table>
</div>
</body>
</html>";
?>
So far everything works relatively well but I have two problems.
The documentation to know what styles I should apply I am using this page but I still think that Some, like the page break, do not work for me.