def render_to_pdf_response_pdfkit(template_name, context_dict, css_files=None): """Render to a PDF response using pdfkit `context_dict` is expected to be generated by htk.view_helpers.wrap_data PyPI: https://pypi.python.org/pypi/pdfkit Installation: https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server Outstanding Issues: - """ import pdfkit html = generate_html_from_template(template_name, context_dict) base_url = context_dict['request']['base_uri'] #html = rewrite_relative_urls_as_absolute(html, base_url) options = { 'page-size': 'Letter', 'orientation': 'Portrait', 'margin-top': '0.75in', 'margin-bottom': '0.75in', 'margin-left': '0.50in', 'margin-right': '0.50in', 'encoding': 'UTF-8', #'print_media_type' : False, #'title' : context_dict.get('title', 'PDF'), } pdf = pdfkit.from_string(html.encode('utf-8'), False, options=options, css=css_files) if pdf: response = HttpResponse(pdf, content_type='application/pdf') else: response = HttpResponseServerError('Error generating PDF file') return response
def render_to_pdf_response_pdfkit(template_name, context_dict, css_files=None): """Render to a PDF response using pdfkit `context_dict` is expected to be generated by htk.view_helpers.wrap_data PyPI: https://pypi.python.org/pypi/pdfkit Installation: https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server Outstanding Issues: - """ import pdfkit html = generate_html_from_template(template_name, context_dict) base_url = context_dict['request']['base_uri'] #html = rewrite_relative_urls_as_absolute(html, base_url) options = { 'page-size' : 'Letter', 'orientation' : 'Portrait', 'margin-top' : '0.75in', 'margin-bottom' : '0.75in', 'margin-left' : '0.50in', 'margin-right' : '0.50in', 'encoding' : 'UTF-8', #'print_media_type' : False, #'title' : context_dict.get('title', 'PDF'), } pdf = pdfkit.from_string(html.encode('utf-8'), False, options=options, css=css_files) if pdf: response = HttpResponse(pdf, content_type='application/pdf') else: response = HttpResponseServerError('Error generating PDF file') return response
def render_to_pdf_response_pisa(template_name, context_dict): """Render to a PDF response using Pisa Caveat: xhtml2pdf / pisa seems to not be well-maintained and does not handle CSS3 https://github.com/xhtml2pdf/xhtml2pdf/issues/44 PyPI: https://pypi.python.org/pypi/pisa/ """ import cStringIO as StringIO from xhtml2pdf import pisa html = generate_html_from_template(template_name, context_dict) result = StringIO.StringIO() pdf = pisa.pisaDocument(StringIO.StringIO(html.encode('utf-8')), result) if pdf: response = HttpResponse(result.getvalue(), mimetype='application/pdf') else: response = HttpResponseServerError('Error generating PDF file') return response