def file_generation(): if not 'number' in session: return '404' else: number = session['number'] session.clear() filename_zip = 'output/light-nanosecond_ruler{}.zip'.format(number) filename = 'light-nanosecond_ruler{}{}.{}' pdf = HTML(string=render_template( 'certificate.html', ruler=ruler.generate_string(number, 'pdf'), bg=get_background_img(), **get_content_numbers(number) )) with ZipFile(filename_zip, 'w', ZIP_DEFLATED) as zip_file: zip_file.writestr(filename.format(number, '-laser', 'svg'), ruler.generate_string(number, 'laser')) zip_file.writestr(filename.format(number, '-certificate', 'pdf'), pdf.write_pdf()) @after_this_request def remove_file(response): try: removeFile(filename_zip) except Exception as error: app.logger.error("Error removing or closing downloaded file handle", error) return response return send_file(filename_zip, as_attachment=True)
def create_pdf_for_templated_letter(self, encrypted_letter_data): letter_details = current_app.encryption_client.decrypt( encrypted_letter_data) current_app.logger.info( f"Creating a pdf for notification with id {letter_details['notification_id']}" ) logo_filename = f'{letter_details["logo_filename"]}.svg' if letter_details[ 'logo_filename'] else None template = LetterPrintTemplate( letter_details['template'], values=letter_details['values'] or None, contact_block=letter_details['letter_contact_block'], # letter assets are hosted on s3 admin_base_url=current_app.config['LETTER_LOGO_URL'], logo_file_name=logo_filename, ) with current_app.test_request_context(''): html = HTML(string=str(template)) try: pdf = BytesIO(html.write_pdf()) except WeasyprintError as exc: self.retry(exc=exc, queue=QueueNames.SANITISE_LETTERS) cmyk_pdf = convert_pdf_to_cmyk(pdf) page_count = get_page_count(cmyk_pdf.read()) cmyk_pdf.seek(0) try: # If the file already exists in S3, it will be overwritten if letter_details["key_type"] == "test": bucket_name = current_app.config['TEST_LETTERS_BUCKET_NAME'] else: bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME'] s3upload( filedata=cmyk_pdf, region=current_app.config['AWS_REGION'], bucket_name=bucket_name, file_location=letter_details["letter_filename"], ) current_app.logger.info( f"Uploaded letters PDF {letter_details['letter_filename']} to {bucket_name} for " f"notification id {letter_details['notification_id']}") except BotoClientError: current_app.logger.exception( f"Error uploading {letter_details['letter_filename']} to pdf bucket " f"for notification {letter_details['notification_id']}") return notify_celery.send_task(name=TaskNames.UPDATE_BILLABLE_UNITS_FOR_LETTER, kwargs={ "notification_id": letter_details["notification_id"], "page_count": page_count, }, queue=QueueNames.LETTERS)
def print_letter_template(): """ POST /print.pdf with the following json blob { "letter_contact_block": "contact block for service, if any", "template": { "template data, as it comes out of the database" } "values": {"dict of placeholder values"}, "filename": {"type": "string"} } """ json = get_and_validate_json_from_request(request, preview_schema) filename = f'{json["filename"]}.svg' if json['filename'] else None template = LetterPrintTemplate( json['template'], values=json['values'] or None, contact_block=json['letter_contact_block'], # letter assets are hosted on s3 admin_base_url=current_app.config['LETTER_LOGO_URL'], logo_file_name=filename, ) html = HTML(string=str(template)) pdf = BytesIO(html.write_pdf()) cmyk_pdf = convert_pdf_to_cmyk(pdf) response = send_file(cmyk_pdf, as_attachment=True, attachment_filename='print.pdf') response.headers['X-pdf-page-count'] = get_page_count(cmyk_pdf.read()) cmyk_pdf.seek(0) return response
def email_invoice(inv): print(current_user.userid) print(inv) invoice = getInvoice(inv).get_json() if(invoice['success']): payee = User.query.filter_by(userid=invoice['invoice']['receiverid']).first() sender = User.query.filter_by(userid=invoice['invoice']['senderid']).first() if payee.email: print('payee email: ', payee.email) if current_user.phone: username=current_user.username swish_qr_base64=swishQRbase64(sender.phone, invoice['invoice']['amount'], invoice['invoice']['description']) html = HTML(string=render_template('invoice_pdf_template.html', username=sender.username, invoice=invoice['invoice'], qrCode_base64=swish_qr_base64, css1=url_for('static', filename='invoice_pdf/boilerplate.css'), css2=url_for('static', filename='invoice_pdf/main.css'), css3=url_for('static', filename='invoice_pdf/normalize.css'))) pdf = io.BytesIO(html.write_pdf()) msg = Message("Du har blivit fakkad :)", sender="*****@*****.**", recipients=[payee.email]) msg.body = "Du har blivit fakkad, se bifogat" msg.attach('invoice.pdf', 'application/pdf', data=pdf.read()) mail.send(msg) return {'message' : "Email sent!"} else: return {'message' : "You have no phone number registred which is needed to genarate swish QRcode :("} else: return {'message' : "Payee has no email address registered :("} else: return {'message' : invoice['message']}
def save_pdf(string_html, path): path_dir = os.path.dirname(path) if path_dir and not os.path.exists(path_dir): os.makedirs(path_dir) obj_html = HTML(string=string_html) data_pdf = obj_html.write_pdf() with open(path, 'wb') as fh: fh.write(data_pdf) return data_pdf
def generate_pdf(pdf_data): """ Generate a PDF from a string of data. :param pdf_data: String of data to input into PDF. :return: PDF File object """ html = HTML(string=pdf_data) f = html.write_pdf() return f
def exportPdf(template): font_config = FontConfiguration() THIS_FILE_DIR = os.path.dirname(os.path.abspath(__file__)) + os.sep base_url = 'file://' + THIS_FILE_DIR css = CSS(string=''' /* dropdown.css */ @import url(https://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300); @prim: #53e3a6; @font-face { font-family: 'Source Sans Pro'; font-style: normal; font-weight: 200; src: url(https://fonts.gstatic.com/s/sourcesanspro/v14/6xKydSBYKcSV-LCoeQqfX1RYOo3i94_wlxdr.ttf) format('truetype'); } @font-face { font-family: 'Source Sans Pro'; font-style: normal; font-weight: 300; src: url(https://fonts.gstatic.com/s/sourcesanspro/v14/6xKydSBYKcSV-LCoeQqfX1RYOo3ik4zwlxdr.ttf) format('truetype'); } .dropdown-content hover{ background-color:#3f6b68; } .dropdown { position: relative; display: inline-block; } .dropdown-select{ font-family: 'Source Sans Pro', sans-serif; color: white; margin-bottom: 20px; background-color: #75CDB6; border: 1px solid rgba(255, 255, 255, 0.4); border-radius: 3px; font-size: 16px; font-weight: 300; padding: 10px 0px 10px 0px; width: 250px; transition: border .5s; text-align: left; } .dropdown-select:hover{ background-color: #75CDB6; } .dropdown-select:focus{ background-color: #75CDB6; width: 300px; color: white; } .dropdown-type{ margin-bottom: 20px; /*align-content: left;*/ color: #75CDB6; background-color: red; border: 2px solid transparent; border-radius: 3px; font-size: 16px; font-weight: 200; padding: 10px 0px 10px 90px; width: 230px; transition: border .5s; text-align: left; margin-left: -5%; } .dropdown-content { display: none; position: absolute; font-family: Didact Gothic; color: #75CDB6; background-color: #75CDB6; min-width: 160px; overflow: auto; z-index: 2; } .dropdown-content a { background-color: #75CDB6; color: #75CDB6; padding: 12px 16px; text-decoration: none; display: block; } .dropdown a:hover { background-color: #75CDB6; } .resizing_select { font-family: 'Source Sans Pro', sans-serif; margin-bottom: 20px; align-content: center; background-color: #75CDB6; border: 2px solid transparent; border-radius: 3px; font-size: 17px; font-weight: 200; padding: 3px 0px 3px 3px; transition: border .5s; color: white; } .resizingSelectEditMark { font-family: 'Source Sans Pro', sans-serif; align-content: center; text-align: center; border: 1px solid rgba(255, 255, 255, 0.4); background-color: rgba(255, 255, 255, 0.2); border-radius: 3px; font-size: 18px; font-weight: 300; padding: 4px 0px 4px 4px; margin: 0 0 10px 0; transition: border .5s; color: white; height: 41px; } .resizingSelectEditMark:hover{ background-color: rgba(255, 255, 255, 0.4); } .resizingSelectEditMark:focus{ background-color: #75CDB6; color: white; } #width_tmp_select{ display : none; } .text{ font-family: 'Source Sans Pro', sans-serif; font-size: 19px; font-weight: 200; } /* end of dropdown.css */ /* form.css */ @import url(https://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300); @prim: #53e3a6; *{ box-sizing: border-box; margin: 0; padding: 0; font-weight: 300; } .customLabel{ text-align: left; margin-left: 29%; } .fa-eye:before { content: "\f06e"; } .fa-eye-slash:before { content: "\f070"; } .customLabel2{ text-align: left; margin-left: 29%; } .control-group { margin-bottom: 20px; } .control-group2 { margin-bottom: 8px; } .control-group3{ margin-bottom: 20px; text-align: left; height: 300px; padding: 0 0.4em 0.4em 0; } @font-face { font-family: 'Source Sans Pro'; font-style: normal; font-weight: 200; src: url(https://fonts.gstatic.com/s/sourcesanspro/v14/6xKydSBYKcSV-LCoeQqfX1RYOo3i94_wlxdr.ttf) format('truetype'); } @font-face { font-family: 'Source Sans Pro'; font-style: normal; font-weight: 300; src: url(https://fonts.gstatic.com/s/sourcesanspro/v14/6xKydSBYKcSV-LCoeQqfX1RYOo3ik4zwlxdr.ttf) format('truetype'); } * { box-sizing: border-box; margin: 0; padding: 0; font-weight: 300; } body { font-family: 'Source Sans Pro', sans-serif; color: white; font-weight: 300; } body ::-webkit-input-placeholder { /* WebKit browsers */ font-family: 'Source Sans Pro', sans-serif; color: white; font-weight: 300; } body :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ font-family: 'Source Sans Pro', sans-serif; color: white; opacity: 1; font-weight: 300; } body ::-moz-placeholder { /* Mozilla Firefox 19+ */ font-family: 'Source Sans Pro', sans-serif; color: white; opacity: 1; font-weight: 300; } body :-ms-input-placeholder { /* Internet Explorer 10+ */ font-family: 'Source Sans Pro', sans-serif; color: white; font-weight: 300; } .wrapper { z-index: 1; background: linear-gradient(to bottom right, #50a3a2 0%, #53e3a6 100%); background-position: center; background-repeat: no-repeat; background-size: cover; position: absolute; top: 0; left: 0; width: 100%; height: 100%; overflow: hidden; } .wrapperError404{ z-index: 1; background: linear-gradient(to bottom right, #50a3a2 0%, #53e3a6 100%); background-position: center; background-repeat: no-repeat; background-size: cover; position: absolute; top: 0; left: 0; width: 100%; height: 100%; overflow: hidden; } .wrapperMarks { z-index: 1; background: linear-gradient(to bottom right, #50a3a2 0%, #53e3a6 100%) no-repeat center; background-size: cover; position: absolute; top: 0; left: 0; width: 100%; height: 100%; overflow: scroll; } .wrapper.form-success .container h1 { transform: translateY(85px); } .container { max-width: 600px; margin: 0 auto; padding: 250px 0; height: 400px; text-align: center; } .containerPageNotFound{ position: absolute; top: 35%; left: 45%; margin-top: -50px; margin-left: -50px; height: 100px; text-align: center; z-index: 2; } .container-marks { max-width: 600px; margin: -120px auto; padding: 250px 0; height: 400px; text-align: center; } .container-editMarks{ max-width: 600px; margin: -120px 900px 0px auto; padding: 250px 0; height: 400px; text-align: center; } .container-absence{ max-width: 600px; margin: -120px 900px 0px auto; padding: 250px 0; height: 400px; text-align: center; } .container-student { max-width: 600px; margin: -150px auto; padding: 250px 0; height: 400px; text-align: center; } .container-laboratories { max-width: 600px; margin: -150px auto; padding: 250px 0; height: 400px; text-align: center; } .containerExportedData{ max-width: 600px; margin: -150px auto; padding: 250px 0; height: 400px; text-align: center; z-index: 2; } .container h1 { font-size: 40px; transition-duration: 1s; transition-timing-function: ease-in-put; font-weight: 200; } form { padding: 20px 0; position: relative; z-index: 2; } .form-student{ margin-top: -130px; } .login-button{ color: #50a3a2; background-color: #ECF0F1; } .login-button:hover{ color: white; background-color: #53e3a6; } form input { -webkit-appearance: none; -moz-appearance: none; appearance: none; outline: 0; border: 1px solid rgba(255, 255, 255, 0.4); background-color: rgba(255, 255, 255, 0.2); width: 250px; border-radius: 3px; padding: 10px 15px; margin: 0 auto 10px auto; display: block; text-align: center; font-size: 18px; color: white; transition-duration: 0.25s; font-weight: 300; } form input:hover { background-color: rgba(255, 255, 255, 0.4); } form input:focus { background-color: #47c7a8; width: 300px; color: white; } form button { -webkit-appearance: none; -moz-appearance: none; appearance: none; outline: 0; background-color: #ff0000; border: 0; padding: 10px 15px; color: white; border-radius: 3px; width: 250px; cursor: pointer; font-size: 18px; transition-duration: 0.25s; } form button:hover { background-color: #f5f7f9; } .bg-bubbles { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 1; } .bg-bubbles li{ position: absolute; list-style: none; display: block; width: 40px; height: 40px; background-color: rgba(255, 255, 255, 0.15); bottom: -160px; -webkit-animation: square 25s infinite; animation: square 25s infinite; transition-timing-function: linear; } .bg-bubbles li:nth-child(1) { left: 10%; } .bg-bubbles li:nth-child(2) { left: 20%; width: 80px; height: 80px; -webkit-animation-delay: 2s; animation-delay: 2s; -webkit-animation-duration: 17s; animation-duration: 17s; } .bg-bubbles li:nth-child(3) { left: 25%; -webkit-animation-delay: 4s; animation-delay: 4s; } .bg-bubbles li:nth-child(4) { left: 40%; width: 60px; height: 60px; -webkit-animation-duration: 22s; animation-duration: 22s; background-color: rgba(255, 255, 255, 0.25); } .bg-bubbles li:nth-child(5) { left: 70%; } .bg-bubbles li:nth-child(6) { left: 80%; width: 120px; height: 120px; -webkit-animation-delay: 3s; animation-delay: 3s; background-color: rgba(255, 255, 255, 0.2); } .bg-bubbles li:nth-child(7) { left: 32%; width: 160px; height: 160px; -webkit-animation-delay: 7s; animation-delay: 7s; } .bg-bubbles li:nth-child(8) { left: 55%; width: 20px; height: 20px; -webkit-animation-delay: 15s; animation-delay: 15s; -webkit-animation-duration: 40s; animation-duration: 40s; } .bg-bubbles li:nth-child(9) { left: 25%; width: 10px; height: 10px; -webkit-animation-delay: 2s; animation-delay: 2s; -webkit-animation-duration: 40s; animation-duration: 40s; background-color: rgba(255, 255, 255, 0.3); } .bg-bubbles li:nth-child(10) { left: 90%; width: 160px; height: 160px; -webkit-animation-delay: 11s; animation-delay: 11s; } @-webkit-keyframes square { 0% { transform: translateY(0); } 100% { transform: translateY(-700px) rotate(600deg); } } @keyframes square { 0% { transform: translateY(0); } 100% { transform: translateY(-700px) rotate(600deg); } } .span{ color: red; } .show{ position: relative; right: -110px; top: -40px; } .input_upload{ -webkit-appearance: none; -moz-appearance: none; appearance: none; outline: 0; border: 1px solid rgba(255, 255, 255, 0.4); background-color: rgba(255, 255, 255, 0.2); width: 270px; border-radius: 3px; padding: 10px 15px; margin: 0 auto 10px auto; display: block; text-align: center; font-size: 18px; color: white; transition-duration: 0.25s; font-weight: 300; } .hiddenLabel{ color: #53D0A5; } .h1align{ text-align: right; } .error404{ margin-top: 300px; text-align: center; } .redirect{ font-size: 30px; } /* end of form.css */ /* sidenav.css */ .sidenav { height: 100%; width: 0px; position: fixed; z-index: 3; top: 0; left: 0; background-color: #111; overflow-x: hidden; transition: 0.5s; padding-top: 0px; } .sidenav a { padding: 8px 8px 8px 32px; text-decoration: none; font-size: 25px; color: #818181; display: block; transition: 0.3s; } .sidenav a:hover { color: #f1f1f1; } .sidenav .closebtn { position: absolute; top: 0; right: 25px; font-size: 36px; margin-left: 50px; } @media screen and (max-height: 450px) { .sidenav {padding-top: 15px;} .sidenav a {font-size: 18px;} } #mySidenav{ padding-top: 40px; } .menu{ z-index:2; font-size: 30px; cursor:pointer; color: #000000; position: absolute; padding: 50px 50px 50px 50px; } /* end of sidenav.css */ /* table.css */ table { font-family: 'Source Sans Pro', sans-serif; border-collapse: collapse; table-layout: auto; width: 100%; letter-spacing: 1px; } table td { border: 1px solid #ccc; } td, th { border: 1px solid #dddddd; padding: 10px; } th{ background: #25a37e; font-weight: bold; } td { text-align: center; } #deleteBin{ margin: -20px 0px -23px 107px; } /* end of table.css */ ''', font_config=font_config, base_url=base_url) html = HTML(string=template) html.write_pdf(target='exports/export.pdf', stylesheets=[css])
def test_wrappers(self): with app.test_request_context(base_url='http://example.org/bar/'): # HTML can also be used with named parameters only: html = HTML(url='http://example.org/bar/foo/') css = CSS(url='http://example.org/bar/static/style.css') assert html.write_pdf(stylesheets=[css]).startswith(b'%PDF')
def save_pdf(html, path): obj_html = HTML(string=html) data_pdf = obj_html.write_pdf() with open(path, 'wb') as fh: fh.write(data_pdf) return redirect(url_for('cv'))