QR code plugin for reportlab and RML language
Project description
Install
pip install reportlab_qr_code_generator
Why is this better than x?
Including image to PDF
Images are blurry.
reportlab-qrcode
Reportlab qrcode is vector QR code generator for reportlab. My project is better in some aspects:
Images can be directly added to Reportlab RML code.
Better rendering:
This library merges adjacent blocks to single area whihch produces image without gaps in every situation.
Smaller output
Hello world using reportlab-qrcode produces vector image with size 9 239 bytes. My code with produces only 1 984 bytes (78.5 reduction in size).
Customizable colors
Usage
This package allows insert QR codes to reportlab document.
This package can be used directly from reportlab RML file or from python code.
RML
To insert QR code from rml file use this code:
<plugInGraphic module="reportlab_qr_code" function="qr">parameters;format;contents</plugInGraphic>
Parameters is key=value list delimited using ‘,’ character, e.g. size=10cm,padding=1cm.
Format is either ‘text’ or ‘base64’ for simple text and base64 encoding. QR code contents is after second semicolon.
Complete example:
<illustration height="5cm" width="5cm" align="center">
<plugInGraphic module="reportlab_qr_code" function="qr">size=5cm,padding=0.5cm;text;Simple text</plugInGraphic>
</illustration>
Python API
QR code can be inserted to canvas using qr_draw(canvas, contents, **params) function.
from reportlab.pdfgen import canvas
from reportlab_qr_code import qr_draw
c = canvas.Canvas("out.pdf")
qr_draw(c, "Hello world", x="1cm", y="1cm", size="10cm")
Parameter list
Name |
Default |
Description |
---|---|---|
size |
5cm |
size of code |
padding |
2.5 |
padding size, without any unit this meanss 2.5 QR code pixels, it can be absolute value (like 1cm) or relative value (10%) |
fg |
black |
foreground color |
bg |
transparent |
background color |
version |
1 |
version passed to qr code library |
error_correction |
‘L’ |
error_correction passed to qr code library (can be L, M, Q or H) |
x |
0 |
x offset |
y |
0 |
y offset |
Examples
RML code:
<!DOCTYPE document SYSTEM "rml_1_0.dtd" [
<!ENTITY lines5 "
0cm 0cm 0cm 0.5cm
0cm 0cm 0.5cm 0cm
5cm 0cm 4.5cm 0cm
5cm 0cm 5cm 0.5cm
0cm 5cm 0.5cm 5cm
0cm 5cm 0cm 4.5cm
5cm 5cm 5cm 4.5cm
5cm 5cm 4.5cm 5cm
">
<!ENTITY lines3 "
0cm 0cm 0cm 0.5cm
0cm 0cm 0.5cm 0cm
3cm 0cm 2.5cm 0cm
3cm 0cm 3cm 0.5cm
0cm 3cm 0.5cm 3cm
0cm 3cm 0cm 2.5cm
3cm 3cm 3cm 2.5cm
3cm 3cm 2.5cm 3cm
">
]>
<document filename="test.pdf" invariant="1">
<template pagesize="21cm,29.7cm">
<pageTemplate id="main" pagesize="21cm,29.7cm">
<frame id="main" x1="1cm" y1="1cm" width="19cm" height="27.7cm"/>
</pageTemplate>
</template>
<stylesheet>
<paraStyle name="Normal" fontSize="12" leading="20" spaceBefore="40" />
</stylesheet>
<story>
<para style="Normal">Simple text </para>
<illustration height="5cm" width="5cm" align="center">
<plugInGraphic module="reportlab_qr_code" function="qr">;text;Simple text</plugInGraphic>
<lineMode width="0.5" /><lines>&lines5;</lines>
</illustration>
<condPageBreak height="7cm"/>
<para>Custom size</para>
<illustration height="3cm" width="3cm" align="center">
<plugInGraphic module="reportlab_qr_code" function="qr">size=3cm;text;Custom size</plugInGraphic>
<lineMode width="0.5" /><lines>&lines3;</lines>
</illustration>
<condPageBreak height="7cm"/>
<para>Base 64 encoded</para>
<illustration height="5cm" width="5cm" align="center">
<plugInGraphic module="reportlab_qr_code" function="qr">;base64;QmFzZSA2NCBlbmNvZGVk</plugInGraphic>
<lineMode width="0.5" /><lines>&lines5;</lines>
</illustration>
<condPageBreak height="7cm"/>
<para>Custom colors</para>
<illustration height="5cm" width="5cm" align="center">
<plugInGraphic module="reportlab_qr_code" function="qr">bg=#eeeeee,fg=#a00000;text;Custom colors</plugInGraphic>
<lineMode width="0.5" /><lines>&lines5;</lines>
</illustration>
<condPageBreak height="7cm"/>
<para>Padding 20%</para>
<illustration height="5cm" width="5cm" align="center">
<plugInGraphic module="reportlab_qr_code" function="qr">padding=20%;text;Padding 20%</plugInGraphic>
<lineMode width="0.5" /><lines>&lines5;</lines>
</illustration>
<condPageBreak height="7cm"/>
<para>Padding 1cm</para>
<illustration height="5cm" width="5cm" align="center">
<plugInGraphic module="reportlab_qr_code" function="qr">padding=1cm;text;Padding 1cm</plugInGraphic>
<lineMode width="0.5" /><lines>&lines5;</lines>
</illustration>
<condPageBreak height="7cm"/>
<para>Padding 1 pixel</para>
<illustration height="5cm" width="5cm" align="center">
<plugInGraphic module="reportlab_qr_code" function="qr">padding=1;text;Padding 1 pixel</plugInGraphic>
<lineMode width="0.5" /><lines>&lines5;</lines>
</illustration>
<condPageBreak height="7cm"/>
<para>Error correction M</para>
<illustration height="5cm" width="5cm" align="center">
<plugInGraphic module="reportlab_qr_code" function="qr">error_correction=M;text;Error correction</plugInGraphic>
<lineMode width="0.5" /><lines>&lines5;</lines>
</illustration>
<condPageBreak height="7cm"/>
<para>Error correction L</para>
<illustration height="5cm" width="5cm" align="center">
<plugInGraphic module="reportlab_qr_code" function="qr">error_correction=L;text;Error correction</plugInGraphic>
<lineMode width="0.5" /><lines>&lines5;</lines>
</illustration>
<para>Version 10</para>
<illustration height="5cm" width="5cm" align="center">
<plugInGraphic module="reportlab_qr_code" function="qr">version=10;text;Version 10</plugInGraphic>
<lineMode width="0.5" /><lines>&lines5;</lines>
</illustration>
</story>
</document>
Output:
Python code:
from reportlab.pdfgen import canvas
from reportlab_qr_code import qr_draw
def main():
c = canvas.Canvas("py.pdf")
qr_draw(c, "Hello world", "1cm", "1cm", size="19cm", bg="#eeeeee")
c.showPage()
c.save()
if __name__ == "__main__":
main()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for reportlab_qr_code_generator-1.2.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94a08b705e2b4e8573c60352a877213709279533002bb9848f9c5b3800f152bb |
|
MD5 | a8588f4479159072d1c35317b7b39108 |
|
BLAKE2b-256 | 0044a054100d2c60a70737f8df1b43cbd039b8759c86d90733e160952aecf0c0 |
Hashes for reportlab_qr_code_generator-1.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0fc85681b97a1b23881ddf52c2f181dc452b9db68d6c8400d76375c9fbeb076 |
|
MD5 | 8d6afc0240307f0f1d02dd49117c8fc7 |
|
BLAKE2b-256 | 3c29c250f114d93bc4c2ebd5940d66ff50008e6c334f8a170667f866ae6bcf6c |