def download(self, data): if kid: data = """<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://purl.org/kid/ns#"> <head> <title>PDF Demo</title> </head> <body>%s</body> </html>""" % data test = kid.Template(source=data) data = test.serialize(output='xhtml') result = StringIO.StringIO() pdf = pisa.CreatePDF( StringIO.StringIO(data), result ) if pdf.err: return "We had some errors in HTML" else: cp.response.headers["content-type"] = "application/pdf" return result.getvalue()
def render(request, template, payload): """This populates the site wide template context in the payload passed to the template. It the job of this methods to make sure that, if user want to see the PDF they are able to see it. """ if request.GET.get('pdf', ''): tarr = template.split('/') template = '%s/%s/%s' % (tarr[0], 'pdf', tarr[1]) template = get_template(template) html = template.render(Context(payload)) import copy hsoup = soup.BeautifulSoup(html) links = hsoup.findAll('a') for link in links: if not link['href'].startswith('http'): link['href'] = '%s%s' % (defaults.base_url, link['href']) html = StringIO.StringIO(str(hsoup)) result = StringIO.StringIO() pdf = pisa.CreatePDF(html, result) if pdf.err: return HttpResponse(pdf.log) return HttpResponse(result.getvalue(), mimetype='application/pdf') if not payload.get('subs', ''): try: subs = request.user.subscribeduser_set.all() payload.update({'subs': subs}) except AttributeError, e: pass
def html_to_pdf(htmldata, baseurl="", account=""): """ Converts HTML content to PDF and returns the PDF file data. Uses pisa for the conversion. """ try: import sx.pisa3 as pisa except: al.error("trying to convert html to pdf, pisa not found.", "utils.html_to_pdf", None) return "" header = "<!DOCTYPE HTML>\n<html><head><style type='text/css'>\n\n</style>" header += '<meta http-equiv="content-type" content="text/html; charset=utf-8">\n' header += "</head><body>" footer = "</body></html>" htmldata = htmldata.replace("font-size: xx-small", "font-size: 6pt") htmldata = htmldata.replace("font-size: x-small", "font-size: 8pt") htmldata = htmldata.replace("font-size: small", "font-size: 10pt") htmldata = htmldata.replace("font-size: medium", "font-size: 14pt") htmldata = htmldata.replace("font-size: large", "font-size: 18pt") htmldata = htmldata.replace("font-size: x-large", "font-size: 24pt") htmldata = htmldata.replace("font-size: xx-large", "font-size: 36pt") htmldata = fix_relative_document_uris(htmldata, baseurl, account) fin = StringIO(header + str(htmldata) + footer) fout = StringIO() pdf = pisa.CreatePDF(fin, fout) if pdf.err: al.error("errors found converting html to pdf.", "utils.html_to_pdf", None) else: return fout.getvalue()
def decorated(func, *args, **kw): output = func(*args, **kw) dst = StringIO.StringIO() result = pisa.CreatePDF(StringIO.StringIO(output), dst) if not result.err: cherrypy.response.headers["Content-Type"] = content_type if filename: cherrypy.response.headers[ "Content-Disposition"] = "attachment; filename=" + filename output = dst.getvalue() return output
def html_to_pdf(html): html = to_utf8(html) import sx.pisa3 as pisa import StringIO dst = StringIO.StringIO() result = pisa.CreatePDF(html, dst) if not result.err: pdf = dst.getvalue() dst.close() return pdf else: return None
def workings(self): from StringIO import StringIO # filename = gen_file_name('workings','.pdf') data = ''' <!doctype html> <head> <title>Workings</title> </head> <body> {} </body> '''.format(cherrypy.session['workings']) result = StringIO() pdf = pisa.CreatePDF(StringIO(data), result, '/') if pdf.err: return 'A conversion error occured' else: cherrypy.response.headers['content-type'] = 'application/pdf' return result.getvalue()
def decorated(func, *args, **kw): def kwpop(default, *names): for name in names: if name in kw: return kw.pop(name) return default # get the output from the decorated function output = func(*args, **kw) dst = StringIO.StringIO() result = pisa.CreatePDF(StringIO.StringIO(output), dst) # print cherrypy.url("index.html") if not result.err: cherrypy.response.headers["Content-Type"] = content_type if filename: cherrypy.response.headers[ "Content-Disposition"] = "attachment; filename=" + filename output = dst.getvalue() return output