简体   繁体   English

如何使用Django输出PDF?

[英]How to Outputting PDFs with Django?

I am referring this https://docs.djangoproject.com/en/dev/howto/outputting-pdf/ for pdf generation with reportlab. 我指的是这个https://docs.djangoproject.com/en/dev/howto/outputing-pdf/,用于使用reportlab生成pdf。

def some_view(request):
    # Create the HttpResponse object with the appropriate PDF headers.
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'

    # Create the PDF object, using the response object as its "file."
    p = canvas.Canvas(response)

    # Draw things on the PDF. Here's where the PDF generation happens.
    # See the ReportLab documentation for the full list of functionality.
    p.drawString(100, 100, "Hello world.")

    # Close the PDF object cleanly, and we're done.
    logging.debug('-----p---')
    logging.debug(p)
    p.showPage()
    p.save()
    return response

If I add this reportlab folder in myapp then it is giving this error: 如果我在myapp中添加此reportlab文件夹,则出现此错误:

ViewDoesNotExist at /aps/print-pdf/
Could not import myapp.views. Error was: No module named reportlab

And if I put this reportlab folder out of myapp folder then it is giving this error: 而且,如果我将这个reportlab文件夹放在myapp文件夹之外,则会出现此错误:

ViewDoesNotExist at /aps/print-pdf/
Tried some_view in module myapp.views. Error was: 'HardenedModulesHook' object has no attribute '_files'

What is wrong in this? 这有什么问题? Please help. 请帮忙。

  1. check myapp is in your settings.INSTALLED_APPS? 检查myapp是否在您的设置中myapp
  2. check reportlab is successfully installed in your python environment, use pip freeze or run a python shell to see if import reportlab can work 检查reportlab是否已成功安装在python环境中,使用pip freeze或运行python shell来查看import reportlab可以正常工作

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM