Harshita
Harshita

Reputation: 31

HTML to DOCX file with php

code in index.php

<?php

$html = file_get_contents( $_REQUEST['fname']);
$filename = 'form_' . uniqid () . '.doc';
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; filename= . $filename");
echo $html;

?>

Path of html file

form_588f71c4e978d.html

after running

http://localhost/html2/html2wordf.php?fname=form_588f71c4e978d.html

i m getting below code

<!doctype html>
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html" charset="utf-8">
   <title></title>
</head>

<body>
<p>hello</p>
</body>
</html>

inside

form_588f71c4e978d.doc 

but i want only hello inside this file

means html to doc data conversion is not working , it just changing its extension from .html to .doc and data inside both the file remain same

any one getting my point ? Guide me thanks

Upvotes: 0

Views: 25084

Answers (1)

Oleg Babakov
Oleg Babakov

Reputation: 76

There is good PHP library, but it's non free: http://www.phpdocx.com/documentation/introduction/html-to-word-PHP

If you just want replace some text in your word template (I suggest that you use docx format) you can unzip docx file and you will find XML file this content.

So, you can use str_replace('{{youVariableInWordTemplate}}', $value, $wordXML);

Another way: use PhpWord

$phpWord = new PhpWord();
$section = $phpWord->addSection();
$html = '<p><strong>You html here</strong></p>';
Html::addHtml($section, $html);

$objWriter = IOFactory::createWriter($phpWord, 'Word2007');
$cacheDir = '/temp_directory_of_your_project/';
$objWriter->save($cacheDir. 'helloWorld.docx');

But this library has problem with table generating. There is opened issue: Using table tag in HTML Reader produces no output with solution to use custom class (see attachment in the post)

Also you can find improved implementation: HTML Reader from PHPWord does't work with tables?

This library support not many HTML tags (array with supported tags below):

$nodes = array(
        // $method        $node   $element    $styles     $data   $argument1      $argument2
        'p'         => array('Paragraph',   $node,  $element,   $styles,    null,   null,           null),
        'h1'        => array('Heading',     null,   $element,   $styles,    null,   'Heading1',     null),
        'h2'        => array('Heading',     null,   $element,   $styles,    null,   'Heading2',     null),
        'h3'        => array('Heading',     null,   $element,   $styles,    null,   'Heading3',     null),
        'h4'        => array('Heading',     null,   $element,   $styles,    null,   'Heading4',     null),
        'h5'        => array('Heading',     null,   $element,   $styles,    null,   'Heading5',     null),
        'h6'        => array('Heading',     null,   $element,   $styles,    null,   'Heading6',     null),
        '#text'     => array('Text',        $node,  $element,   $styles,    null,    null,          null),
        'span'      => array('Span',        $node,  null,       $styles,    null,    null,          null), //to catch inline span style changes
        'strong'    => array('Property',    null,   null,       $styles,    null,   'bold',         true),
        'em'        => array('Property',    null,   null,       $styles,    null,   'italic',       true),
        'sup'       => array('Property',    null,   null,       $styles,    null,   'superScript',  true),
        'sub'       => array('Property',    null,   null,       $styles,    null,   'subScript',    true),
        'table'     => array('Table',       $node,  $element,   $styles,    null,   'addTable',     true),
        'tbody'     => array('Table',       $node,  $element,   $styles,    null,   'skipTbody',    true), //added to catch tbody in html.
        'tr'        => array('Table',       $node,  $element,   $styles,    null,   'addRow',       true),
        'td'        => array('Table',       $node,  $element,   $styles,    null,   'addCell',      true),
        'ul'        => array('List',        null,   null,       $styles,    $data,  3,              null),
        'ol'        => array('List',        null,   null,       $styles,    $data,  7,              null),
        'li'        => array('ListItem',    $node,  $element,   $styles,    $data,  null,           null),
    );

Upvotes: 3

Related Questions