LoginSignup
1
0

More than 5 years have passed since last update.

PDFKITがwkhtmltopdfを見つけられないことがある

Posted at

Pythonのライブラリ、pdfkitを使っていると、wkhtmltopdfがインストール済みの環境にもかかわらず、「No wkhtmltopdf executable found」というエラーでPDF作成に失敗することがあります。

答えはPDFKITの構成ファイルにあります。

pdfkit\configuration.py
        if not self.wkhtmltopdf:
            if sys.platform == 'win32':
                self.wkhtmltopdf = subprocess.Popen(
                    ['where', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0].strip()
            else:
                self.wkhtmltopdf = subprocess.Popen(
                    ['which', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0].strip()

Windows環境では、whereコマンドを使ってwkhtmltopdfを探しています。このため、wkhtmltopdfにパスが通っていない環境では、処理に失敗します(というか、whereコマンドなんてあったんだ・・・)。
そして、2018/10/17現在最新のバージョン0.12.5では、wkhtmltopdf.exeにパスを通してくれません(ナンテコッタイ)
そのため、以下の二通りの方法で解決が可能です。

  • wkhtmltopdf.exeにパスを通す
  • pdfkitのConfigureオブジェクトを作り、そこでwkhtmltopdf.exeのパスを指定する

自分で使う環境ならともかく、他者に使ってもらうことを考えると、パスを自力で探したほうが早いという気もします(どうせパスの情報はレジストリにも書かれていますし)

というわけで、wkhtmltopdf.exeのパスを検索して、設定してみた

wkhtmltopdfのパスは、インストール済みであれば、HKEY_LOCAL_MACHINE\SOFTWARE\wkhtmltopdf\PdfPathに格納されています。

program.py
    import winreg
    try:
      with winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\wkhtmltopdf', 
        access=winreg.KEY_READ | winreg.KEY_WOW64_64KEY) as k:
        data, regtype = winreg.QueryValueEx(k, "PdfPath")
        configure = pdfkit.configuration(wkhtmltopdf=data)
        regtype = regtype

        pdfkit.from_file(html, "output.pdf", configuration=configure)    
    except FileNotFoundError:
      pass

winreg.KEY_WOW64_64KEYは、Pythonインタプリタ自体のアーキテクチャにかかわらず、64bitのレジストリビューのデータを確認するというフラグです。64bit版のPythonを使用している場合は省略可能です(64bit版のPythonを使っているときに指定しても害はないので、そのままにしてあります)

1
0
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
1
0