Sunday, November 30, 2014

Re: PDF not saving on Django using open()


return response

This might work:

return response

Though, it would be more memory efficient to write it to a temporary file and then either redirect to that url or stream out the temporary file.


On Friday, November 28, 2014 5:06:14 AM UTC-5, George Rogers wrote:

trying to save files on static after pdfcrowd provides the file. I've tried adding the path to be specific it doesn't work.

from PyPDF2 import PdfFileMerger, PdfFileReader  from django.template import RequestContext  from django.shortcuts import render_to_response, get_object_or_404    import pdfcrowd  from django.http import HttpResponse  from wildling.models import *  from django.shortcuts import redirect    def home(request, template_name='brochures_form.html'):      context = RequestContext(request)      if request.method == 'POST':          context['name'] = name = request.POST.get('name','')          context['adults'] = adults = request.POST.get('adult','')          context['children'] = children = request.POST.get('child','')          context['infant'] = infant = request.POST.get('infant','')          context['start_date'] = start_date = request.POST.get('start-date','')          context['end_date'] = end_date = request.POST.get('end_date','')          context['country'] = country = request.POST.get('country','')          context['destination'] = destination = request.POST.get('destination','')          context['price'] = price = request.POST.get('price','')          context['background_photo'] = background_photo = request.POST.get('background-photo','')          context['from_user'] = from_user = request.POST.get('from','')          context['iteniary'] = iteniary = request.POST.get('iteniary','')          context['days'] = days = request.POST.get('days','')          context['destination'] = destination = request.POST.get('destination','')          context['property_name'] = property_name = request.POST.get('property','')          context['content'] = content = request.POST.get('content','')          context['iteniary'] = iteniary = request.POST.get('iteniary','')            #destinations and countries background photo created by not set          brochure = Brochures(name=name, ref_id="xxx", adults=adults, children=children, infant=infant,                      price=price, content=content, iteneary=iteniary)            request.session['br_id'] =          return redirect('')        return render_to_response(template_name, context, )        def generate_pdf(request, br_id, num):      context = RequestContext(request)      list_of_pages = [""+x+"/" for x in list("123456")]      pdfs = ["page"+x+".pdf" for x in list("123456")]      merger = PdfFileMerger()      client = pdfcrowd.Client("samuel254", "86e62657536d264e8217478d56abd72e")      br_id = response.session["br_id"]      brochure = Brochures.objects.get(br_id)        # set HTTP response headers      response = HttpResponse(mimetype="application/pdf")      response["Cache-Control"] = "max-age=0"      response["Accept-Ranges"] = "none"      response["Content-Disposition"] = "attachment; filename=document-output.pdf"        for num in list("123456"):          path = "/home/samuel/Documents/Documents/wilderness/wilderness/static/pdf/"          html = "{1}/page{0}/".format(num,br_id)          pdf = "page{0}.pdf".format(num)          pdf_file = client.convertURI(html)          local = open(pdf,'w')          local.write(pdf_file)          local.close()          merger.append(open(pdf, 'r+b'))        merger.write("document-output.pdf")      response.write(merger)      return response


from django.conf import settings  from django.conf.urls import patterns, include, url  from django.conf.urls.static import static  from django.views.generic import TemplateView    from django.contrib import admin  from views import generate_pdf,home    urlpatterns = patterns("",      url(r"^$", home, name="home"),      url(r"^(?P<br_id>d{4})/page(?P<num>[0-9]+)/", generate_pdf),        url(r"^admin/", include(,      url(r"^account/", include("account.urls")),  )    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

I need to confirm where the file is going, it's not crashing and returns the document-output.pdf but it's blank.

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
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment