【问题标题】:How can I use 'pdfkit' in djngo?如何在 django 中使用“pdfkit”?
【发布时间】:2021-12-01 11:47:36
【问题描述】:

我想在我的 django 项目中生成 pdf。我使用 pdfkit 模块将 html 页面转换为 pdf 文件。我使用此功能,但它有错误,例如: 操作系统错误 异常值:
找不到 wkhtmltopdf 可执行文件:“b''” 如果此文件存在,请检查此进程是否可以读取它,或者您可以在方法调用中手动将路径传递给它,检查自述文件。否则请安装 wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

  def customer_render_pdf_view(request, *args, **kwargs):
     pk = kwargs.get('pk')
     customer = get_object_or_404(Person, pk=pk)
     enrolment = get_object_or_404(Enrollment,pk=pk)  
     template_path = 'test.html'
     context = {'customer': customer, 'enrolment':enrolment}    
     template = get_template(template_path)
     html = template.render(context)    
     pdf = pdfkit.from_string(html, False)
     response = HttpResponse(pdf, content_type='application/pdf' )
     response['Content-Disposition'] = 'filename= "report.pdf"'
     return response

【问题讨论】:

    标签: python django pdfkit


    【解决方案1】:

    你可以这样使用

    导入报告实验室和 pdf

    【讨论】:

      【解决方案2】:
      from django.shortcuts import render, redirect, get_object_or_404
      from django.http import HttpResponse
      from django.template.loader import get_template
      import pdfkit
      from .models import Buses
          def pdf(request, id):
             bus = Buses.objects.get(id=id)
             template = get_template('buses/pdf.html')
             html = template.render({'bus': bus})
             options = {
                'page-size': 'Letter',
                'encoding': "UTF-8",
             }
             config = 
             pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf') 
              #use your actual location here
      
      
             pdf = pdfkit.from_string(html, False, configuration=config, 
             options=options)
             response = HttpResponse(pdf, content_type='application/pdf')
             response['Content-Disposition'] = 'attachment; filename=" 
             {}_{}.pdf"'.format(bus.company, bus.name)
         return response
      

      【讨论】:

        猜你喜欢
        • 2017-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-14
        • 2020-02-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多