[php] html -> pdf 변환/다운로드
[php] html -> pdf 변환/다운로드 방법
1. 먼저 html pdf로 변환하기 위해서는 리눅스에 파일 설치가 필요하다.
https://wkhtmltopdf.org/ 해당 사이트에서 서버에 맞는 파일을 다운로드 한다.
2. centos 7 버전 기준으로 wkhtmltox-0.12.6-1.centos7.x86_64.rpm 해당 파일을 다운로드 받는다
3. 해당 파일을 서버에서 yum install 을 통해 설치 한다.
4. 그리고 php 파일에서 아래와 같은 형태로 실행한다
header('Content-Type: text/html; charset=UTF-8');
$url = "http://www.naver.com"; // 변환할 페이지 주소
$down_name = "test"; // 변환 파일명
$folder = "/home/user/public_html/uploads/"; // 폴더위치
//폴더의 test.pdf 저장
system('wkhtmltopdf '.$url.' '.$folder.$down_name.'.pdf'); // php 에서 shell 명령어를 실행 할수있도록 처리
//tmp폴더의 test.pdf를 클라이언트가 다운받을 수 있도록
$sFilePath = $folder.$down_name.'.pdf';
$sFileName = $down_name.'.pdf';
header("Content-Disposition: attachment; filename=\"".$sFileName."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".strval(filesize($sFilePath)));
header("Cache-Control: cache, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
echo file_get_contents($sFilePath);
flush();
unlink($sFilePath);//tmp폴더의 test.pdf 파일을 삭제