【问题标题】:the filename of pdf file doesnt work correctly with wkhtmltopdfpdf 文件的文件名不能与 wkhtmltopdf 一起正常工作
【发布时间】:2021-07-01 05:55:48
【问题描述】:

我的 Django 项目中有一个下载按钮,我可以在其中以 pdf 格式导出某个日期的报告。在我的装有 Linux 的笔记本电脑上一切正常,但是当我在我们公司的本地服务器中设置项目时,显示的文件名没有日期。 这是我的代码:

  template_name = 'pdf.html'
    template = get_template(template_name)
    html = template.render({"data": data, "date":date, "index":index})

    if 'DYNO' in os.environ:
        print('loading wkhtmltopdf path on heroku')
        WKHTMLTOPDF_CMD = subprocess.Popen(
            ['which', os.environ.get('WKHTMLTOPDF_BINARY', 'wkhtmltopdf-pack')],
            stdout=subprocess.PIPE).communicate()[0].strip()
    else:
        print('loading wkhtmltopdf path on localhost')
        WKHTMLTOPDF_CMD = ('/usr/local/bin/wkhtmltopdf/bin/wkhtmltopdf')

    config = pdfkit.configuration(wkhtmltopdf=WKHTMLTOPDF_CMD)
    options = {
        'margin-bottom': '10mm',
        'footer-center': '[page]'
    }
    pdf = pdfkit.from_string(html, False, configuration=config, options=options)
    response = HttpResponse(pdf, content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="otchet-{}.pdf"'.format(date)
    return response

本地下载时,文件名是-otchet-2021-06-30.pdf 但在服务器上,它看起来像 - otchet%20.pdf 我不知道如何解决它...

【问题讨论】:

    标签: django filenames wkhtmltopdf


    【解决方案1】:

    只是一个想法。使用 f 字符串。

        response['Content-Disposition'] = f'attachment; filename="otchet-{date}.pdf"'
    

    【讨论】:

      猜你喜欢
      • 2022-06-13
      • 1970-01-01
      • 2020-09-07
      • 2020-08-14
      • 1970-01-01
      • 2022-06-10
      • 2014-04-27
      • 2017-01-17
      • 2011-06-30
      相关资源
      最近更新 更多