def generate_report(report): print "report: %r" % (report,) abs_path = os.path.join("reports", "{}.html.mako".format(report["report"])) if os.path.exists(abs_path): source = Template(filename=abs_path).render(report=report) else: source = "<h1>There is no report</h1>" # TODO: create the tmp directory html_file = os.path.join("tmp", "{}.html".format(report["uuid"])) pdf_file = os.path.join("tmp", "{}.pdf".format(report["uuid"])) # TODO: Remove the temporary file with open(html_file, "w") as fp: fp.write(source) wkhtmltopdf = WKhtmlToPdf(html_file, pdf_file) wkhtmltopdf.render() action = report.get("action", None) if action and "smtp" in action: smtp_config = action["smtp"] message = mailer.Message(From=smtp_config["from"], To=smtp_config["to"], Subject=smtp_config["subject"]) message.Body = """This Report has been generated by printus""" message.attach(pdf_file) sender = mailer.Mailer("smtp.brutele.be") sender.send(message) return True
def generate_pdf(self): filename = os.path.join(settings.VAR_ROOT, "uploads", slugify(self.name)) + ".pdf" html = self.html #need to get a template here pdf = WKhtmlToPdf( url, filename, orientation='Landscape', ) result = pdf.render() return result
def pdf_view(request, document_id): username = '******' password = '******' http = httplib2.Http() url = "http://%s/login/" % settings.BILLING_APP_HOST body = {'username': username, 'password': password} headers = {'Content-type': 'application/x-www-form-urlencoded'} resp, content = http.request(url, 'POST', headers=headers, body=urllib.urlencode(body)) #log.debug("resp: %s" % resp.__dict__) cookie = resp['set-cookie'].split(';')[0].replace('=', ' ') document = get_object_or_404(Document, pk=document_id) url = "http://%s/harvard_doc/html/%s/" % (settings.DOCUMENT_APP_HOST, str(document_id)) pdf_file = "/var/www/harvard_doc/uploads/%s.pdf" % slugify(document.name) #log.debug("pdf_file: %s" % pdf_file) pdf = WKhtmlToPdf( url, pdf_file, cookie=cookie, orientation='Landscape', ) pdf.render() pdf_data = open(pdf_file, "rb").read() #log.debug("pdf_data: %s" % pdf_data) response = HttpResponse(pdf_data, content_type='application/pdf') filename = "%s.pdf" % slugify(document.name) response['Content-Disposition'] = 'attachment; filename="%s"' % filename return response