Sending PDF from javascript to django

143 views
Skip to first unread message

Joel Mathew

unread,
Nov 14, 2018, 4:16:52 AM11/14/18
to django...@googlegroups.com
I use jspdf to generate pdf for downloading by end users. I wish to
add a function to let them send an email to themselves through the
server.
Reading a little bit, I've seen a method to send the pdf to the server
as a base encoded string, and then have the server decode it, before
emailing the object. It seems to be bad practise and a convoluted way,
to me.

Anyway, the javascript is doing this:

var pdf = doc.output();
cliniclabel = $("#TopPatientBar").data("cliniclabel")
patient_id = $('body').find("#PatientIP").html();
$.ajax({
method: "POST",
url: `/clinic/${cliniclabel}/prescription/sendemail/patient/${patient_id}`,
data: {data: pdf},
}).done(function(data){
console.log(data);
});

And as a preliminary step, I'm trying to read the POST data:

def SendPrescriptionbyMail(request, cliniclabel, patient_id):
patient_id = int(patient_id)
if request.method == 'POST':
print("POST data", request.POST)
cus = customer.objects.get(cstid = patient_id)
recipient=cus.email

But I got this:
[14/Nov/2018 14:39:27] "GET
/appointments/static/appointments/js/popper.min.js.map HTTP/1.1" 404
1749
[14/Nov/2018 14:39:30] "GET /clinic/medicines HTTP/1.1" 200 8389
2018-11-14 14:39:36,360 django.security.RequestDataTooBig ERROR
Request body exceeded settings.DATA_UPLOAD_MAX_MEMORY_SIZE.
2018-11-14 14:39:36,441 django.request WARNING Bad Request:
/clinic/madhav/prescription/sendemail/patient/18
[14/Nov/2018 14:39:36] "POST
/clinic/madhav/prescription/sendemail/patient/18 HTTP/1.1" 400 17930

But rather than create an XY problem, can anyone tell me whether I'm
approaching this in the right way? Isnt base encoding a pdf object
from javascript for sending to django the wrong way to do this?

What would be the right way? Just point me along what I need to be looking at.

Sincerely yours,

Joel G Mathew

Joel Mathew

unread,
Nov 14, 2018, 4:50:03 AM11/14/18
to django...@googlegroups.com
I tried the following:

def myhandle_uploaded_file(f):
with open('some/file/name.txt', 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)

def SendPrescriptionbyMail(request, cliniclabel, patient_id):
patient_id = int(patient_id)
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
myhandle_uploaded_file(request.FILES['file'])
return HttpResponseRedirect('/success/url/')

But I got the following error, which I cant make heads or tails of:

2018-11-14 15:16:49,585 django.request ERROR Internal Server Error:
/clinic/madhav/prescription/sendemail/patient/18
Traceback (most recent call last):
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/core/handlers/exception.py",
line 34, in inner
response = get_response(request)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/utils/deprecation.py",
line 93, in __call__
response = self.process_response(request, response)
File "/home/joel/myappointments/venv/lib/python3.6/site-packages/django/middleware/common.py",
line 105, in process_response
if response.status_code == 404:
AttributeError: 'str' object has no attribute 'status_code'

Sincerely yours,

Joel G Mathew

Jason

unread,
Nov 14, 2018, 8:41:48 AM11/14/18
to Django users
your original error was due to exceding django's max upload size.  check out https://docs.djangoproject.com/en/2.1/ref/settings/#data-upload-max-memory-size

also, you should check out https://djangopackages.org/grids/g/pdf/

What I would do is generate the pdf in the view from a rendered template and then send from email.  No need to generate it on the client and upload for an email.

Joel

unread,
Nov 14, 2018, 9:08:11 AM11/14/18
to django...@googlegroups.com
Ideally I would love to generate the pdf on the server. Hovered I am yet to discover an easy way to create a pdf easily with the ease of jspdf's table plugin.

Perhaps someone can shed light on a good python library to do this without much ado.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/506231af-2c44-4e8c-9c9d-36d8ea913b7b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matthew Pava

unread,
Nov 14, 2018, 9:41:04 AM11/14/18
to django...@googlegroups.com

There is django-hardcopy.

 

However, I’m in process of changing my PDF generation algorithm.  I originally used PhantomJS with gs-print and gs-view for Windows.  Unfortunately, PhantomJS has been discontinued, and it wasn’t taking advantage of rendering changes to HTML (especially for printing or PDF generation) that had developed over the years, especially with Google Chrome’s Blink engine, which used to formerly be Webkit, the same engine that PhantomJS was using.  My current goal is to utilize headless Chrome with Puppeteer, which is a NodeJS package.  Since PhantomJS was a NodeJS package, too, it shouldn’t be to involved to get a script working for Puppeteer.  And then I am guaranteed to always have the latest version of Chrome.

Pradeep Singh

unread,
Nov 14, 2018, 9:51:15 AM11/14/18
to django...@googlegroups.com
help me to fix it...

Screenshot (49).png

Joel Mathew

unread,
Nov 14, 2018, 9:44:45 PM11/14/18
to django...@googlegroups.com
Thank you. I decided to go with reportlab since it seems to be really powerful and doesnt require a headless browser. The options it has seem similiar (at least superficially) to the way I'm coding js in pdfjs. Thank you for your input. This was a XY problem after all. I should never have thought about generating PDF on client and then uploading to server to email the enduser.

Sincerely yours,

Joel G Mathew


On Wed, 14 Nov 2018 at 20:10, Matthew Pava <Matthe...@iss.com> wrote:
Reply all
Reply to author
Forward
0 new messages