ELHADEF
ELHADEF

Reputation: 106

Problem when generating pdf (BytesIO / canvas): return incomprehensives characters in web browser instead of uploading pdf

I habe deploy my django app on pythonanywhere but have an error with export in excel

my code works except on pythonanywhere

I had FileWrapper but it doesn't work. Instead of openning a pdf files, it display this:

%PDF-1.3 %���� ReportLab Generated PDF document http://www.reportlab.com 1 0 obj << /F1 2 0 R >> endobj 2 0 obj << /BaseFont /Helvetica /Encoding /WinAnsiEncoding /Name /F1 /Subtype /Type1 /Type /Font >> endobj 3 0 obj << /BitsPerComponent 8 /ColorSpace /DeviceRGB /Filter [ /ASCII85Decode /FlateDecode ] /Height 161 /Length 23591 /Subtype /Image /Type /XObject /Width 277 >> stream Gb"/lG>qMqp@&$+mJ#IcpYKseYrD#T/D+V;,#rq('Lbh!6)5rc,/#BI!bn(rcJB*TQN"8&/+nC0RJHn;**\]eIJ;;2J`WmRS&:*c_k6E@a@)8:qK(4Bq94Ym6^k*l<@!l@u&m6nPY[E;=(`uka6l(^l/6F)(`Wm81OTjbI$^SkcnUUj:AAS&VJgI^1Hsjc'=YN%D@#6EO+'>a8(5rbT=<\-m#8SkQ"lf[mQH4[JJH\\W_>(ft-*gp+@)3IY0Ku^KM(BJc=Kk1/N"7IA+q)Bq(+bROir][TaF`r5"\WCBpqVJ-ZMq<-N/W3+q=aD6p^^#GSK3,!m=5,S^k0O(IgJ;]"cTV8QDoqsP)5F$G:ASk+`DV'\=H?8[mPrHAG]\,F1'-[G=@:5^,Jtd1fd!KHZV2(cL*!pJgH$JI!f["KIFrb8XF6tbDM\_e?5,"rn]iuManl7S\-Vb3HFdWU=T7oXnD_,Zh]s"Rt%uTiLfkQr,R;QJ*q7N-$Jmp+7GZm@*?;_c6)NDlC>gdJd"e(rQ4gC%1OW9*=lKH59i)d*kctfoX9%k%1g-8D00Rj_W[!ZNY.n-:L_GZh+R2)?M\WefQb&I)&Q0C/UP'Yq(p.SO*CSS$?Dc@l[]6*Hh=5p"2^CN3\B%V6J.TL;;qtXTeZQopil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[!UJqFY*&o?[7p^m^J`*dlD263B0k%p].h\EClOB2[T:Dj@_l.I0QJD?-<

how to fix this issue?

views.py

from django.db.models import Q
from django.utils.translation import ugettext as _
import time
import io
from django.http import FileResponse
from reportlab.pdfgen import canvas
from datetime import datetime
from django.utils import timezone
from parameters.models import Thesaurus
# http://www.learningaboutelectronics.com/Articles/How-to-create-an-update-view-with-a-Django-form-in-Django.php
from django.contrib.auth.decorators import login_required
from django.contrib.auth.decorators import permission_required
# from django.contrib import messages
from werkzeug.wsgi import FileWrapper
from randomization.models import Preinclusion, Randomisation, Medicament, ListeRandomisation, Randomiser, is_randomized, is_randomizable, reattribuer_medicament, email
from parameters.models import Profile, Pays, Region, Site, Thesaurus
from .forms import RandomisationForm, ReallocationForm
from django.conf import settings
from django.views.decorators.csrf import csrf_exempt

# création du pdf de résultat de la randomisation ou de la réattribution d'un traitement
# paramètre get['type'] (1/2) en fonction du type d'édition
# 1 : randomisation / 2 : reallocation
@login_required
def pdf(request):
    # Create a file-like buffer to receive PDF data.
    buffer = io.BytesIO()

    # Create the PDF object, using the buffer as its "file."
    p = canvas.Canvas(buffer)
...
    # FileResponse sets the Content-Disposition header so that browsers
    # present the option to save the file.
    buffer.seek(0)

    return FileResponse(FileWrapper(buffer), as_attachment=True, filename=form + ' ' + pat +'.pdf')

Upvotes: 0

Views: 839

Answers (1)

Nicolas Appriou
Nicolas Appriou

Reputation: 2331

It seems that the response is missing a Content-Type in your response.

return FileResponse(FileWrapper(buffer), content_type='application/pdf')

The FileWrapper is used to send the content of the PDF in chunks. So you need to manually set the Content-Type and Content-Length response headers.

But in your case, since the content of the PDF is already in memory (inside your buffer variable), you should be able to just pass the buffer into the response. You don't really need to load it in chunks as FileWrapper does.

Upvotes: 1

Related Questions