Html to word convert in php

How to convert the html tag string to a word document using phpword?

To convert an HTML string to a Microsoft Word document using PHP, you can utilize the PHPWord library. This library provides an easy-to-use interface for creating and manipulating Word documents, and it supports a wide range of features, including text formatting, tables, images, and more. If you’re looking for a way to programmatically generate Word documents from HTML content, PHPWord is a great option to consider.

Method 1: Use PHPWord to Import HTML

You can use PHPWord to import HTML and convert it to a Word Document. Here are the steps:

composer require phpoffice/phpword
require_once 'vendor/autoload.php'; $phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '

Hello World!

'
; \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save('hello_world.docx');
require_once 'vendor/autoload.php'; $phpWord = new \PhpOffice\PhpWord\PhpWord(); $section = $phpWord->addSection(); $html = '

Hello World!

'
; \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html); $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save('hello_world.docx');

This will create a new Word Document named «hello_world.docx» with the HTML string «Hello World!» converted to a Word Document using PHPWord.

Читайте также:  Лутц изучаем python 5 издание pdf

Method 2: Save Word Document as HTML, then Convert to Word Document

To convert an HTML tag string to a Word document using PHPWord, you can follow these steps:

  1. Save the HTML tag string as a Word document using the saveAs method of the PhpOffice\PhpWord\IOFactory class.
$html = "

Hello world!

"
; $phpWord = new \PhpOffice\PhpWord\PhpWord(); $section = $phpWord->addSection(); $htmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML'); $htmlWriter->save("document.docx");
  1. Convert the saved Word document to a new Word document using the load method of the PhpOffice\PhpWord\IOFactory class and the save method of the PhpOffice\PhpWord\Writer\Word2007 class.
$word2007Writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $phpWord = \PhpOffice\PhpWord\IOFactory::load("document.docx"); $word2007Writer->save("new_document.docx");
$html = "

Hello world!

"
; $phpWord = new \PhpOffice\PhpWord\PhpWord(); $section = $phpWord->addSection(); $htmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML'); $htmlWriter->save("document.docx"); $word2007Writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $phpWord = \PhpOffice\PhpWord\IOFactory::load("document.docx"); $word2007Writer->save("new_document.docx");

This code saves the HTML tag string as a Word document named «document.docx» and then converts it to a new Word document named «new_document.docx».

Method 3: Use Third-Party Conversion Tools

To convert an HTML tag string to a Word document using PHPWord, we can make use of third-party conversion tools such as the phpdocx library. This library provides a TransformDoc class that can convert HTML to Word using PHPWord.

Step 1: Install phpdocx

We can install phpdocx using Composer by running the following command:

composer require phpdocx/phpdocx

Step 2: Convert HTML to Word

To convert an HTML tag string to Word using phpdocx , we can use the TransformDoc class as follows:

use Phpdocx\Transform\TransformDoc; $html = "

Hello, world!

"
; $docx = new TransformDoc(); $docx->setStrHTML($html); $docx->generateDocx("output.docx");

This will generate a Word document named output.docx containing the HTML tag string.

Step 3: Customize the Conversion

We can customize the conversion process by passing additional parameters to the generateDocx method. For example, we can specify the output file format and the conversion options:

$docx->generateDocx("output.docx", "docx", array( "orientation" => "landscape", "margins" => array( "top" => 720, "right" => 720, "bottom" => 720, "left" => 720 ) ));

This will generate a landscape-oriented Word document with 1-inch margins on all sides.

Источник

Convert HTML to MS Word Document using PHP

The conversion of HTML into MS Word Document mainly used in the web application to generate a .doc/.docx file with dynamic HTML content. Microsoft word document is the most popular file format to export dynamic content for offline use. Export HTML content to MS word functionality can be easily implemented using JavaScript. But, if you want to convert dynamic content to Doc, server-side interaction is needed.

The server-side export to word functionality is very useful to convert dynamic HTML content to MS word document and download as a .docx file. The MS word document can be easily generated with HTML content using PHP. In this tutorial, we will show you how to convert HTML to MS word document in PHP.

HTML To Word (DOC/DOCX) Library

The HTML_TO_DOC class is a custom library that helps to generate MS word documents and include the HTML formatted content in the Word document using PHP.

  • setDocFileName() – Set document file name.
  • setTitle() – Set document title.
  • getHeader() – Create header section of the document.
  • getFotter() – Create footer section of the document.
  • createDoc() – Create word document in .dcox format.
  • _parseHtml() – Parse and filter HTML from source.
  • write_file() – Insert content in the word file.
/** 
* Convert HTML to MS Word document
* @name HTML_TO_DOC
* @version 2.0
* @author CodexWorld
* @link https://www.codexworld.com
*/
class HTML_TO_DOC
<
var
$docFile = '';
var
$title = '';
var
$htmlHead = '';
var
$htmlBody = '';

/**
* Constructor
*
* @return void
*/
function __construct() <
$this->title = '';
$this->htmlHead = '';
$this->htmlBody = '';
>

/**
* Set the document file name
*
* @param String $docfile
*/
function setDocFileName($docfile) <
$this->docFile = $docfile;
if(!
preg_match("/\.doc$/i",$this->docFile) && !preg_match("/\.docx$/i",$this->docFile)) <
$this->docFile .= '.doc';
>
return;
>

/**
* Set the document title
*
* @param String $title
*/
function setTitle($title) <
$this->title = $title;
>

/**
* Return header of MS Doc
*
* @return String
*/
function getHeader() <
$return = xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">








Оцените статью