Overview

Namespaces

  • Kotchasan
    • Cache
    • Database
    • Http
    • Log
    • Orm
  • None
  • PHP
  • Psr
    • Cache
    • Http
      • Message
    • Log

Classes

  • ArrayIterator
  • DateInterval
  • DOMNode
  • Kotchasan
  • Kotchasan\Accordion
  • Kotchasan\ApiController
  • Kotchasan\ArrayTool
  • Kotchasan\Cache\ApcCache
  • Kotchasan\Cache\Cache
  • Kotchasan\Cache\CacheItem
  • Kotchasan\Cache\FileCache
  • Kotchasan\CKEditor
  • Kotchasan\Collection
  • Kotchasan\Config
  • Kotchasan\Controller
  • Kotchasan\Country
  • Kotchasan\Csv
  • Kotchasan\Curl
  • Kotchasan\Currency
  • Kotchasan\Database
  • Kotchasan\Database\Db
  • Kotchasan\Database\DbCache
  • Kotchasan\Database\Driver
  • Kotchasan\Database\PdoMysqlDriver
  • Kotchasan\Database\Query
  • Kotchasan\Database\QueryBuilder
  • Kotchasan\Database\Schema
  • Kotchasan\Database\Sql
  • Kotchasan\DataTable
  • Kotchasan\Date
  • Kotchasan\DOMNode
  • Kotchasan\DOMParser
  • Kotchasan\Email
  • Kotchasan\File
  • Kotchasan\Files
  • Kotchasan\Form
  • Kotchasan\Grid
  • Kotchasan\Html
  • Kotchasan\Htmldoc
  • Kotchasan\HtmlTable
  • Kotchasan\Http\AbstractMessage
  • Kotchasan\Http\AbstractRequest
  • Kotchasan\Http\Message
  • Kotchasan\Http\NotFound
  • Kotchasan\Http\Request
  • Kotchasan\Http\Response
  • Kotchasan\Http\Stream
  • Kotchasan\Http\UploadedFile
  • Kotchasan\Http\Uri
  • Kotchasan\Image
  • Kotchasan\InputItem
  • Kotchasan\Inputs
  • Kotchasan\KBase
  • Kotchasan\Language
  • Kotchasan\ListItem
  • Kotchasan\Log\AbstractLogger
  • Kotchasan\Log\Logger
  • Kotchasan\Login
  • Kotchasan\Menu
  • Kotchasan\Mime
  • Kotchasan\Model
  • Kotchasan\Number
  • Kotchasan\ObjectTool
  • Kotchasan\Orm\Field
  • Kotchasan\Orm\Recordset
  • Kotchasan\Password
  • Kotchasan\Pdf
  • Kotchasan\Province
  • Kotchasan\Router
  • Kotchasan\Session
  • Kotchasan\Singleton
  • Kotchasan\Tab
  • Kotchasan\TableRow
  • Kotchasan\Template
  • Kotchasan\Text
  • Kotchasan\Validator
  • Kotchasan\View
  • PHPMailer
  • Psr\Log\AbstractLogger
  • Psr\Log\LogLevel
  • Psr\Log\NullLogger
  • SMTP

Interfaces

  • ArrayAccess
  • Countable
  • DateTimeInterface
  • Iterator
  • IteratorAggregate
  • Psr\Cache\CacheItemInterface
  • Psr\Cache\CacheItemPoolInterface
  • Psr\Http\Message\MessageInterface
  • Psr\Http\Message\RequestInterface
  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\ServerRequestInterface
  • Psr\Http\Message\StreamInterface
  • Psr\Http\Message\UploadedFileInterface
  • Psr\Http\Message\UriInterface
  • Psr\Log\LoggerAwareInterface
  • Psr\Log\LoggerInterface
  • SeekableIterator
  • Serializable
  • Traversable

Traits

  • Psr\Log\LoggerTrait

Exceptions

  • Exception
  • InvalidArgumentException
  • Kotchasan\ApiException
  • Kotchasan\Cache\Exception
  • Kotchasan\Database\Exception
  • Kotchasan\InputItemException
  • LogicException
  • phpmailerException
  • RuntimeException

Functions

  • createClass
  • debug
  • getClassPath
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @filesource Kotchasan/Htmldoc.php
  4:  *
  5:  * @copyright 2016 Goragod.com
  6:  * @license http://www.kotchasan.com/license/
  7:  *
  8:  * @see http://www.kotchasan.com/
  9:  */
 10: 
 11: namespace Kotchasan;
 12: 
 13: /**
 14:  * Convert HTML to MS Word file.
 15:  *
 16:  * @author Goragod Wiriya <admin@goragod.com>
 17:  *
 18:  * @since 1.0
 19:  */
 20: class Htmldoc
 21: {
 22:     /**
 23:      * ชื่อไฟล์.
 24:      *
 25:      * @var string
 26:      */
 27:     private $docFile;
 28:     /**
 29:      * เนื้อหา.
 30:      *
 31:      * @var string
 32:      */
 33:     private $htmlBody;
 34:     /**
 35:      * HTML header.
 36:      *
 37:      * @var string
 38:      */
 39:     private $htmlHead;
 40:     /**
 41:      * title.
 42:      *
 43:      * @var string
 44:      */
 45:     private $title;
 46: 
 47:     /**
 48:      * Class Constructor.
 49:      */
 50:     public function __construct()
 51:     {
 52:         $this->title = 'Untitled';
 53:         $this->htmlHead = '';
 54:         $this->htmlBody = '';
 55:         $this->docFile = '';
 56:     }
 57: 
 58:     /**
 59:      * สร้างเอกสาร MS Word จาก HTML.
 60:      *
 61:      * @param string $html HTML Content
 62:      * @param string $file Document File Name
 63:      */
 64:     public function createDoc($html, $file = '')
 65:     {
 66:         // parse เอกสาร
 67:         $this->parseHtml($html, $file);
 68:         // ส่งออกเป็น word
 69:         $response = new \Kotchasan\Http\Response();
 70:         $response->withHeaders(array(
 71:             'Content-type' => 'application/vnd.ms-word',
 72:             'Content-Disposition' => 'attachment;Filename='.$this->docFile,
 73:         ))
 74:             ->withContent($this->render())
 75:             ->send();
 76:     }
 77: 
 78:     /**
 79:      * กำหนดชื่อเอกสาร.
 80:      *
 81:      * @param string $docfile
 82:      */
 83:     public function setDocFileName($docfile)
 84:     {
 85:         $this->docFile = $docfile;
 86:         if (!preg_match('/\.doc$/i', $this->docFile)) {
 87:             $this->docFile .= '.doc';
 88:         }
 89: 
 90:         return $this;
 91:     }
 92: 
 93:     /**
 94:      * Parse HTML source.
 95:      *
 96:      * @param string $html HTML Content
 97:      * @param string $file Document File Name
 98:      */
 99:     private function parseHtml($html, $file)
100:     {
101:         // remove script
102:         $html = preg_replace('/<script((.|\n)*?)>((.|\n)*?)<\/script>/ims', '', $html);
103:         // head
104:         if (preg_match('/<head>(.*)<\/head>/isU', $html, $matches)) {
105:             $this->htmlHead = preg_replace('/<title>(.*)<\/title>/isU', '', $matches[1]);
106:         }
107:         // file name
108:         if ($file == '' && preg_match('/<title>(.*)<\/title>/isU', $html, $matches)) {
109:             $this->setDocFileName($matches[1].'.doc');
110:         }
111:         // body
112:         if (preg_match('/<body[^>]+>(.*)<\/body>/isU', $html, $matches)) {
113:             // <span class="line"></span>
114:             $this->htmlBody = preg_replace_callback('/<span[^>]+class="line([0-9]{0,})">([^>]+)<\/span>/isuU', function ($items) {
115:                 $datas = array(0 => 20, 1 => 40, 2 => 60, 3 => 80, 4 => 100);
116:                 $text = trim(str_replace('&nbsp;', ' ', $items[2]));
117:                 $len = ($datas[(int) $items[1]] - mb_strlen($text)) / 2;
118:                 for ($i = 0; $i < $len; ++$i) {
119:                     $text = '.'.$text.'.';
120:                 }
121: 
122:                 return ' <span> '.$text.' </span> ';
123:             }, $matches[1]);
124:         }
125:     }
126: 
127:     /**
128:      * สร้างเอกสาร DOC.
129:      *
130:      * @return string
131:      */
132:     private function render()
133:     {
134:         return <<<EOH
135:   <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:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
136:   <head>
137:     <style>
138:       v\:* {behavior:url(#default#VML);}
139:       o\:* {behavior:url(#default#VML);}
140:       w\:* {behavior:url(#default#VML);}
141:       .shape {behavior:url(#default#VML);}
142:     </style>
143:     <style>
144:       @page {
145:         size: 21cm 29.7cm;
146:         margin: 1cm 1cm 1cm 1cm;
147:         mso-page-orientation: portrait;
148:       }
149:       @page WordSection1 {
150:         mso-title-page: no;
151:         mso-paper-source:0;
152:         mso-header-margin: 0;
153:         mso-footer-margin: 0;
154:       }
155:       div.WordSection1 {
156:         page:WordSection1;
157:         mso-header-margin: 0;
158:         mso-footer-margin: 0;
159:       }
160:     </style>
161:     $this->htmlHead
162:   </head>
163:   <body>
164:     $this->htmlBody
165:   </body>
166: </html>
167: EOH;
168:     }
169: }
170: 
Kotchasan API documentation generated by ApiGen