如何在Django项目中正确合并wkhtmltopdf来下载PDF?

问题描述 投票:0回答:1

我有一个 Django 项目,其中包含一个 HTML,我想将其下载为 PDF。我正确下载了wkhtmltopdf可执行文件并将其注册到settings.py中。但是,当我尝试下载 PDF 时,出现此错误:

No wkhtmltopdf executable found: "b''"
If this file exists please check that this process can read it or you can pass path to it manually in method call, check README. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

这些是一些代码片段:

views.py:

@login_required
def download_resume(request):
    # Retrieve data from your Django models
    personal_info = personalinfo.objects.filter(user=request.user).last()
    summaries = summary.objects.filter(user=request.user).last()
    experiences = experience.objects.filter(user=request.user)
    educations = education.objects.filter(user=request.user)
    certs = certificates.objects.filter(user=request.user)
    skillset = skills.objects.filter(user=request.user)

    # Generate the PDF content using a template
    template = get_template('template0.html')
    context = {
        'personal_info': personal_info,
        'summaries': summaries,
        'experiences': experiences,
        'educations': educations,
        'certs': certs,
        'skillset': skillset
    }

    # Render the template with the context data
    html_content = template.render(context)

    # Convert HTML to PDF using wkhtmltopdf
    pdf = pdfkit.from_string(html_content, False)

    # Create a response with the PDF content
    response = FileResponse(pdf, content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="resume.pdf"'

    return response

设置.py:

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

WKHTMLTOPDF_BIN_PATH = r'C:\Users\lulu\PycharmProjects\builderproject\wkhtmltopdf\binwkhtmltopdf.exe'

我确保 wkhtmltopdf 已下载并在设置中注册,但是当我单击下载按钮时,我仍然收到错误。可能是什么原因导致此错误?

python django wkhtmltopdf pdfkit
1个回答
0
投票

您遇到的错误消息表明,即使您已配置 WKHTMLTOPDF_BIN_PATH 设置,Django 也无法找到 wkhtmltopdf 可执行文件。以下是排除和解决问题的一些步骤:

  • 验证可执行路径:仔细检查 wkhtmltopdf 的路径 可执行文件是正确的。在您的settings.py中,您提供了路径 如下:
WKHTMLTOPDF_BIN_PATH = r'C:\Users\lulu\PycharmProjects\builderproject\wkhtmltopdf\bin\wkhtmltopdf.exe'
  • 确保该路径指向正确的位置
    wkhtmltopdf
    在您的系统上可执行。
  • 使用绝对路径:建议不要使用相对路径 使用
    wkhtmltopdf
    可执行文件的绝对路径。绝对的 路径从根目录(例如 C:)开始,并且不依赖于 当前工作目录。修改你的
    WKHTMLTOPDF_BIN_PATH
    就像 这个:
WKHTMLTOPDF_BIN_PATH = r'C:\Users\lulu\PycharmProjects\builderproject\wkhtmltopdf\bin\wkhtmltopdf.exe'
  • 检查权限:确保
    wkhtmltopdf
    可执行文件具有 执行适当的权限。确保该文件是 不是只读的,并且运行 Django 应用程序的用户具有 对文件的执行权限
  • 测试可执行文件:打开命令提示符或终端并尝试 使用简单的命令直接运行
    wkhtmltopdf
    可执行文件 确认它正在工作。例如:

"C:\Users\lulu\PycharmProjects\builderproject\wkhtmltopdf\bin\wkhtmltopdf.exe" --version

如果此命令有效,则意味着可执行文件已正确安装并且可以访问。

  • 重新启动服务器:更改设置后,重新启动 您的 Django 开发服务器,以确保新设置生效 效果。
  • 使用
    pdfkit
    配置:而不是指定
    WKHTMLTOPDF_BIN_PATH
    在Django的设置中,可以配置
    pdfkit
    直接在您的视图函数中。例如:

    import pdfkit

    def download_resume(request):
        # ... your existing code ...

        # Configure pdfkit with the path to wkhtmltopdf
        pdfkit_config = pdfkit.configuration(wkhtmltopdf='C:/Users/lulu/PycharmProjects/builderproject/wkhtmltopdf/bin/wkhtmltopdf.exe')

        # Convert HTML to PDF using wkhtmltopdf and the configured path
        pdf = pdfkit.from_string(html_content, False, configuration=pdfkit_config)

        # ... your existing code ...

这样,您就不需要在 Django 设置中设置

WKHTMLTOPDF_BIN_PATH

按照这些步骤操作后,尝试再次下载 PDF,它应该可以正常工作,而不会出现“找不到 wkhtmltopdf 可执行文件”错误。确保您已安装

pdfkit
(
pip install pdfkit
) 并在项目中正确配置。

© www.soinside.com 2019 - 2024. All rights reserved.