安裝wkhtmltopdf
wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz tar xvfJ wkhtmltox-0.12.4_linux-generic-amd64.tar.xz cd wkhtmltox/bin sudo cp ./wkhtmltopdf /usr/bin/wkhtmltopdf # 測試 wkhtmltopdf http://www.baidu.com ./test.pdf
運行過程可能提示缺少一些動態庫,直接安裝即可。
字體方塊或亂碼
需要增加中文字體
拷貝Windows下的微軟雅黑字體文件(C:\Windows\fonts下)到/usr/share/fonts/msyh下,沒有需要新建。
yum install -y fontconfig mkfontscale cd /usr/share/fonts/msyh mkfontscale mkfontdir fc-cache -fv source /etc/profile 執行 fc-list :lang=zh 可看到字體已經安裝
nodejs調用
npm install wkhtmltopdf
// URL wkhtmltopdf('http://google.com/', { pageSize: 'letter' }) .pipe(fs.createWriteStream('out.pdf')); // HTML文件 wkhtmltopdf(fs.createReadStream('index.html'), {output:'out.pdf'});
更多用法參考官網:https://wkhtmltopdf.org/usage/wkhtmltopdf.txt