Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert html to word using php?

Tags:

php

I am trying to convert HTML to MS WORD document (.doc/.docx) with PHP script. With the available scripts from internet I am able to convert the text of HTML to doc. But I need total html with inline css will be in my doc.I made one script

$html = file_get_contents('html path');
$tags = "<br>";
$test = strip_tags($page,$html);
$breaks = array("<br />","<br>","<br/>");  
$text = str_ireplace($breaks, "\r\n", $test);  
$text = iconv('UTF-8', 'ASCII//TRANSLIT',$text);
$handle = fopen("newdoc.doc", "w+");
fwrite($handle, $text);
fclose($handle);

It's working for the text content of HTML only. But I can't add images to it is there any way to do it ? Please Help, Thanks in advance.

like image 764
Ishaque Javed Avatar asked Nov 16 '16 09:11

Ishaque Javed


People also ask

How can I convert HTML file to PHP file?

Just change file extension . html to . php (index. html to index.

Can we convert PHP code to HTML?

Find and select the PHP files on your computer and click Open to bring them into Doxillion to convert them to the HTML file format. You can also drag and drop your PHP files directly into the program to convert them as well.


3 Answers

just keep following code in top of the page need to convert:

<? header("Content-Type: application/vnd.ms-word");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=Report.doc");
?>
like image 80
SAR Avatar answered Sep 16 '22 22:09

SAR


I would highly recommend PHPdocx as it does exactly what your asking for, but at a cost. I purchased it sometime ago and use it frequently.

require_once 'pathToPHPDocX/classes/CreateDocx.inc';
$docx = new CreateDocx();
$html='http://www.2mdc.com/PHPDOCX/simpleHTML_Image.html';
$docx->embedHTML($html, array('isFile' => true, 'downloadImages' => true));
$docx->createDocx('simpleHTML');

http://www.phpdocx.com/documentation/introduction/html-to-word-PHP

like image 38
Kitson88 Avatar answered Sep 16 '22 22:09

Kitson88


I am currently working through something similar and have gone with

  • PHPWord

Huge amount of information on the site to get you started and you can style each element how you would like.

Images can be added simply with: $section->addImage($src, [$style]);

I would take a look. Its a bit more involved than what you are currently using, but allows you to do what you need.

like image 24
Dave L Avatar answered Sep 20 '22 22:09

Dave L