Php word to html

Php word to html

INTRODUCTION
HTML TO DOCX CONVERSION MAGIC

Welcome to a step-by-step tutorial on how to convert HTML to DOCX using PHP. Need to generate formal Word reports? Capture a webpage into a document? Sadly, PHP does not support DOCX file format natively. So in order to convert HTML to DOCX in PHP, we will have to download and use a third party package that can create DOCX documents.

But just how do we do that? We shall walk through an example in this guide on the usage of a free package called PHPWord – Read on to find out how!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

NAVIGATION
TABLE OF CONTENTS

Extras
Example Code Download

Step 1
Get PHPWord

Step 2
PHP Converter

Extras
More Goodies

Closing
What Next?

 

 

EXTRA
SOURCE CODE DOWNLOAD

First, here is the download link to the example source code as promised.

 

SOURCE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK START

  • Download and unzip into a folder.
  • A copy of PHPWord is not included in the zip file. Please download and install Composer first. Then fire up the command prompt, navigate to the project folder, and run composer require phpoffice/phpword to fetch the latest version automatically.
  • Hooray! There is no database involved, so just follow through each of the files contained within.

 

STEP 1
GET PHPWORD

To do the conversion magic, we are going to use an open source package called PHPWord. The unfortunate part is, you cannot just download it from GitHub, unzip, and use it straight in your script – I have tried that and it somehow did not work “out of the box”.

 

INSTALL COMPOSER

So we are going to need another application called Composer to get PHPWord. No idea why some developers like to do this, shouldn’t it be optional to less the hassle? Not all people will adopt Composer permanently after this?

 

GET THE LATEST VERSION OF PHPWORD

Anyway, after you have installed Composer, navigate to your project folder in the command prompt (or terminal), run composer require phpoffice/phpword, and watch it pull the latest version magically:

1-download.txt

D:\http\test>composer require phpoffice/phpword Using version ^0.16.0 for phpoffice/phpword ./composer.json has been created Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 4 installs, 0 updates, 0 removals   - Installing pclzip/pclzip (2.8.2): Loading from cache   - Installing phpoffice/common (0.2.9): Loading from cache   - Installing zendframework/zend-escaper (2.6.0): Loading from cache   - Installing phpoffice/phpword (0.16.0): Downloading (100%) phpoffice/phpword suggests installing ext-gd2 (Allows adding images) phpoffice/phpword suggests installing ext-xsl (Allows applying XSL style sheet to headers, to main document part, and to footers of an OOXML template) phpoffice/phpword suggests installing dompdf/dompdf (Allows writing PDF) Writing lock file Generating autoload files

 

 

STEP 2
PHP CONVERTER

Now that you have the library downloaded into your project folder, the next step is to simply use it to convert HTML code to DOCX.

 

2-convert.php

<?php /* [START PHPWORD] */ require "vendor/autoload.php"; $pw = new \PhpOffice\PhpWord\PhpWord();  /* [THE HTML] */ $section = $pw->addSection(); $html = "<h1>HELLO WORLD!</h1>"; $html .= "<p>This is a paragraph of random text</p>"; $html .= "<table><tr><td>A table</td><td>Cell</td></tr></table>"; \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);  /* [SAVE FILE ON THE SERVER] */ // $pw->save("html-to-doc.docx", "Word2007");  /* [OR FORCE DOWNLOAD] */ header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment;filename="convert.docx"'); $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($pw, 'Word2007'); $objWriter->save('php://output'); ?>

The script to generate the DOCX documents is ridiculously simple.

  • Start by including the PhpWord library, and creating the object.
  • Add a new section to the DOX document, then simply pump your HTML code in.
  • Output the file, and that’s it!

 

 

 

EXTRA) PHPWORD SUPPORTS OTHER FILE FORMATS

If you poke around the sample scripts that PHPWord provides in \vendor\phpoffice\phpword\samples , you will notice that it is actually capable of more than just .DOCX– It can also create rich text files, open document text, and even PDFs. So be sure to make full use of it –

3-more.php

<?php /* [START PHPWORD] */ require "vendor/autoload.php"; $pw = new \PhpOffice\PhpWord\PhpWord();  /* [THE HTML] */ $section = $pw->addSection(); $html = "<h1>HELLO WORLD!</h1>"; $html .= "<p>This is a paragraph of random text</p>"; $html .= "<table><tr><td>A table</td><td>Cell</td></tr></table>"; \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);  /* [PHPWORD SUPPORTS MANY OTHER FORMATS] */ $writers = [   'Word2007' => 'docx',   'ODText' => 'odt',   'RTF' => 'rtf',   'HTML' => 'html',   'PDF' => 'pdf' ]; foreach ($writers as $format => $extension) {   $target = "output.$extension";   $pw->save($target, $format); } ?>

 

LINKS & REFERENCES

 

 

CLOSING
WHAT NEXT?

Thank you for reading, and we have come to the end of this short guide. I hope that it has helped you with your project, and if there is some stuff that you wish to add on to this guide, please feel free to comment below. Good luck with your future projects and happy coding!

Php word to html

W.S. Toh is a senior web developer and software engineer with over 10 years of experience in building websites, systems, and applications. He is also a tech-gadget lover, secretly an evil code ninja.



Источник: code-boxx.com


Добавить комментарий