- HTML to docx Converter 그림파일 포함 + 서버에 저장하지 않고 다운로드시키기
1 사전 작업[ | ]
2 htmltodocx2.php 작성[ | ]
Console
Copy
[root@zetawiki htmltodocx]# pwd
/var/www/html/ex/htmltodocx
[root@zetawiki htmltodocx]# vi htmltodocx2.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>';
$html_dom = new simple_html_dom();
$html_dom->load($html);
$html_arr = $html_dom->find('html',0)->children();
$phpWord = new PHPWord();
$section = $phpWord->createSection();
$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);
$writer = PHPWord_IOFactory::createWriter($phpWord, 'Word2007');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="htmltodocx2.docx"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
3 테스트[ | ]
- 웹브라우저에서 http://서버주소/ex/htmltodocx/htmltodocx2.php 접속
- 예제: http://zetawiki.com/ex/htmltodocx/htmltodocx2.php
- htmltodocx2.docx가 다운로드 된다.
- 그림 파일이 잘 첨부되었다.
4 같이 보기[ | ]
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.