3

I have some HTML with a couple of css elements with floats and such and I want to convert it to a document like doc, docx, or rtf etc. I want to use php for the conversion.

redn0x
  • 83
  • 2
  • 8
  • 2
    Please use SO's search. It's all been discussed before – Pekka Sep 09 '11 at 16:11
  • At first i did use the SO Search option, and I found some stuff that I tried and did not work for me. The most recent question that I found was 2 years old so thats why I decided to reopen the subject and see what new technologies may popup. – redn0x Sep 09 '11 at 16:25
  • 3
    then please point out what didn't work for you - otherwise you're likely to get the same answers over and over again – Pekka Sep 09 '11 at 16:26

1 Answers1

0

My choice is https://packagist.org/packages/phpoffice/phpword

1 line to install:

composer require phpoffice/phpword

And you can convert HTML to RTF or DOC:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->addParagraphStyle('Heading2', ['alignment' => 'center']);    
$section = $phpWord->addSection();
$html = '<h1>Adding element via HTML</h1>';
$html .= '<p>Some well-formed HTML snippet needs to be used</p>';
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);

https://github.com/PHPOffice/PHPWord/blob/08064480ea915a7283a5a09ba68ad04869cb259e/samples/Sample_26_Html.php

Here is detailed article how to do styling using Symfony framework: https://ourcodeworld.com/articles/read/361/how-to-create-a-word-file-with-php-in-symfony-3

P.S.:

Here is a list of other possible options: https://www.w3.org/Tools/html2things.html

Eugene Kaurov
  • 2,356
  • 28
  • 39