def export_to_pdf(request): try: if request.method == "POST": if "export_to_pdf" in request.POST: #called by the colorbox form = PdfTemplateForm(request.POST) if form.is_valid(): template_name = form.cleaned_data['template'] contacts = form.get_contacts() context = { "contacts": contacts, "search_dict": json.loads(form.cleaned_data['search_dict']), } context = form.patch_context(context) pdf_options = getattr(settings, 'BALAFON_PDF_OPTIONS', None) if pdf_options is None: cmd_options = { 'margin-top': 0, 'margin-bottom': 0, 'margin-right': 0, 'margin-left': 0, } else: cmd_options = pdf_options.get(template_name, {}) pdf_view = PDFTemplateView(filename='balafon.pdf', template_name=template_name, request=request, cmd_options=cmd_options) return pdf_view.render_to_response(context) else: return render(request, 'Search/export_to_pdf.html', {'form': form}) else: search_form = SearchForm(request.POST) if search_form.is_valid(): contacts = search_form.get_contacts() search_dict = json.dumps(search_form.serialize()) form = PdfTemplateForm(initial={ 'contacts': contacts, 'search_dict': search_dict }) return render(request, 'Search/export_to_pdf.html', {'form': form}) except Exception as msg: logger.exception("export_to_pdf") raise raise Http404
def export_to_pdf(request): try: if request.method == "POST": if "export_to_pdf" in request.POST: #called by the colorbox form = PdfTemplateForm(request.POST) if form.is_valid(): template_name = form.cleaned_data['template'] contacts = form.get_contacts() context = { "contacts": contacts, "search_dict": json.loads(form.cleaned_data['search_dict']), } context = form.patch_context(context) pdf_options = getattr(settings, 'BALAFON_PDF_OPTIONS', None) if pdf_options is None: cmd_options = {'margin-top': 0, 'margin-bottom': 0, 'margin-right': 0, 'margin-left': 0, } else: cmd_options = pdf_options.get(template_name, {}) pdf_view = PDFTemplateView( filename='balafon.pdf', template_name=template_name, request=request, cmd_options=cmd_options) return pdf_view.render_to_response(context) else: return render_to_response( 'Search/export_to_pdf.html', {'form': form}, context_instance=RequestContext(request) ) else: search_form = SearchForm(request.POST) if search_form.is_valid(): contacts = search_form.get_contacts() search_dict = json.dumps(search_form.serialize()) form = PdfTemplateForm(initial={'contacts': contacts, 'search_dict': search_dict}) return render_to_response( 'Search/export_to_pdf.html', {'form': form}, context_instance=RequestContext(request) ) except Exception, msg: logger.exception("export_to_pdf") raise
def put_on_paper(request, pk, kind="confirmation", fmt='html'): """ 'Print' was taken? """ order = get_object_or_404(Order, pk=pk) data = order.get_print_dict(kind) template = order.get_print_template(kind) if fmt == 'pdf': fn = data.get('title') + '.pdf' view = PDFTemplateView(request=request, template_name=template, filename=fn) return view.render_to_response(data) return render(request, template, data)