0

I am trying to convert a HTML content to a doc file using PHP. I am following this tutorial.

Everything works fine when I export only textual data. But when I try to export an image file, like:

<?php
    include_once 'html_to_doc.php';  
    $htmlContent = '<h1 style="text-align: center; color: rgb(50, 91, 205); font-size: 35px;">
    <u>TITLE</u></h1>
    <div style="text-align:center";><table style="background-color:rgb(81, 205, 50); border: 2px solid black;">
    <tr>
        <td style="border: 1px solid black;">
            <img src="/image.jpg" width="150" height="150">
        </td>
        <td style="border: 1px solid black;">
            <img src="/new_image.jpg" width="150" height="150">
        </td>
        <td style="border: 1px solid black; text-align:center">
            <div style="background-color:rgb(148, 76, 17); width:100%; padding:5px; color: white; font-weight: bold;">20/03/2021</div><br>
            <div style="background-color:rgb(20, 104, 104); padding:5px; color: white; font-style: italic; font-weight: bold;">Exclusive</div>
        </td>
    </tr>
    </table></div><br>';
    $htd = new HTML_TO_DOC();
    $htd->createDoc($htmlContent, "word_file",1);
?>  

There's a cross (X) mark in place of the image, in the doc file, even when the image url is correct. Where am I going wrong? Please help me.

When I try to print the image directory

    echo __DIR__.'/image.jpg';

and go to the link, the image shows. That means the image url is correct. So why isn't it being rendered in my word file?

  • what do you think `/image.jpg` actually means ? – Ken Lee Mar 20 '23 at 10:21
  • Does this HTML work if you use it directly in a web page? That would be a good test to begin with. I note you've forgotten the quote marks at the end of each time you've declared a `src` for the images. So that's one obvious problem (there may be others). Always double-check and debug your work initially. – ADyson Mar 20 '23 at 10:22
  • Both the script and the image are in the root directory –  Mar 20 '23 at 10:23
  • You were missing a double quote after `src="/image.jpg` before you edited your question, but even with it, I'm not sure you will get the image in the word document. Have a look at [Convert your images to base64](https://www.base64-image.de/). – KIKO Software Mar 20 '23 at 10:23
  • @KIKOSoftware oh sorry, that was a typing mistake. I have also tried the `Base64` equivalent of the image, it still doesn't show in the file. –  Mar 20 '23 at 10:25
  • `that was a typing mistake`...always copy/paste your code into Stackoverflow, to avoid this kind of thing. Then we don't get distracted which stuff which isn't an issue in the real code. – ADyson Mar 20 '23 at 10:29
  • Where is the image file located in your filesystem, relative to the PHP script you're executing this from? – ADyson Mar 20 '23 at 10:33
  • P.S. I haven't looked in detail but this https://github.com/Korchy/html_to_doc seems to be a very similar concept, and, from looking at the demo they've provided, appears to support use of images. – ADyson Mar 20 '23 at 10:34
  • @ADyson both the image and the script are inside `C:\xampp\htdocs` –  Mar 20 '23 at 10:41
  • Ok. Like I said...did you test this HTML snippet in a HTML document, independently? Does it find the image then? – ADyson Mar 20 '23 at 10:45
  • @ADyson, yes, when I execute the HTML code independently, the images show –  Mar 20 '23 at 10:48
  • I don't understand, why you to get image since "/image.jpg" into src ? Because if image was in same folder you write "./image.jpg", or if doesn't work you try complete path into src="completePath/image.jpg". – JC Cavalca Mar 20 '23 at 12:53

0 Answers0