Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert HTML (tinyMCE) to WORD (.docx)

I've been successfully able to generate a .docx document with https://github.com/djpate/docxgen but as soon as i try to include TinyMCE text, i no longer can open the document. (non valid char).

Is there a way to convert the HTML text before giving it to docxgen to avoid such error?

like image 892
sf_tristanb Avatar asked Feb 22 '12 10:02

sf_tristanb


2 Answers

Finally, I settled with this answer to create a doc (simply output html and Word will recognize it):

    header( 'Content-Type: application/msword' ); 
    header("Content-disposition: attachment; filename=" .date("Y-m-d").".doc");  
    /*
    header("Content-type: application/vnd.ms-word");
    header("Content-disposition: attachment; filename=" .date("Y-m-d").".rtf");
    */
    $html = preg_replace('%/[^\\s]+\\.(jpg|jpeg|png|gif)%i', 'http://www.akubocrm.com\\0', $html);

    print "<html xmlns:v=\"urn:schemas-microsoft-com:vml\"";
    print "xmlns:o=\"urn:schemas-microsoft-com:office:office\"";
    print "xmlns:w=\"urn:schemas-microsoft-com:office:word\"";
    print "xmlns=\"http://www.w3.org/TR/REC-html40\">";
    print "<xml>
     <w:WordDocument>
      <w:View>Print</w:View>
      <w:DoNotHyphenateCaps/>
      <w:PunctuationKerning/>
      <w:DrawingGridHorizontalSpacing>9.35 pt</w:DrawingGridHorizontalSpacing>
      <w:DrawingGridVerticalSpacing>9.35 pt</w:DrawingGridVerticalSpacing>
     </w:WordDocument>
    </xml>
    ";

    die($html);
like image 179
relipse Avatar answered Sep 30 '22 11:09

relipse


I've decided to go with the pro version of the library http://www.phpdocx.com/ as it simplifies the whole process. I hope it'll fill my needs.

like image 27
sf_tristanb Avatar answered Sep 30 '22 09:09

sf_tristanb