How to use PHP to turn Word documents into HTML and PDF

Source: Internet
Author: User

This article mainly describes how to use PHP to the Word document to HTML and PDF, interested in the reference of friends, I hope to be helpful to everyone.

One: Use Phpword to generate a Word document in the following steps:

Installation Environment configuration:

* * Must be installed: * *

1. "=php 5.3.3 +
2. XML Parser Extension
3. Zend\escaper Component
4. Zend\stdlib Component
5. Zend\validator Component
Selective Installation:
ZIP extension (required extensions for template templates)
GD extension
XMLWriter extension
XSL extension
Dompdf Library

You can find the PHP package class library required by https://packagist.org/.

Phpword can be installed through the composer composer require phpoffice\phpword ;
You can also add dependencies directly to the Phpword in the configuration file:

{"     require": {        "Phpoffice/phpword": "v0.14". * "     }}

Then execute composer update , (composer version too low, use composer self-update )

How to use:

Set Default Style $phpword->setdefaultfontname (' imitation ');//font $phpword->setdefaultfontsize (16);//size//Add page $section = $ Phpword->createsection ();//Add directory $styletoc = [' tableader ' and ' = \phpoffice\phpword\style\toc::tableader_dot];$ Stylefont = [' spaceafter ' = ' = ', ' name ' = ' Tahoma ', ' size ' = []; $section->addtoc ($styleFont, $styleTOC);// Default Style $section->addtext (' Hello php! '); $section->addtextbreak ();//newline character//specified style $section->addtext (' Hello world! ', [' name ' = ' Arial ', ' Si Ze ' = +, ' bold ' = True,]); $section->addtextbreak (5);//Multiple line breaks//Custom style $mystyle = ' MyStyle '; $phpWord-&gt        ; Addfontstyle ($myStyle, [' name ' = ' Verdana ', ' size ' = ', ' color ' = ' 1bff32 ', ' Bold ' = True, ' spaceafter ' = +,]); $section->addtext (' Hello laravel! ', $myStyle); $section->a Ddtext (' Hello vue.js! ', $myStyle); $section->addpagebreak ();//page break//Add text resource $textrun = $section->createtextrun () ; $textrun->ADDTExt (' bold ', [' bold ' = True]); $section->addtextbreak ();//newline character $textrun->addtext (' tilt ', [' italic ' = True]); $ Section->addtextbreak ();//newline character $textrun->addtext (' Font Color ', [' color ' = ' AACC00 ']);//List $liststyle = [' ListType ' = = \phpoffice\phpword\style\listitem::type_number]; $section->addlistitem (' List Item I ', 0, NULL, ' ListType '); $ Section->addlistitem (' list item I.A ', 1, NULL, ' ListType '); $section->addlistitem (' list item i.b ', 1, NULL, ' ListType '); $section->addlistitem (' list item i.c ', 2, NULL, ' ListType '); $section->addlistitem (' list item II ', 0, NULL, ' ListType '); $section->addlistitem (' list item ' A ', 1, NULL, ' ListType '); $section->addlistitem (' list item II.b ', 1, NULL, ' ListType ');//Hyperlink $linkstyle = [' Color ' = ' 0000FF ', ' underline ' = = \phpoffice\phpword\style\font:: Underline_single]; $phpWord->addlinkstyle (' Mylinkstyle ', $linkStyle); $section->addlink (' http:// Www.baidu.com ', ' Baidu a bit ', ' mylinkstyle '); $section->addlink (' http://www.baidu.com ', NULL, ' Mylinkstyle ');//Add picture $imagestyle = [' width ' = + 480, ' height ' + 640, ' align ' = ' center ']; $section->add Image ('./img/t1.jpg ', $imageStyle); $section->addimage ('./img/t2.jpg ', $imageStyle);//Add title $phpword-> Addtitlestyle (1, [' bold ' = True, ' color ' = ' 1bff32 ', ' size ' = ', ' name ' = ' Verdana ']); $section->addtit Le (' title 1 ', 1); $section->addtitle (' Title 2 ', 1); $section->addtitle (' Title 3 ', 1);//Add Table $styletable = [' bordercolor ' = > ' 006699 ', ' bordersize ' = 6, ' Cellmargin ' +,]; $styleFirstRow = [' bgColor ' = ' 66BBFF '];//first line style $php Word->addtablestyle (' myTable ', $styleTable, $styleFirstRow) $table = $section->addtable (' myTable '); $table- >addrow (400);//Line Gao 400$table->addcell (a)->addtext (' study number '), $table->addcell (' name ')->addtext (' names '); $ Table->addcell (->addtext) (' Professional '), $table->addrow (400);//Line High 400$table->addcell ()->addtext (' 2015123 '), $table->addcell (' xiaoming ')->addtext (' Xiao Ming '), $table->addcell (-&GT); AddText (' Computer Science and Technology '); $table->addrow (400);//Line High 400$table->addcell ("->addtext"), $table Addcell ("->addtext"), $table->addcell->addtext (' pedagogy technology ');//header and Footer $header = $section CreateHeader (); $footer = $section->createfooter (); $header->addpreservetext (' header '); $footer Addpreservetext (' footer-page {page}-{numpages}. '); /The generated document is Word2007$writer = \phpoffice\phpword\iofactory::createwriter ($phpWord, ' Word2007 '); $writer->save ('./ Word/hello.docx ');
Word Turns Html$phpword = \phpoffice\phpword\iofactory::load ('./word/hello.docx ');        $xmlWriter = \phpoffice\phpword\iofactory::createwriter ($phpWord, "HTML");        $xmlWriter->save ('./html/hello.html ');

II: Generate PDF using Tcpdf
To install using composer:composer require tecnickcom/tcpdf

How to use:

$pdf = new \tcpdf (); $pdf->writehtml (' <p> content </p> '); Output Pdf$pdf->output (' TT. pdf ', ' I ');//i output, D download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.