PermissionError at /generate-pdf [Errno 13] Permission denied weasyprint

1.1k views Asked by At

hello am using weasyprint to generate a pdf whats wrong with my code here getting access denied.

def generate_pdf(request):
    """Generate pdf."""
    # Model data
    student = Student.objects.all().order_by('last')

    # Rendered
    html_string = render_to_string('pdf-output.html', {'student': student})
    html = HTML(string=html_string)
    result = html.write_pdf()

    # Creating http response
    response = HttpResponse(content_type='application/pdf;')
    response['Content-Disposition'] = 'inline; filename=list_os_students.pdf'
    response['Content-Transfer-Encoding'] = 'binary'
    with tempfile.NamedTemporaryFile(delete=True) as output:
        output.write(result)
        output.flush()
        output = open(output.name, 'rb')
        response.write(output.read())

    return response

If i change the line output = open(output.name, 'rb') to output = open(output.seek(0), 'rb') the page loads to infinity while in console printing

[22/Oct/2020 11:36:54] "GET /dashboard HTTP/1.1" 200 84413
Not Found: /assets/media/avatars/avatar15.jpg
[22/Oct/2020 11:36:54] "GET /assets/media/avatars/avatar15.jpg HTTP/1.1" 404 2917
Not Found: /assets/media/avatars/avatar2.jpg
[22/Oct/2020 11:36:54] "GET /assets/media/avatars/avatar2.jpg HTTP/1.1" 404 2914
Not Found: /assets/media/avatars/avatar1.jpg
Not Found: /assets/media/avatars/avatar13.jpg
[22/Oct/2020 11:36:54] "GET /assets/media/avatars/avatar1.jpg HTTP/1.1" 404 2914
Not Found: /assets/media/avatars/avatar11.jpg
[22/Oct/2020 11:36:54] "GET /assets/media/avatars/avatar13.jpg HTTP/1.1" 404 2917
[22/Oct/2020 11:36:54] "GET /assets/media/avatars/avatar11.jpg HTTP/1.1" 404 2917

Traceback

Traceback (most recent call last):
  File "C:\Users\cipher\Python\Django\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\cipher\Python\Django\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\cipher\Python\Django\sis\dashboard\views.py", line 72, in generate_pdf
    output = open(output.name, 'rb')

Exception Type: PermissionError at /generate-pdf
Exception Value: [Errno 13] Permission denied: 'C:\\Users\\cipher\\AppData\\Local\\Temp\\tmp996pqacl'

1

There are 1 answers

1
cipher On BEST ANSWER

After struggling for hours,I have been able to escape the error, hope it helps others too.I replaced the line output = open(output.name, 'rb') with line output.seek(0) so looks like

 response = HttpResponse(content_type='application/pdf;')
    response['Content-Disposition'] = 'inline; filename=list_of_students.pdf'
    response['Content-Transfer-Encoding'] = 'binary'
    with tempfile.NamedTemporaryFile(delete=True) as output:
        output.write(result)
        output.flush()
        output.seek(0)
        response.write(output.read())

    return response

Personally I don't know why and I preferred the solution to have access not denied as asked on the question,If you happen to have answer or explanation share please.