- HTML to docx Converter 그림파일 포함
1 사전 작업[ | ]
2 그림파일 준비[ | ]
Console
Copy
[root@zetawiki ~]# wget https://licensebuttons.net/l/by-sa/4.0/88x31.png -O /var/www/html/hello.png
... (생략)
100%[=================================================>] 1,624 --.-K/s in 0s
2015-09-01 18:06:16 (159 MB/s) - “/var/www/html/hello.png” saved [1624/1624]
Console
Copy
[root@zetawiki ~]# ll /var/www/html/hello.png
-rw-r--r-- 1 root root 1624 Jan 25 2014 /var/www/html/hello.png
3 test2.php 작성[ | ]
PHP
Copy
<?php
require_once 'htmltodocx/phpword/PHPWord.php';
require_once 'htmltodocx/simplehtmldom/simple_html_dom.php';
require_once 'htmltodocx/htmltodocx_converter/h2d_htmlconverter.php';
function relative_path_to_root() {
$depth = count(explode('/', getcwd()))-1;
return str_repeat('/..', $depth);
}
$html = '<html>
<body>
<h1>Hello 친구들</h1>
<img src="hello.png"/>
<ul>
<li>하나</li>
<li>둘</li>
</ul>
</body>
</html>';
$phpword = new PHPWord();
$section = $phpword->createSection();
$html_dom = new simple_html_dom();
$html_dom->load($html);
$html_arr = $html_dom->find('html',0)->children();
$state = array(
'phpword_object' => &$phpword,
'base_root' => 'http://localhost',
'base_path' => relative_path_to_root().'/var/www/html/'
);
htmltodocx_insert_html($section, $html_arr[0]->nodes, $state);
$objWriter = PHPWord_IOFactory::createWriter($phpword, 'Word2007');
$objWriter->save('sample2.docx');
4 테스트[ | ]
Console
Copy
[root@zetawiki ~]# php test2.php
[root@zetawiki ~]# ll sample2.docx
-rw-r--r-- 1 root root 10142 Sep 1 18:50 sample2.docx
- → sample2.docx를 내려받아 열어보니 그림도 보인다.
5 같이 보기[ | ]
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.