Centos7安裝Wkhtmltopdf -- nodejs將html轉pdf


安裝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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM