Convert HTML (tinyMCE) to WORD (.docx)

14,190

Solution 1

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.

Solution 2

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);
Share:
14,190
sf_tristanb
Author by

sf_tristanb

CTO @ Y-Proximité (Lyon - France) | @sf_tristanb Symfony developper | Personal project : http://www.seek-team.com

Updated on June 11, 2022

Comments

  • sf_tristanb
    sf_tristanb almost 2 years

    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?

  • ZanattMan
    ZanattMan over 11 years
    This software is privative and you have to pay to use almost every useful function, so it isn't a good solution.
  • sf_tristanb
    sf_tristanb over 11 years
    It's better than nothing. If you have a better solution, feel free to add it.
  • relipse
    relipse about 11 years
    Did phpdocx.com fill your needs Tristan? I too am thinking about buying this (embedHTML() is in the pro version)
  • sf_tristanb
    sf_tristanb about 11 years
    Yeah but I must warn you that the library is way beyond good coding standard but it does what we need.