0

I want to create report in doc file this will include images and table. I wrote a following code

<?php
@header("Cache-Control: ");// leave blank to avoid IE errors
@header("Pragma: ");// leave blank to avoid IE errors
@header('Content-Description: File Transfer');
@header("Content-type: application/vnd.ms-word");
@header("Content-Disposition: attachment; filename=report.doc");
?>

<img src="http://l1.yimg.com/dh/ap/default/130613/fruit-chaat.jpg" border="0" style="border:none">
<br>
<table style="border:2;background-color:red;">
<?php 
$arr = array();
$arr = array(1,2,3,4,5,);
foreach($arr as $row=>$value){  ?>
    <tr><td>First <?php echo $value; ?></td>
    </tr>
<?php } ?>
</table>

But It write image from a location if that location is not reachable that image will not display in document file. I used to embed image but this embeds code instead of image. Please suggest some solutions. Is there any other way to doIt. I know about Com object but it works only on window. I am doing development on linux server.

5
  • Yes , but is there any way to achieve the solution. I just want to place some images and table in doc file. Is php provides some api for this. Commented Sep 12, 2013 at 16:06
  • 1
    You have several options: if you're on a windows box with MS Word installed, you can use COM to create a real .doc file; if on *nix, use Open/Libre office with UNO and the PUNO library to do the same; there's a couple of libraries like PHPWord or Livedocx; otherwise you can use <img src="data:image/jpeg;base64,..."> to embed the actualy image in the html file rather than simply linking to it Commented Sep 12, 2013 at 16:11
  • 1
    I used uri code in doc file instead of image. As below code <?php @header("Cache-Control: "); @header("Pragma: "); @header('Content-Description: File Transfer'); @header("Content-type: application/vnd.ms-word"); @header("Content-Disposition: attachment; filename=test.doc"); function data_uri($file, $mime) { $contents = file_get_contents($file); $base64 = base64_encode($contents); return ('data:' . $mime . ';base64,' . $base64); } ?> <img src="<?php echo data_uri("localhost/doc1/images/hs2.png",'image/png'); ?>" border="0" style="border:none"> Commented Sep 13, 2013 at 4:49
  • If you're using the src="data:image/jpeg;base64 method, you need to actually include the hex data that represents the bytestream of the image file in the image tag itself, as represented by the ... in my comment. If your using a normal img tag, then you need the full url for the image including the http:// and the full hostname Commented Sep 13, 2013 at 7:01
  • I am using src="data:image/jpeg;base64 method, how to include hex data that represent the bytestream. Can you give me some example here. I am stuck here to get image in doc file. Commented Sep 13, 2013 at 10:20

1 Answer 1

3

Finally I solved this problem using OpenTBS Created a template file with header, footer, images and place holders. Place holders I replaced with dynamic values. Like $reporttitle is defined in demo_ms_word.php and created a place holder as below [onshow.reportsubtitle].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.