使用(Flask)pdfkit和wkhtmltopdf

时间:2018-08-07 07:13:36

标签: python-3.x heroku wkhtmltopdf pdfkit python-pdfkit

我正在将python中的HTML文件转换为pdf并将这些pdf文件附加到邮件中并进行处理

  • 我已经为Windows安装了pdfkit(0.6.1),wkhtmltopdf(0.2)和wkhtmltopdf。并添加了wkhtmltopdf bin路径。它可以在我的本地系统中正常工作,这是我的代码

    url=""
    PDF = pdfkit.from_url(url, False)
    result= SendPDFtomail(PDF)
    return result 
    
  • 当我尝试通过添加wkhtmltopdf的buildpack和我的应用程序的buildpack将相同的代码推送到heroku中时,

      1. heroku/python
      2. https://github.com/dscout/wkhtmltopdf-buildpack.git
    
  • 当我将构建推送到heroku时

       git push heroku master
       Counting objects: 3, done.
       Delta compression using up to 4 threads.
       Compressing objects: 100% (3/3), done.
       Writing objects: 100% (3/3), 336 bytes | 84.00 KiB/s, done.
       Total 3 (delta 2), reused 0 (delta 0)
       remote: Compressing source files... done.
       remote: Building source:
       remote:
       remote: -----> Python app detected
       remote:  !     The latest version of Python 3.6 is python-3.6.6 (you 
       are using python-3.6.4, which is unsupported).
       remote:  !     We recommend upgrading by specifying the latest version 
       (python-3.6.6).
       remote:        Learn More: 
       https://devcenter.heroku.com/articles/python-runtimes
       remote: -----> Installing requirements with pip
       remote:
       remote: -----> wkhtmltopdf app detected
       remote: -----> Moving wkhtmltopdf binaries to /app/bin
       remote: -----> Discovering process types
       remote:        Procfile declares types -> web
       remote:
       remote: -----> Compressing...
       remote:        Done: 103.8M
       remote: -----> Launching...
       remote:        Released v19
       remote:        https://XXXXXXX.herokuapp.com/ deployed to 
       Heroku
       remote:
       remote: Verifying deploy... done.
    

我的应用已正确部署,在Heroku中运行代码时出现错误

      Exception :No wkhtmltopdf executable found: "b''"
      If this file exists please check that this process can read it. 
      Otherwise please install wkhtmltopdf - 
      https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

这与我没有将wkhtmltopdf路径添加到系统时收到的错误相同,正如我们在构建Moving wkhtmltopdf binaries to /app/bin中看到的那样,如何使Heroku配置为该wkhtmltopdf二进制路径?我们如何在Heroku中使用config vars来完成这项工作

我已经尝试过此generating-pdfs-wkhtmltopdf-heroku,但无法获取。无论我在哪里搜索到需要配置gem文件的红宝石解决方案,如何在python中完成

过去两天我一直在努力,请帮帮忙

预先感谢

Meghana Goud

1 个答案:

答案 0 :(得分:2)

我现在正在使用OSX和heroku来部署我的flask应用程序。 部署后,heroku wkhtmltopdf二进制文件位于./bin/wkhtmltopdf中。 所以我写了这段代码来解决这个问题。

if (platform.system() == 'Darwin'):
    config = pdfkit.configuration()
else:
    config = pdfkit.configuration(wkhtmltopdf='./bin/wkhtmltopdf')