티스토리 뷰

PHP에서 HTML 문서를 PDF로 변환하는 기능이 필요해졌다.
돈이 없으니 유료는 배제(유료도 HTML파일을 완벽하게 PDF로 변환해주지 못함)하고
컨버팅해주는 소스를 검색해보니 다양한 오픈소스가 존재했다.

TCPDF

 

TCPDF

Started in 2002, TCPDF is now one of the world's most active Open Source projects, used daily by millions of users and included in thousands of CMS and Web applications. Check the examples... Important A new version of this library is under development at

tcpdf.org

FPDF

 

FPDF

 

www.fpdf.org

MPDF

 

mPDF – mPDF Manual

mPDF mPDF is a PHP library which generates PDF files from UTF-8 encoded HTML. It is based on FPDF and HTML2FPDF with a number of enhancements. The original author, Ian Back, wrote mPDF to output PDF files ‘on-the-fly’ from his website, handling different l

mpdf.github.io

DOMPDF

 

dompdf/dompdf

HTML to PDF converter (PHP5). Contribute to dompdf/dompdf development by creating an account on GitHub.

github.com

PHPWKHTMLTOPDF

 

mikehaertl/phpwkhtmltopdf

A slim PHP wrapper around wkhtmltopdf with an easy to use and clean OOP interface - mikehaertl/phpwkhtmltopdf

github.com

WKHTMLTOPDF

 

wkhtmltopdf

What is it? wkhtmltopdf and wkhtmltoimage are open source (LGPLv3) command line tools to render HTML into PDF and various image formats using the Qt WebKit rendering engine. These run entirely "headless" and do not require a display or display service. The

wkhtmltopdf.org

GITHUB의 STAR점수 기준 DOMPDF가 가장 높고 최근까지 버그리포팅이 되어 있었으나
한글폰트 이슈 및 안정화적인 측면에서 WKHTMLTOPDF를 사용하기로 결정했다.

WKHTMLTOPDF는 QT Webkit 랜더링 엔진을 사용한 오픈소스(LGPLv3) 명령행 방식의 변환기로
해당 사이트에서 각 운영체제에 맞는 바이너리를 받아서 설치하면 된다.

로컬 환경에 설치 중 문제가 발생했는데 해당 사이트에서 제공하는 설치 파일이 macOS Catalina에서는
알수 없는 개발자 프로그램으로 설치가 되지 않는다.

OS 업데이트를 일주일전에 했었기 때문에 brew update 후 cask로 wkhtmltopdf를 설치했다.
brew 업데이트 및 패키지 설치 시 생각보다 오래 걸려 처음에는 멈춘줄 알았다.

1) shell에서 설치

$ brew update && brew cask install wkhtmltopdf

 

2) 설치 완료 후 테스트

$ wkhtmltopdf https://google.com google.pdf
Loading pages (1/6)
Counting pages (2/6)                                               
Resolving links (4/6)                                                       
Loading headers and footers (5/6)                                           
Printing pages (6/6)
Done

 

PDF파일이 정상적으로 생성되었다.
한글 사이트 테스트 해보니 특별히 한글폰트를 설정하지 않아도 정상적으로 노출된다.

단, 스크립트에 의해 수정된 DOM 부분은 정상적으로 출력되지 않았다.

댓글