LoginSignup
24
27

More than 5 years have passed since last update.

htmlファイルをpythonでpdfに出力する

Posted at

Markdownで生成したhtmlファイルをpdfとして保存する方法。
ブラウザで開き、pdfファイルとして保存する方法が簡単だと思いますが、ここではpythonで処理する方法を紹介します。

準備

環境:python 3.4.2 / windows 8.1 Pro

まず、pdfkitパッケージをインストールします。(執筆時点では0.5.0)
pip install pdfkit

python IDLEを開いて、インストールがうまくいったかを確認します。
IDLEはターミナルからpython -m idlelib.idleとして立ち上げられます。(IDLEを開かず、単にpythonと打って試すこともできます。)

>>>import pdfkit
>>>

何事もなくプロンプトがもどれば成功です。exit()でpythonを終了します。

この段階でpython scriptを書いて試したところ、wkhtmltopdfをインストールするよう指示がでました。wkhtmltopdfのページから0.12.3.2(64 bit)をダウンロードしてインストールし、パスを通しておきます。(私の場合、C:\Program Files\wkhtmltopdf\bin

ターミナルから、which wkhtmltopdfと打ってパスが表示されればうまくいっています。

使ってみる

使い方はpdfkitのページに書かれています。htmlファイルのパスをコマンドラインからargとして渡すとhtmlファイルと同じディレクトリにpdfファイルが生成されるコードを下に示します。

# html2pdf.py
# convert html to pdf with pdfkit

import pdfkit
import sys
import os

html = sys.argv[1]

html_base = os.path.splitext(html)[0]
pdf_out = html_base + '.pdf'

options = {
    'page-size': 'A4',
    'margin-top': '0.1in',
    'margin-right': '0.1in',
    'margin-bottom': '0.1in',
    'margin-left': '0.1in',
    'encoding': "UTF-8",
    'no-outline': None
}

pdfkit.from_file(html, pdf_out, options=options)

htmlファイルのほか、URLを受けて同様の処理をするmethod、.from_url()も用意されています。こちらも便利に使えると思います。

24
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
27