Git Product home page Git Product logo

python-pdfkit-example's Introduction

python-pdfkit-example

python-pdfkit HTML TO PDF Example

使用 python-pdfkit 將 HTML 轉成 PDF

常常需要將一些東西轉換成 PDF,今天教你使用 python-pdfkit 快速完成這個功能。

使用 Python Flask 搭配 python-pdfkit 實現轉換成 PDF 功能。

特色

  • 搭配 python-pdfkit 實現轉換成 PDF 功能 ( HTML 轉成 PDF)。

安裝套件

請先確定電腦有安裝 Python

因為轉出 PDF 是用到 python-pdfkit 以及 wkhtmltopdf

所以必須安裝這兩個套件,以下用 Windows 環境介紹

python-pdfkit

pip install pdfkit

更多 python-pdfkit ,可參考 python-pdfkit

wkhtmltopdf

最後 python 還是會去使用 wkhtmltopdf.exe ,所以請務必安裝 wkhtmltopdf

安裝方法很簡單,基本上就是無腦安裝 exe 檔(請選擇符合自己電腦的版本),請記好自己安裝的路徑

alt tag

alt tag

安裝好 wkhtmltopdf 之後,建議大家可以先做測試,測試方法如下

先使用 cmd (命令提示字元) 切換到 wkhtmltopdf 路徑下

接著請在 cmd (命令提示字元) 輸入以下指令

wkhtmltopdf "https://www.google.com.tw/" out.pdf

alt tag

順利的話,路徑下會多出 out.pdf 的檔案,打開它可以看到我們將 google 的首頁轉成 pdf 文件了

alt tag

更多 wkhtmltopdf ,可參考 wkhtmltopdf

開始使用 python-pdfkit

設定路徑

前面有提到 python-pdfkit 還是會去使用 wkhtmltopdf.exe,所以我們必須告訴系統自己的 wkhtmltopdf.exe 路徑

config = pdfkit.configuration(wkhtmltopdf='wkhtmltopdf.exe 存在路徑')
pdfkit.from_url("目標網址", "輸出檔案", configuration=config)
config = pdfkit.configuration(wkhtmltopdf='C:/Program Files (x86)/wkhtmltopdf/bin/wkhtmltopdf.exe')
pdfkit.from_url("https://www.google.com.tw/", "out.pdf", configuration=config)

當然,你也可以直接設定電腦的環境變數。

一些 python-pdfkit 基本的使用

import pdfkit

# 指定目標網址 轉成 pdf 
pdfkit.from_url('https://www.google.com.tw/', 'out.pdf')

# 指定目標檔案(html) 轉成 pdf 
pdfkit.from_file('test.html', 'out.pdf')

# 指定目標字串 轉成 pdf 
pdfkit.from_string('Hello!', 'out.pdf')

指定目標字串也可以寫 html

body = """
    <html>
      <head>
        <meta name="pdfkit-page-size" content="Legal"/>
        <meta name="pdfkit-orientation" content="Landscape"/>
      </head>
      Hello World!
      </html>
    """

pdfkit.from_string(body, 'out.pdf')

合併多的網址、檔案(html) 轉成 pdf

pdfkit.from_url(['https://www.google.com.tw/', 'https://tw.yahoo.com/', 'https://shopping.pchome.com.tw/'], 'out.pdf')
pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')

額外多的設定,像是邊距、邊碼、大小

options = {
    'page-size': 'Letter',
    'margin-top': '0.75in',
    'margin-right': '0.75in',
    'margin-bottom': '0.75in',
    'margin-left': '0.75in',
    'encoding': "UTF-8",
    'custom-header' : [
        ('Accept-Encoding', 'gzip')
    ]
    'cookie': [
        ('cookie-name1', 'cookie-value1'),
        ('cookie-name2', 'cookie-value2'),
    ],
    'no-outline': None
}

pdfkit.from_url('https://www.google.com.tw/', 'out.pdf', options=options)

更多的額外設定的參數,可參考這份說明 wkhtmltopdf.txt

P.S
如果出現如下圖(很多黑色框框),代表你的電腦可能沒安裝字型檔 alt tag

如果出現亂碼(如下圖),代表是編碼問題

alt tag

指定編碼即可解決這個問題

options = {
        'encoding': 'UTF-8'
    }
pdfkit.from_string(body, filename, configuration=config, options=options)

alt tag

如果出現下方的錯誤

IOError: 'No wkhtmltopdf executable found'

通常是你沒安裝 wkhtmltopdf 或是 你忘記指定 wkhtmltopdf.exe 的路徑

執行畫面 (簡單範例)

執行程式,請用 系統管理員 執行 (不然有時候會無法存取)

請將記得安裝套件

pip install -r requirements.txt

首頁 alt tag 可以輸入網址 (將目標網址轉成 PDF )

PDF template (範例) alt tag

執行環境

  • Python 3.4.3

Reference

License

MIT license

python-pdfkit-example's People

Contributors

twtrubiks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

python-pdfkit-example's Issues

how can i change the pdf file font size?

@twtrubiks
i use the code
stream = pdfkit.from_string(input=html, output_path=False, options=options)
in python to get a file stream, but the result is not satisfy, it`s font is too small, however it show nomal in html file,
now, how can i slove this problem?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.