Django过滤器可以使用PDFKIT来生成PDF

发布于 2025-01-25 05:18:17 字数 4956 浏览 2 评论 0原文

我正在尝试使用以下方式生成pdfkit:

view.py

def view_PDF(request, id):
    sub = get_object_or_404(Student, id=id)
    sub_fil = Subject.objects.filter(student_name = request.user)
    context = {
        "sub": sub.id,
        "sub_no": sub.sub_no,
        "book": sub.book,
        'sub_fil':sub_fil,
    }
    return render(request, 'school/pdf_template.html', context)

def generate_PDF(request, id):
    # Use False instead of output path to save pdf to a variable
    pdf = pdfkit.from_url(request.build_absolute_uri(reverse('student-detail', args=[id])), False)
    response = HttpResponse(pdf,content_type='application/pdf')
    response['Content-Disposition'] = 'filename="student.pdf"' 
    return response

当我省略此行sub_fil = sub_fil = subs_fil = object.objects.filter(student_name = request.user)它在没有该行的信息的情况下运行良好显示一些信息非常重要。使用该行运行服务器时,我会遇到此问题:

OSError at /student-download/2
wkhtmltopdf reported an error:
Loading pages (1/6)
[>                                                           ] 0%
[======>                                                     ] 10%
[==============================>                             ] 50%
Error: Failed to load http://127.0.0.1:8000/student-detail/2, with network status code 299 and http status code 500 - Error downloading http://127.0.0.1:8000/student-detail/2 - server replied: Internal Server Error
[============================================================] 100%
Counting pages (2/6)                                               
[============================================================] Object 1 of 1
Resolving links (4/6)                                                       
[============================================================] Object 1 of 1
Loading headers and footers (5/6)                                           
Printing pages (6/6)
[>                                                           ] Preparing
[============>                                               ] Page 1 of 5
[========================>                                   ] Page 2 of 5
[====================================>                       ] Page 3 of 5
[================================================>           ] Page 4 of 5
[============================================================] Page 5 of 5
Done                                                                      
Exit with code 1 due to network error: UnknownContentError
Request Method: GET
Request URL:    http://127.0.0.1:8000/student-download/2
Django Version: 3.0
Exception Type: OSError
Exception Value:    
wkhtmltopdf reported an error:
Loading pages (1/6)
[>                                                           ] 0%
[======>                                                     ] 10%
[==============================>                             ] 50%
Error: Failed to load http://127.0.0.1:8000/invoice-detail/2, with network status code 299 and http status code 500 - Error downloading http://127.0.0.1:8000/student-detail/2 - server replied: Internal Server Error
[============================================================] 100%
Counting pages (2/6)                                               
[============================================================] Object 1 of 1
Resolving links (4/6)                                                       
[============================================================] Object 1 of 1
Loading headers and footers (5/6)                                           
Printing pages (6/6)
[>                                                           ] Preparing
[============>                                               ] Page 1 of 5
[========================>                                   ] Page 2 of 5
[====================================>                       ] Page 3 of 5
[================================================>           ] Page 4 of 5
[============================================================] Page 5 of 5
Done                                                                      
Exit with code 1 due to network error: UnknownContentError
Exception Location: C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\pdfkit\pdfkit.py in to_pdf, line 156
Python Executable:  C:\Users\user\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.0
Python Path:    
['D:\\Django\\DJANGO',
 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python39\\lib',
 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python39',
 'C:\\Users\\user\\AppData\\Roaming\\Python\\Python39\\site-packages',
 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages']
Server time:    Fri, 29 Apr 2022 13:12:14 +0000

请帮助我如何解决此问题,因为我仍然是初学者

I am trying to generate pdfkit using this way:

view.py

def view_PDF(request, id):
    sub = get_object_or_404(Student, id=id)
    sub_fil = Subject.objects.filter(student_name = request.user)
    context = {
        "sub": sub.id,
        "sub_no": sub.sub_no,
        "book": sub.book,
        'sub_fil':sub_fil,
    }
    return render(request, 'school/pdf_template.html', context)

def generate_PDF(request, id):
    # Use False instead of output path to save pdf to a variable
    pdf = pdfkit.from_url(request.build_absolute_uri(reverse('student-detail', args=[id])), False)
    response = HttpResponse(pdf,content_type='application/pdf')
    response['Content-Disposition'] = 'filename="student.pdf"' 
    return response

When I omit this line sub_fil = Subject.objects.filter(student_name = request.user) it works well without information from the subject but that line is very important to display some information. When running the server with that line I got this problem:

OSError at /student-download/2
wkhtmltopdf reported an error:
Loading pages (1/6)
[>                                                           ] 0%
[======>                                                     ] 10%
[==============================>                             ] 50%
Error: Failed to load http://127.0.0.1:8000/student-detail/2, with network status code 299 and http status code 500 - Error downloading http://127.0.0.1:8000/student-detail/2 - server replied: Internal Server Error
[============================================================] 100%
Counting pages (2/6)                                               
[============================================================] Object 1 of 1
Resolving links (4/6)                                                       
[============================================================] Object 1 of 1
Loading headers and footers (5/6)                                           
Printing pages (6/6)
[>                                                           ] Preparing
[============>                                               ] Page 1 of 5
[========================>                                   ] Page 2 of 5
[====================================>                       ] Page 3 of 5
[================================================>           ] Page 4 of 5
[============================================================] Page 5 of 5
Done                                                                      
Exit with code 1 due to network error: UnknownContentError
Request Method: GET
Request URL:    http://127.0.0.1:8000/student-download/2
Django Version: 3.0
Exception Type: OSError
Exception Value:    
wkhtmltopdf reported an error:
Loading pages (1/6)
[>                                                           ] 0%
[======>                                                     ] 10%
[==============================>                             ] 50%
Error: Failed to load http://127.0.0.1:8000/invoice-detail/2, with network status code 299 and http status code 500 - Error downloading http://127.0.0.1:8000/student-detail/2 - server replied: Internal Server Error
[============================================================] 100%
Counting pages (2/6)                                               
[============================================================] Object 1 of 1
Resolving links (4/6)                                                       
[============================================================] Object 1 of 1
Loading headers and footers (5/6)                                           
Printing pages (6/6)
[>                                                           ] Preparing
[============>                                               ] Page 1 of 5
[========================>                                   ] Page 2 of 5
[====================================>                       ] Page 3 of 5
[================================================>           ] Page 4 of 5
[============================================================] Page 5 of 5
Done                                                                      
Exit with code 1 due to network error: UnknownContentError
Exception Location: C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\pdfkit\pdfkit.py in to_pdf, line 156
Python Executable:  C:\Users\user\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.0
Python Path:    
['D:\\Django\\DJANGO',
 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python39\\lib',
 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python39',
 'C:\\Users\\user\\AppData\\Roaming\\Python\\Python39\\site-packages',
 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages']
Server time:    Fri, 29 Apr 2022 13:12:14 +0000

Please help how can I fix this problem for I am still a beginner

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文