|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +"""Python lib to print QR Platba (czech payment QR code) for fpdf users. |
| 4 | +This is alternative to the qrplatba module. |
| 5 | +install_requires = ['wfpdf', 'pyqrcode'] # wfpdf requires fpdf |
| 6 | +
|
| 7 | +Usage: |
| 8 | +See qrplatba() method doc. |
| 9 | +For the example see script call bellow (if __name__ == '__main__':). |
| 10 | +""" |
| 11 | + |
| 12 | +from datetime import date |
| 13 | +import os |
| 14 | +import tempfile |
| 15 | + |
| 16 | +import pyqrcode # pip install pyqrcode |
| 17 | +from wfpdf import PDF |
| 18 | + |
| 19 | + |
| 20 | +def qrplatba(pdf, |
| 21 | + IBAN, castka=None, mena='CZK', splatnost=None, msg=None, KS='0558', VS='', SS=None, IBAN2=None, |
| 22 | + w=36, x=None, y=None, **payment_more): |
| 23 | + """This is the main method which generates the QR platba code. |
| 24 | + pdf - object from: with PDF(<outputfilename>) as pdf: |
| 25 | + (we use wfpdf (which is really minor wrapper), |
| 26 | + we haven't not tested the calling with fpdf directly, but maybe it is easy) |
| 27 | + IBAN..IBAN2 - ACC, AM, CC, DT, MSG, X-KS, X-VS, X-SS, ALT-ACC attributes of payment |
| 28 | + w - width [mm] of the generated image (real width is a little bigger thanks to lines and 'QR platba' label) |
| 29 | + code is printed at current position (if not defined using x,y) |
| 30 | + current position moves to the end of 'QR platba' label |
| 31 | + **payment_more - any other payment attributes (key=value will generate *key:value , '_' in key changes to '-') |
| 32 | + """ |
| 33 | + if x is None: |
| 34 | + x = pdf.get_x() |
| 35 | + if y is None: |
| 36 | + y = pdf.get_y() |
| 37 | + |
| 38 | + qr = 'SPD*1.0*ACC:' + IBAN |
| 39 | + if IBAN2: |
| 40 | + qr += '*ALT-ACC:' + IBAN2 |
| 41 | + if castka: |
| 42 | + qr += '*AM:' |
| 43 | + try: |
| 44 | + qr += '%.2f' % castka |
| 45 | + except TypeError: |
| 46 | + qr += castka |
| 47 | + if mena: |
| 48 | + qr += '*CC:' + mena |
| 49 | + if splatnost: |
| 50 | + qr += '*DT:' |
| 51 | + try: |
| 52 | + qr += splatnost |
| 53 | + except TypeError: |
| 54 | + qr += date.strftime(splatnost, '%Y%m%d') |
| 55 | + if msg: |
| 56 | + qr += '*MSG:' + msg |
| 57 | + if KS: |
| 58 | + qr += '*X-KS:' + KS |
| 59 | + if VS: |
| 60 | + qr += '*X-VS:' + VS |
| 61 | + if SS: |
| 62 | + qr += '*X-SS:' + SS |
| 63 | + for key in payment_more: |
| 64 | + qr += '*%s:' % key.replace('_', '-') + payment_more[key] |
| 65 | + |
| 66 | + png, pixels = getQRCode(qr) |
| 67 | + # as long as we generate qr code with scale=1 w/pixels is module size in [mm]: |
| 68 | + module_size = 1.0 * w / pixels |
| 69 | + # for the line around we add 1* module_size to sure have good quiet zone |
| 70 | + pdf.image(png, x=x + module_size, y=y + module_size, w=w, type='png') |
| 71 | + os.unlink(png) |
| 72 | + |
| 73 | + pdf.set_font_size(6) |
| 74 | + # pdf.set_font('', style='B') # we don't set this here |
| 75 | + pllbl = 'QR platba' |
| 76 | + pllbl_w = pdf.get_string_width(pllbl) |
| 77 | + # some positions in next code were corrected (+-module_size) to obtain good result |
| 78 | + pllbl_right = x + pllbl_w + 7 * module_size |
| 79 | + square_size = w + 2 * module_size |
| 80 | + pdf.line(x, y, x + square_size, y) # top |
| 81 | + pdf.line(x, y, x, y + square_size) # left |
| 82 | + pdf.line(x + square_size, y, x + square_size, y + square_size) # right |
| 83 | + pdf.line(x, y + square_size, x + 2 * module_size, y + square_size) # bottom before |
| 84 | + pdf.line(pllbl_right, y + square_size, x + square_size, y + square_size) # bottom after |
| 85 | + pdf.set_xy(x + 3 * module_size, y + square_size) |
| 86 | + pdf.cell(pllbl_w, h=2, txt=pllbl) |
| 87 | + |
| 88 | +def getQRCode(txt): |
| 89 | + qrc = pyqrcode.create(txt) |
| 90 | + fullname = getTemppngName() |
| 91 | + qrc.png(fullname) # requires pypng installed |
| 92 | + return fullname, qrc.get_png_size() |
| 93 | + |
| 94 | +def getTemppngName(): |
| 95 | + with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as f: |
| 96 | + return f.name |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == '__main__': |
| 100 | + IBAN = 'CZ6255000000000001234567' |
| 101 | + castka = 2499 |
| 102 | + |
| 103 | + with PDF('test_qrplatba.pdf') as pdf: |
| 104 | + pdf.set_font('', style='B') |
| 105 | + qrplatba(pdf, IBAN, castka=2499) |
| 106 | + qrplatba(pdf, IBAN, castka='249.00', mena='USD', VS='2015111', msg='Smith J.', w=30, x=20, y=60, |
| 107 | + X_URL='HTTP://WWW.SOMEURL.COM/') |
0 commit comments