>  기사  >  백엔드 개발  >  PHP를 사용하여 HTML을 PDF 파일로 변환하는 방법

PHP를 사용하여 HTML을 PDF 파일로 변환하는 방법

藏色散人
藏色散人원래의
2020-07-06 09:18:284642검색

php를 사용하여 html을 pdf 파일로 변환하는 방법: 먼저 pdf를 다운로드하여 설치한 다음 효과를 테스트한 다음 "shell_exec" 함수를 사용하여 PHP에서 호출하고 마지막으로 페이징 문제를 해결합니다.

PHP를 사용하여 HTML을 PDF 파일로 변환하는 방법

몇몇 html 페이지에서 pdf 파일을 생성해야 한다는 고객님이 계셨는데, php를 사용하여 html 페이지를 pdf 파일로 변환해주는 클래스를 찾아봤습니다. html2pdf, pdflib, FPDF 등을 많이 찾아봤지만 어느 것도 내 요구 사항을 충족하지 못했다고 할 수 있습니다.

pdflib, FPDF 이 두 가지 방법에는 pdf를 생성하는 프로그램 작성이 필요합니다. 즉, html2pdf는 html 페이지를 pdf 파일로 변환할 수 있지만 html의 경우 간단한 html 코드만 변환할 수 있습니다. 콘텐츠가 배경 뉴스 편집기를 통해 조판되기를 원하면 확실히 작동하지 않을 것입니다.

오랜 시간 고생 끝에 바이두와 구글에서 검색을 하다가, 그 노력이 결실을 맺었고, 드디어 아주 사용하기 쉬운 방법을 찾았습니다.

그것은: wkhtmltopdf, wkhtmltopdf는 브라우저에서 탐색할 수 있는 모든 웹 페이지를 직접 PDF로 변환할 수 있습니다. 우선, PHP 클래스가 아니라 HTML 페이지를 PDF로 변환하는 소프트웨어라는 점을 설명하겠습니다. 하지만 단순한 데스크톱 소프트웨어가 아니며 cmd 일괄 처리를 직접 수행합니다. 그리고 PHP에는 shell_exec() 함수가 있습니다. 다음은 PHP를 사용하여 PDF 파일을 생성하는 방법에 대한 단계별 소개입니다.

먼저 pdf를 다운로드하여 설치하세요
다운로드 주소: http://code.google.com/p/wkhtmltopdf/downloads/list
다양한 플랫폼에 대한 설치 패키지가 있습니다. 영어가 좋지 않다면 Google을 사용하세요. 번역해 보세요. 다음은 Windows 플랫폼에서 사용하는 예입니다. wkhtmltopdf-0.9.9-installer.exe 버전을 다운로드하여 win7 32비트 64비트 및 Windows 2003에서 문제 없이 설치하고 테스트했습니다. 다운로드 후 아래에서 사용할 설치 경로를 주의해서 직접 설치하세요.
설치 후 "Path"라는 시스템 환경 변수에 다음을 추가해야 합니다. ;C:Program Files (x86)wkhtmltopdf, 이는 설치한 디렉터리입니다. 설치 후 컴퓨터를 다시 시작하십시오.

둘째, 효과 테스트
cmd에 직접 입력: wkhtmltopdf http://www.shwzzz.cn/ F:website1.pdf
첫 번째는 실행 중인 소프트웨어 이름(변경되지 않음) 두 번째는 세 번째 부분입니다. URL은 생성된 경로와 파일 이름입니다. Enter를 누른 후 생성 진행률 표시줄이 표시됩니까? 성공을 축하합니다. 생성 디렉터리로 이동하여 새로 생성된 PDF 파일이 있는지 확인하세요.

셋째, php에서 호출
php에서 호출하는 방법은 매우 간단합니다. shell_exec 함수를 사용하면 됩니다. shell_exec 함수를 사용할 수 없는 경우 php.ini에서 비활성화되어 있는지 확인하세요.
예: b2c7ff02e5174be99b172ec4ea3db754

셋째, 페이징 문제 해결
wkhtmltopdf는 매우 유용하지만 다소 불만족스럽기도 합니다. HTML 페이지가 너무 길어서 지정된 위치에 페이지를 매겨야 하는 경우 어떻게 해야 합니까? wkhtmltopdf 개발자가 개발할 때 이것을 고려하지 않은 것은 아닙니다.
예를 들어 다음 html 페이지는

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<title>pdf</title>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
</head>  
<style type="text/css">  
*{ margin:0px; padding:0px;}  
div{ width:800px; height:1362px;margin:auto;}  
</style>  
<body>  
<div style=" background:#030"></div>  
<div style=" background:#033"></div>  
<div style=" background:#369"></div>  
<div style=" background:#F60"></div>  
<div style=" background:#F3C"></div>  
<div style=" background:#F0F"></div>  
<div style=" background:#0FF"></div>  
<div style=" background:#FF0"></div>  
<div style=" background:#00F"></div>  
<div style=" background:#0F0"></div>  
<div style=" background:#033"></div>  
<div style=" background:#369"></div>  
<div style=" background:#F60"></div>  
<div style=" background:#030"></div>  
<div style=" background:#033"></div>  
<div style=" background:#369"></div>  
<div style=" background:#F60"></div>  
<div style=" background:#F3C"></div>  
<div style=" background:#F0F"></div>  
<div style=" background:#0FF"></div>  
<div style=" background:#FF0"></div>  
<div style=" background:#00F"></div>  
<div style=" background:#0F0"></div>  
</body>  
</html>

PDF로 생성할 때 PDF를 셀 수 없이 디버깅한 후 각 블록이 한 페이지가 되기를 원했습니다. 한 페이지가 1362px 정도인데 끝으로 갈수록 값이 틀려지는데, PDF 한 페이지가 몇 픽셀인지는 아직 알 수 없습니다.

하지만 wkhtmltopdf에는 p 스타일 뒤에 page-break-inside:avoid;를 추가하는 좋은 방법이 있으며 괜찮습니다.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<title>pdf</title>  
<link href="css/style.css" rel="stylesheet" type="text/css" />  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
</head>  
<style type="text/css">  
*{ margin:0px; padding:0px;}  
div{ width:800px; min-height:1362px;margin:auto;page-break-inside:avoid;}  
</style>  
<body>  
<div style=" background:#030"></div>  
<div style=" background:#033"></div>  
<div style=" background:#369"></div>  
<div style=" background:#F60"></div>  
<div style=" background:#F3C"></div>  
<div style=" background:#F0F"></div>  
<div style=" background:#0FF"></div>  
<div style=" background:#FF0"></div>  
<div style=" background:#00F"></div>  
<div style=" background:#0F0"></div>  
<div style=" background:#033"></div>  
<div style=" background:#369"></div>  
<div style=" background:#F60"></div>  
<div style=" background:#030"></div>  
<div style=" background:#033"></div>  
<div style=" background:#369"></div>  
<div style=" background:#F60"></div>  
<div style=" background:#F3C"></div>  
<div style=" background:#F0F"></div>  
<div style=" background:#0FF"></div>  
<div style=" background:#FF0"></div>  
<div style=" background:#00F"></div>  
<div style=" background:#0F0"></div>  
</body>  
</html>

http://code.google.com/p/wkhtmltopdf/wkhtmltopdf 문제에 대한 커뮤니케이션 플랫폼이지만 영어로 제공됩니다.

많은 관련 지식을 알고 싶으시면 PHP 중국어 홈페이지를 방문해주세요!

위 내용은 PHP를 사용하여 HTML을 PDF 파일로 변환하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.