简体   繁体   English

如何让 WKHTMLTOPDF 在 Heroku 上工作?

[英]How to get WKHTMLTOPDF working on Heroku?

I created a website which generates PDF using PDFKIT and I know how to install and setup environment variable path on Window.我创建了一个使用 PDFKIT 生成 PDF 的网站,我知道如何在 Window 上安装和设置环境变量路径。 I managed to deploy my first website on Heroku but now I'm getting error "No wkhtmltopdf executable found: "b''" When trying to generate the PDF. I have no idea, How to install and setup WKHTMLTOPDF on Heroku because this is first time I'm dealing with Linux. I really tried everything before asking this but even following this not working for me. Python 3 flask install wkhtmltopdf on heroku我设法在 Heroku 上部署了我的第一个网站,但现在我收到错误“找不到 wkhtmltopdf 可执行文件:”b''”尝试生成 PDF 时。我不知道如何在 Heroku 上安装和设置 WKHTMLTOPDF,因为这是我第一次与 Linux 打交道。在问这个问题之前,我真的尝试了一切,但即使按照这个对我也不起作用。Python 3 flask install wkhtmltopdf on heroku

If possible, please guide me with step by step on how to install and setup this.如果可能,请逐步指导我如何安装和设置它。

I followed all the resource and everything but couldn't make it work.我遵循了所有资源和所有内容,但无法使其正常工作。 Every time I get the same error.每次我都遇到同样的错误。 I'm using Django version 2. Python version 3.7.我正在使用 Django 版本 2。Python 版本 3.7。

This is what I get if I do heroku stack如果我做 heroku 堆栈,这就是我得到的

Available Stacks cedar-14 container heroku-16 * heroku-18可用堆栈 cedar-14 容器 heroku-16 * heroku-18

Error, I'm getting when generating the PDF.错误,我在生成 PDF 时遇到了错误。 No wkhtmltopdf executable found: "b''" If this file exists please check that this process can read it.未找到 wkhtmltopdf 可执行文件:"b''" 如果此文件存在,请检查此进程是否可以读取它。 Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf否则请安装 wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

My website works very well on localhost without any problem and as far as I know, I'm sure that I have done something wrong in installing wkhtmltopdf.我的网站在 localhost 上运行良好,没有任何问题,据我所知,我确定我在安装 wkhtmltopdf 时做错了什么。

Thank you谢谢

It's non-trivial .这是不平凡的 If you want to avoid all of the below's headache, you can just use my service, api2pdf: https://github.com/api2pdf/api2pdf.python .如果您想避免以下所有令人头疼的问题,您可以使用我的服务 api2pdf: https://github.com/api2pdf/api2pdf.python Otherwise, if you want to try and work through it, see below.否则,如果您想尝试解决它,请参见下文。

1) Add this to your requirements.txt to install a special wkhtmltopdf pack for heroku as well as pdfkit. 1) 将此添加到您的requirements.txt 以安装用于heroku 和pdfkit 的特殊wkhtmltopdf 包。

git+git://github.com/johnfraney/wkhtmltopdf-pack.git
pdfkit==0.6.1

2) I created a pdf_manager.py in my flask app. 2)我在我的烧瓶应用程序中创建了一个 pdf_manager.py。 In pdf_manager.py I have a method:在 pdf_manager.py 我有一个方法:

def _get_pdfkit_config():
     """wkhtmltopdf lives and functions differently depending on Windows or Linux. We
      need to support both since we develop on windows but deploy on Heroku.

     Returns:
         A pdfkit configuration
     """
     if platform.system() == 'Windows':
         return pdfkit.configuration(wkhtmltopdf=os.environ.get('WKHTMLTOPDF_BINARY', 'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'))
     else:
         WKHTMLTOPDF_CMD = subprocess.Popen(['which', os.environ.get('WKHTMLTOPDF_BINARY', 'wkhtmltopdf')], stdout=subprocess.PIPE).communicate()[0].strip()
         return pdfkit.configuration(wkhtmltopdf=WKHTMLTOPDF_CMD)

The reason I have the platform statement in there is that I develop on a windows machine and I have the local wkhtmltopdf binary on my PC.我在那里有平台声明的原因是我在 Windows 机器上开发,并且我的 PC 上有本地 wkhtmltopdf 二进制文件。 But when I deploy to Heroku, it runs in their linux containers so I need to detect first which platform we're on before running the binary.但是当我部署到 Heroku 时,它在他们的 linux 容器中运行,所以我需要在运行二进制文件之前首先检测我们在哪个平台上。

3) Then I created two more methods - one to convert a url to pdf and another to convert raw html to pdf. 3)然后我创建了另外两种方法 - 一种将 url 转换为 pdf,另一种将原始 html 转换为 pdf。

def make_pdf_from_url(url, options=None):
    """Produces a pdf from a website's url.
    Args:
        url (str): A valid url
        options (dict, optional): for specifying pdf parameters like landscape
            mode and margins
    Returns:
        pdf of the website
    """
    return pdfkit.from_url(url, False, configuration=_get_pdfkit_config(), options=options)

def make_pdf_from_raw_html(html, options=None):
    """Produces a pdf from raw html.
    Args:
        html (str): Valid html
        options (dict, optional): for specifying pdf parameters like landscape
            mode and margins
    Returns:
        pdf of the supplied html
    """
    return pdfkit.from_string(html, False, configuration=_get_pdfkit_config(), options=options)

I use these methods to convert to PDF.我使用这些方法转换为 PDF。

Just follow these steps to Deploy Django app(pdfkit) on Heroku:只需按照以下步骤在 Heroku 上部署 Django 应用程序(pdfkit):

Step 1: : Add following packages in requirements.txt file第一步:在requirements.txt文件中添加以下包

wkhtmltopdf-pack==0.12.3.0 
pdfkit==0.6.0

Step 2: Add below lines in the views.py to add path of binary file第 2 步:在 views.py 中添加以下几行以添加二进制文件的路径

import os, sys, subprocess, platform

if platform.system() == "Windows":
        pdfkit_config = pdfkit.configuration(wkhtmltopdf=os.environ.get('WKHTMLTOPDF_BINARY', 'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'))
else:
        os.environ['PATH'] += os.pathsep + os.path.dirname(sys.executable) 
        WKHTMLTOPDF_CMD = subprocess.Popen(['which', os.environ.get('WKHTMLTOPDF_BINARY', 'wkhtmltopdf')], 
            stdout=subprocess.PIPE).communicate()[0].strip()
        pdfkit_config = pdfkit.configuration(wkhtmltopdf=WKHTMLTOPDF_CMD)

Step 3: And then pass pdfkit_config as argument as below第 3 步:然后将 pdfkit_config 作为参数传递如下

pdf = pdfkit.from_string(html,False,options, configuration=pdfkit_config)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 3 flask在heroku上安装wkhtmltopdf - Python 3 flask install wkhtmltopdf on heroku 在heroku上托管时如何让fibit python工作? - How do I get fibit python working when hosted on heroku? 使用(Flask)pdfkit和wkhtmltopdf在heroku python应用程序中部署时,wkhtmltopdf不起作用 - wkhtmltopdf doesn't work when deployed in heroku python app using(Flask) pdfkit and wkhtmltopdf wkhtmltopdf 以非零代码 -9 退出。 错误。 我怎样才能摆脱这个错误? - wkhtmltopdf exited with non-zero code -9. error. How can I get rid of this error? 如何阻止 wkhtmltopdf.exe 弹出? - How to stop wkhtmltopdf.exe popping up? 如何在 Pythonanywhere 中安装 django-wkhtmltopdf。? - How to install django-wkhtmltopdf in Pythonanywhere.? 如何使 pdfkit(和 wkhtmltopdf)在 Google Colab 上工作? - How to make pdfkit (and wkhtmltopdf) work on Google Colab? python ffmpeg子进程在heroku上不起作用 - python ffmpeg subprocess not working on heroku Python(Heroku):烧瓶按钮不起作用 - Python (Heroku): Flask buttons not working Axios GET 请求在我的本地构建上工作,但在 Heroku 上失败 - 未捕获(承诺)错误:请求失败,状态码 500 - Axios GET request working on my local build but failing on Heroku - Uncaught (in promise) Error: Request failed with status code 500
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM