Friday, November 28, 2014

PDF not saving on Django using open()

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)          brochure.save()            request.session['br_id'] = brochure.id          return redirect('http://wilderness.maasaimara.com/generate/')        return render_to_response(template_name, context, )        def generate_pdf(request, br_id, num):      context = RequestContext(request)      list_of_pages = ["http://127.0.0.1:8000/page"+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 = "http://wilderness.maasaimara.com/{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

The urls.py

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(admin.site.urls)),      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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cfc4823d-1ee3-475d-be96-8e183b01830e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment