1

I am trying to figure out if there is a way to convert HTML to DOCX file using PHP. HTML comes from wysiwyg editor (with inline styles) and I want to be able to create a DOCX file from it.

Looks like PHPWord can do just some basic inline styles and other libraries don't support inline styles at all.

Is there any way to achieve this?

2
  • 1
    Is there a specific reason you want docx? using just a doc file, and creating html inside can allow for styling Commented Apr 12, 2017 at 19:39
  • not really, care to explain "creating html inside" ? Commented Apr 12, 2017 at 22:57

1 Answer 1

2

You can approach it like this:

<?php
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");

echo "<html>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
echo "<body>";
echo "<b>My first document</b>";
echo "</body>";
echo "</html>";
?>

You can add inline styles like so:

echo "<h1 style=\"color:red;\"> Hello World! </h1>";
Sign up to request clarification or add additional context in comments.

2 Comments

That's pretty cool. But can I do some magic like setting a header/footer using this approach?
Another problem is, the moment I edit and save this document, it creates folder with three files in order to keep it's formatting

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.