Convertir código HTML a un documento de MS Word es una práctica bastante habitual en muchas aplicaciones web. Lo que hacen es generar un documento .doc/.docx con contenido HTML dinámico. Los documentos de Microsoft Word son muy populares a la hora de exportar contenido dinámico para uso offline. Puede parecer complicado, pero para nada es asÃ. Se puede implementar de forma sencilla con Javascript por ejemplo. Pero si lo que quieres es llevarlo a cabo desde el lado del servidor, en este artÃculo te explicaremos cómo hacerlo mediante PHP.
De HTML a Word
La clase HTML_TO_DOC es una librerÃa personalizada que te ayuda a generar documentos de MS Word e incluir contenido HTML formateado en dichos documentos utilizando PHP.
- setDocFileName(): Define el nombre del documento
- setTitle(): Define el tÃtulo del documento
- getHeader(): Crea la sección header del documento
- getFooter(): Crea la sección footer del documento
- createDoc(): Crea el documento Word en formato .docx
- _parseHtml(): Extrae el contenido HTML de una fuente dada
- write_file(): Inserta contenido en el fichero word
<?php class HTML_TO_DOC { var $docFile = ''; var $title = ''; var $htmlHead = ''; var $htmlBody = ''; function __construct(){ $this->title = ''; $this->htmlHead = ''; $this->htmlBody = ''; } function setDocFileName($docfile){ $this->docFile = $docfile; if(!preg_match("/.doc$/i",$this->docFile) && !preg_match("/.docx$/i",$this->docFile)){ $this->docFile .= '.doc'; } return; } function getHeader(){ $return = <<<EOH <html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta http-equiv=Content-Type content="text/html; charset=utf-8"> <meta name=ProgId content=Word.Document> <meta name=Generator content="Microsoft Word 9"> <meta name=Originator content="Microsoft Word 9"> <!--[if !mso]> <style> v:* {behavior:url(#default#VML);} o:* {behavior:url(#default#VML);} w:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <![endif]--> <title>$this->title</title> <!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Print</w:View> <w:DoNotHyphenateCaps/> <w:PunctuationKerning/> <w:DrawingGridHorizontalSpacing>9.35 pt</w:DrawingGridHorizontalSpacing> <w:DrawingGridVerticalSpacing>9.35 pt</w:DrawingGridVerticalSpacing> </w:WordDocument> </xml><![endif]--> <style> <!-- /* Font Definitions */ @font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:536871559 0 0 0 415 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:7.5pt; mso-bidi-font-size:8.0pt; font-family:"Verdana"; mso-fareast-font-family:"Verdana";} p.small {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:1.0pt; mso-bidi-font-size:1.0pt; font-family:"Verdana"; mso-fareast-font-family:"Verdana";} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} --> </style> <!--[if gte mso 9]><xml> <o:shapedefaults v:ext="edit" spidmax="1032"> <o:colormenu v:ext="edit" strokecolor="none"/> </o:shapedefaults></xml><![endif]--><!--[if gte mso 9]><xml> <o:shapelayout v:ext="edit"> <o:idmap v:ext="edit" data="1"/> </o:shapelayout></xml><![endif]--> $this->htmlHead </head> <body> EOH; return $return; } /** * Return Document footer * * @return String */ function getFotter(){ return "</body></html>"; } function createDoc($html, $file, $download = false){ if(is_file($html)){ $html = @file_get_contents($html); } $this->_parseHtml($html); $this->setDocFileName($file); $doc = $this->getHeader(); $doc .= $this->htmlBody; $doc .= $this->getFotter(); if($download){ @header("Cache-Control: ");// leave blank to avoid IE errors @header("Pragma: ");// leave blank to avoid IE errors @header("Content-type: application/octet-stream"); @header("Content-Disposition: attachment; filename="$this->docFile""); echo $doc; return true; }else { return $this->write_file($this->docFile, $doc); } } function _parseHtml($html){ $html = preg_replace("/<!DOCTYPE((.|n)*?)>/ims", "", $html); $html = preg_replace("/<script((.|n)*?)>((.|n)*?)</script>/ims", "", $html); preg_match("/<head>((.|n)*?)</head>/ims", $html, $matches); $head = !empty($matches[1])?$matches[1]:''; preg_match("/<title>((.|n)*?)</title>/ims", $head, $matches); $this->title = !empty($matches[1])?$matches[1]:''; $html = preg_replace("/<head>((.|n)*?)</head>/ims", "", $html); $head = preg_replace("/<title>((.|n)*?)</title>/ims", "", $head); $head = preg_replace("/</?head>/ims", "", $head); $html = preg_replace("/</?body((.|n)*?)>/ims", "", $html); $this->htmlHead = $head; $this->htmlBody = $html; return; } function write_file($file, $content, $mode = "w"){ $fp = @fopen($file, $mode); if(!is_resource($fp)){ return false; } fwrite($fp, $content); fclose($fp); return true; } }
Convertir HTML a un documento Word
En el siguiente ejemplo convertimos contenido HTML a un documento MS Word en formato .docx utilizando la clase HTML_TO_DOC. Para ello, primero cargamos la clase:
// Cargamos la librerÃa include_once 'HtmlToDoc.class.php'; // Inicializamos la clase $htd = new HTML_TO_DOC();
Definimos el contenido que queremos convertir:
$htmlContent = ' <h1>Hola Mundo</h1> <p>Este documento esta creado a partir de HTML.</p>';
Llamamos a la función createDoc() para convertir HTML a un documento Word. Como parámetros, le pasamos el contenido HTML ($htmlContent) y el nombre del documento (mi-documento).
$htd->createDoc($htmlContent, "mi-documento");
Descargamos el fichero Word
Para descargar el fichero Word, tendremos que pasarle un tercer parámetro a la función createDoc() tal que asÃ:
$htd->createDoc($htmlContent, "mi-documento", 1);
Crear documento Word de un archivo HTML
También puedes convertir un fichero HTML a WordPress especificando el nombre del documento en el primero parámetro de la función createDoc.
$htd->createDoc("archivo.html", "mi-documento");