Discussion:
Sending PDF from javascript to django
Joel Mathew
2018-11-14 09:15:36 UTC
Permalink
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
--
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/CAA%3Diw_-5iVw_QnGy5hPft-%3DzAi8y5wkvuJ4QKAXRazhZaYgtjw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Joel Mathew
2018-11-14 09:48:59 UTC
Permalink
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
Post by Joel Mathew
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.
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);
});
patient_id = int(patient_id)
print("POST data", request.POST)
cus = customer.objects.get(cstid = patient_id)
recipient=cus.email
[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.
/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
--
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/CAA%3Diw_9brsFs0KpQA0oCGg4zia1rYeJXOHz2QeK0%2Bp4rNsdSng%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Jason
2018-11-14 13:41:48 UTC
Permalink
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.
--
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.
Joel
2018-11-14 14:07:23 UTC
Permalink
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.
Post by Jason
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.
--
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
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
<https://groups.google.com/d/msgid/django-users/506231af-2c44-4e8c-9c9d-36d8ea913b7b%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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/CAA%3Diw_9QAr_Y2deWgjsXmZcmM8HALA3Gg0jDz_dZVtnSt9Vtog%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Matthew Pava
2018-11-14 14:40:09 UTC
Permalink
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.

From: django-***@googlegroups.com [mailto:django-***@googlegroups.com] On Behalf Of Joel
Sent: Wednesday, November 14, 2018 8:07 AM
To: django-***@googlegroups.com
Subject: Re: Sending PDF from javascript to django

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.

On Wed, 14 Nov, 2018, 7:12 PM Jason <***@gmail.com<mailto:***@gmail.com> wrote:
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.
--
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<mailto:django-users+***@googlegroups.com>.
To post to this group, send email to django-***@googlegroups.com<mailto: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<https://groups.google.com/d/msgid/django-users/506231af-2c44-4e8c-9c9d-36d8ea913b7b%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
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<mailto:django-users+***@googlegroups.com>.
To post to this group, send email to django-***@googlegroups.com<mailto: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/CAA%3Diw_9QAr_Y2deWgjsXmZcmM8HALA3Gg0jDz_dZVtnSt9Vtog%40mail.gmail.com<https://groups.google.com/d/msgid/django-users/CAA%3Diw_9QAr_Y2deWgjsXmZcmM8HALA3Gg0jDz_dZVtnSt9Vtog%40mail.gmail.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
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/95f81d5cad9e4abb87af8d794ca745e6%40iss2.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.
Pradeep Singh
2018-11-14 14:50:25 UTC
Permalink
help me to fix it...
Post by Matthew Pava
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.
*Sent:* Wednesday, November 14, 2018 8:07 AM
*Subject:* Re: Sending PDF from javascript to django
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.
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.
--
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
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
<https://groups.google.com/d/msgid/django-users/506231af-2c44-4e8c-9c9d-36d8ea913b7b%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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
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/CAA%3Diw_9QAr_Y2deWgjsXmZcmM8HALA3Gg0jDz_dZVtnSt9Vtog%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAA%3Diw_9QAr_Y2deWgjsXmZcmM8HALA3Gg0jDz_dZVtnSt9Vtog%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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
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/95f81d5cad9e4abb87af8d794ca745e6%40iss2.ISS.LOCAL
<https://groups.google.com/d/msgid/django-users/95f81d5cad9e4abb87af8d794ca745e6%40iss2.ISS.LOCAL?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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/CANwgZcaAgvCZMrtbdt8mQBBwETX9d_iar47bWe4NXV1NB8Y2Nw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Joel Mathew
2018-11-15 02:43:39 UTC
Permalink
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
Post by Matthew Pava
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.
*Sent:* Wednesday, November 14, 2018 8:07 AM
*Subject:* Re: Sending PDF from javascript to django
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.
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.
--
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
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
<https://groups.google.com/d/msgid/django-users/506231af-2c44-4e8c-9c9d-36d8ea913b7b%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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
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/CAA%3Diw_9QAr_Y2deWgjsXmZcmM8HALA3Gg0jDz_dZVtnSt9Vtog%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAA%3Diw_9QAr_Y2deWgjsXmZcmM8HALA3Gg0jDz_dZVtnSt9Vtog%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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
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/95f81d5cad9e4abb87af8d794ca745e6%40iss2.ISS.LOCAL
<https://groups.google.com/d/msgid/django-users/95f81d5cad9e4abb87af8d794ca745e6%40iss2.ISS.LOCAL?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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/CAA%3Diw_-0XNe%3DmRQwXFkneTzMUcjNr3NHEMoxXZtzhovcKDgvvw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...