ubuntu1604 && centos 7 python3.6 pdfkit html轉pdf

1.安裝wkhtmltopdf

pdfkit是python對wkhtmlpdf的封裝所以需要在環境上先安裝wkhtmltopdf包

下載地址: https://wkhtmltopdf.org/downloads.html

(1)uname -m 查看系統架構

(2)x86_64 ubuntu 16.04安裝

(3)x86_64 centos 7 安裝

  •  選擇 CentOS 7  x86_64 / i686, 先右鍵複製下載鏈接。
  • 下載:wget  https://downloads.wkhtmltopdf.org/0.12/0.12.5/wkhtmltox-0.12.5-1.centos7.x86_64.rpm
  • 嘗試安裝: rpm -ivh wkhtmltox-0.12.5-1.centos7.x86_64.rpm
  • 出錯,需要依賴 
  • error: Failed dependencies:
        libXext is needed by wkhtmltox-1:0.12.5-1.centos7.x86_64
        libXrender is needed by wkhtmltox-1:0.12.5-1.centos7.x86_64
        xorg-x11-fonts-75dpi is needed by wkhtmltox-1:0.12.5-1.centos7.x86_64
        xorg-x11-fonts-Type1 is needed by wkhtmltox-1:0.12.5-1.centos7.x86_64
  • 安裝依賴 yum install -y libXext libXrender xorg-x11-fonts-75dpi xorg-x11-fonts-Type1
  • 再次安裝 rpm -ivh wkhtmltox-0.12.5-1.centos7.x86_64.rpm

(4)測試

  wkhtmltopdf "www.baidu.com" test.pdf

 

2. 安裝pdfkit

pip install pdfkit

 

3. python編碼

爲了實現 web界面 "導出文件爲pdf"  這個功能,我使用的是restful,tornado框架。

導出文件,需要設置header的content-type,Content-Disposition。

MIME:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_types

Content-Disposition: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Disposition

(1)content-type : application/pdf 

(2)Content-Disposition: "attachment; filename.pdf"

@Route(r'contract/exporttopdf')
class ExportContractHandler(BaseContractHandler):
    @coroutine
    def get(self):
        # 查詢保存的html string
        html_str = yield self.query_contract_edit_info()

        # 轉換爲pdf,設置標題
        pdf = self.pdfkit_str(html_str=html_str, title="my contract")

        # 設置頭,爲附件下載, 並設置好下載文件名字
        self.set_header("content_type", "application/pdf")
        self.set_header("Content-Disposition", 'attachment; filename=contract-{}.pdf'.format(int(time.time())))
        self.write(pdf)


@staticmethod
def pdfkit_str(html_str, title):
    options = {
        "title": title,  # 文檔標題
        'page-size': 'A4',  # A4紙
        'margin-top': '0in',
        'margin-right': '0in',
        'margin-bottom': '0in',
        'margin-left': '0in',
        'encoding': "UTF-8",   # 中文
        # 'custom-header': [
        #     ('Accept-Encoding', 'gzip')
        # ],
        'quiet': '',  # 默認將顯示所有wkhtmltopdf輸出,不想看到需傳遞quiet選項
        # 'cookie': [
        #     ('cookie-name1', 'cookie-value1'),
        #     ('cookie-name2', 'cookie-value2'),
        # ],
        # "header-right": "Page [page] of [toPage]", # 第幾頁
        # 'no-outline': None,                        # 不要目錄,大綱深度失效
        # 'outline-depth': 5,                        # 大綱的深度
        # 'enable-forms': True                       # 表單
    }
    cover = ""  # 封面
    css = ""    # css樣式
    toc = ''    # 目錄
    pdfkit.from_string(
        html_str,
        'temp.pdf',
        options=options,
        cover=cover,
        css=css,
        toc=toc
    )
    try:
        with open('temp.pdf', "rb") as f:
            return f.read()
    except:
        raise Exception("Read pdf file error")

4. 中文字體問題

一測發現中文全部是口!這怎麼回事?

原因之一:原來是缺少中文字體!

這裏有一文章可參考解決:

https://www.ostechnix.com/install-microsoft-windows-fonts-ubuntu-16-04/

我是採用拷貝window下的字體到服務器解決

安裝中文字體:
    1.查看目前安裝字體: fc-list
    2.創建目錄: mkdir /usr/share/fonts/zh_CN
    3.拷貝windows下 C:\Windows\Fonts 的中文字體到 /usr/share/fonts/zh_CN
    4.執行 fc-cache -fv
    5.查看是否已安裝 fc-list

解決後:

在centos系統上,試了一下中文還是有問題!NO!但還是解決了。

原因2: 需要在html的字符集設置爲utf8

<head><meta charset="UTF-8"></head>

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章