Created
January 8, 2021 15:13
-
-
Save Zulbukharov/db5238ded84bd5f4684d0e8a4d36b848 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import tempfile | |
| from reportlab.graphics import renderPDF | |
| from reportlab.pdfgen import canvas | |
| from reportlab.lib.pagesizes import letter, A4 | |
| from reportlab.pdfbase import pdfmetrics | |
| import qrcode | |
| import qrcode.image.svg | |
| from svglib.svglib import svg2rlg | |
| from reportlab.pdfbase.ttfonts import TTFont | |
| from reportlab.pdfbase.pdfmetrics import stringWidth | |
| import cStringIO | |
| FONT_NAME = 'Arial' | |
| FONT_SIZE = 72 | |
| pdfmetrics.registerFont(TTFont(FONT_NAME, 'Arial.ttf')) | |
| def int_to_str(num): | |
| alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
| new_str = '' | |
| while num > 0: | |
| new_str = str(alphabet[num % 26]) + new_str | |
| num //= 26 | |
| return new_str | |
| point = 1 | |
| inch = 600 | |
| def make_qr_code_drawing(data, size): | |
| qr = qrcode.QRCode( | |
| version=2, # QR code version a.k.a size, None == automatic | |
| error_correction=qrcode.constants.ERROR_CORRECT_H, # lots of error correction | |
| box_size=size, # size of each 'pixel' of the QR code | |
| border=4 # minimum size according to spec | |
| ) | |
| qr.add_data(data) | |
| qrcode_svg = qr.make_image(image_factory=qrcode.image.svg.SvgPathFillImage) | |
| svg_file = tempfile.NamedTemporaryFile() | |
| qrcode_svg.save(svg_file) # store as an SVG file | |
| svg_file.flush() | |
| qrcode_rl = svg2rlg(svg_file.name) # load SVG file as reportlab graphics | |
| svg_file.close() | |
| return qrcode_rl | |
| def convert_y(y, h): | |
| return h - y | |
| def center(w, text_width): | |
| return (w - text_width) / 2 | |
| def make_pdf_file(output_filename, url): | |
| qrcode_rl = make_qr_code_drawing(url, 60) | |
| c = canvas.Canvas(output_filename, pagesize=letter) | |
| w, h = letter | |
| renderPDF.draw(qrcode_rl, c, 30, h - 650) | |
| c.setStrokeColorRGB(0,0,0) | |
| c.setFillColorRGB(0,0,0) | |
| c.setFont(FONT_NAME, FONT_SIZE) | |
| data_width = stringWidth(url, FONT_NAME, FONT_SIZE) | |
| c.drawString( center(w, data_width), 100, url ) | |
| text = "Анкета" | |
| text_width = stringWidth(text, FONT_NAME, FONT_SIZE) | |
| c.drawString(center(w, text_width), convert_y(80, h), (text).encode("utf-8")) | |
| c.showPage() | |
| c.save() | |
| if __name__ == "__main__": | |
| output = cStringIO.StringIO() | |
| make_pdf_file(output, f"ashyq.kz/{int_to_str(6000)}") | |
| pdf_out = output.getvalue() | |
| output.close() | |
| response = make_response(pdf_out) | |
| response.headers['Content-Disposition'] = "attachment; filename='d" | |
| response.mimetype = 'application/pdf' | |
| return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment