Package: reportlab-qr-code-generator
Installpipinstallreportlab_qr_code_generatorExample outputWhy is this better than x?Including image to PDFImages are blurry.reportlab-qrcodeReportlab 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 outputFirst paragraph of Lorem ipsum products using reportlab-qrcode vector image with
size 181 418 bytes.  My code with produces only 34 131 bytes (81% reduction in
size).Customizable colorsUsageThis package allows insert QR codes to reportlab document.This package can be used directly from reportlab RML file or from python code.RMLTo insert QR code from rml file use this code:<plugInGraphicmodule="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:<illustrationheight="5cm"width="5cm"align="center"><plugInGraphicmodule="reportlab_qr_code"function="qr">size=5cm,padding=0.5cm;text;Simpletext</plugInGraphic></illustration>Python APIQR code can be inserted to canvas usingqr_draw(canvas, contents, **params)function.fromreportlab.pdfgenimportcanvasfromreportlab_qr_codeimportqr_drawc=canvas.Canvas("out.pdf")qr_draw(c,"Hello world",x="1cm",y="1cm",size="10cm")Command line interfaceThis module can be used as standalone command to generate PDF document.Example:python-mreportlab_qr_code"Content"--outfileqr.pdfContent argument is optional. Without this argument, command will read from
stdin. Output file is optional too, without argument, command will write to
stdout.Arguments:--outfileOutput file or stdout if omitted--base64Base64 encoded text--compressPDF compression (default enabled)--no-compressDisable compression--versionQR code version--error_correctionError correction strength (L, M, Q or H)--sizeCode size (e.g. 10cm)--paddingPadding around code (e.g. 1cm or 1 for one pixel or 5%)--fgForeground color--bgBackground color--invertInvert--negativeInstead of invert bits, inverts whole image--radiusRound code (radius)--enhanced-pathEnhanced path rendering--no-enhanced-pathDisable path enhancement--gradientEither"linear x1 y1 x2 y2 colors"or"radial x y radius colors"Dimensions are in range (0, 1), position (0, 0) is top left corner, (1, 1) is bottom right corner. Colors is list"[position] color"e.g."0.0 #ffffff 1.0 #000000". Position is optional. Without position argument, distances are calculated automatically. Example:--gradient"linear 0.0 0.0 0.1 1.0 0.5 \#1050c0 0.3 \#1050c0 0.7 \#e0e000"--holeCoordinates in formx:y:w:h. Allowed are absolute length units, relative units (%) and pixels (without unit suffix).--drawSelect area to draw. Possuble values are:'all','eye[1-3]','eyes','eyepupil[1-3]','eyepupils','eyeball[1-3]','eyeballs','align','alignpupils','alignballs'. It’s possible to combine operations with +/- symbol e.g. all-eyes-align. To show only eye1 and eye3 without pupil it’s possible to write something likeeye1+eye3-eyepupil3. Arguments passed before first draw are globally set. Arguments after draw are specific for preceding draw call.Some crazy examples:# 1python-mreportlab_qr_code"Padding 1cm"\--outfileqr.pdf\--error_correctionL\--size10cm\--padding1cm\--radius0.5\--enhanced-path\--gradient"linear 0 1 1 0 0.1 \#ff0000 0.9 \#0000ff"# 2python-mreportlab_qr_code"Padding 1cm"\--outfileqr.pdf\--error_correctionL\--size10cm\--padding1cm\--radius3.5\--gradient"linear 1 0 0 1 0.1 \#ff0000 0.9 \#0000ff"# 3python-mreportlab_qr_code"OPENSOURCE"\--outfileqr.pdf\--size10cm\--padding1cm\--radius1.5\--bg"\#ddddcc"\--fg="\#665510"# 4python-mreportlab_qr_code"Padding 1cm"\--outfileqr.pdf\--error_correctionL\--size10cm\--padding1cm\--radius3.5\--enhanced-path\--gradient"linear 0 1 1 0 0.1 \#ff0000 0.9 \#0000ff"Parameter listParametersNameDefaultDescriptionsize5cmsize of codepadding2.5padding size, without any unit this meanss 2.5 QR code pixels, it can be
absolute value (like 1cm) or relative value (10%)fgblackforeground colorbgtransparentbackground colorinvertFalseinvert pixel valuesmaskFalserender only masknegativeFalserender negative of codeversion1version passed to qr code libraryerror_correction‘L’error_correction passed to qr code library (can be L, M, Q or H)x0x offsety0y offsethole[]list of holes in formx:y:w:h…(can be repeated)draw+allselect elements to draw. Prefix + (plus) means include, - (minus)
exclude. Allowed options are:'all','eye[1-3]','eyes','eyepupil[1-3]','eyepupils','eyeball[1-3]','eyeballs','align','alignpupils'and'alignballs'ExamplesPython examle:fromreportlab.pdfgenimportcanvasfromreportlab_qr_codeimportqr_drawdefmain():c=canvas.Canvas("py.pdf")qr_draw(c,"Hello world",x="1cm",y="1cm",size="19cm",bg="#eeeeee")c.showPage()c.save()if__name__=="__main__":main()RML document example:<!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
">]><documentfilename="test.pdf"invariant="1"compression="1"><template><pageTemplateid="main"pagesize="17cm,39cm"><frameid="main"x1="0.5cm"y1="0.0cm"width="5cm"height="39cm"/><frameid="main"x1="6cm"y1="0.0cm"width="5cm"height="39cm"/><frameid="main"x1="11.5cm"y1="0.0cm"width="5cm"height="39cm"/></pageTemplate></template><stylesheet><paraStylename="Normal"fontSize="12"leading="16"spaceBefore="16"/></stylesheet><story><parastyle="Normal">Simpletext</para><illustrationheight="5cm"width="5cm"align="center"><plugInGraphicmodule="reportlab_qr_code"function="qr">;text;Simpletext</plugInGraphic><lineModewidth="0.5"/><lines>&lines5;</lines></illustration><condPageBreakheight="7cm"/><para>Customsize</para><illustrationheight="3cm"width="3cm"align="center"><plugInGraphicmodule="reportlab_qr_code"function="qr">size=3cm;text;Customsize</plugInGraphic><lineModewidth="0.5"/><lines>&lines3;</lines></illustration><condPageBreakheight="7cm"/><para>Base64encoded</para><illustrationheight="5cm"width="5cm"align="center"><plugInGraphicmodule="reportlab_qr_code"function="qr">;base64;QmFzZSA2NCBlbmNvZGVk</plugInGraphic><lineModewidth="0.5"/><lines>&lines5;</lines></illustration><condPageBreakheight="7cm"/><para>Customcolors</para><illustrationheight="5cm"width="5cm"align="center"><plugInGraphicmodule="reportlab_qr_code"function="qr">bg=#eeeeee,fg=#a00000;text;Customcolors</plugInGraphic><lineModewidth="0.5"/><lines>&lines5;</lines></illustration><condPageBreakheight="7cm"/><para>Padding20%</para><illustrationheight="5cm"width="5cm"align="center"><plugInGraphicmodule="reportlab_qr_code"function="qr">padding=20%;text;Padding20%</plugInGraphic><lineModewidth="0.5"/><lines>&lines5;</lines></illustration><condPageBreakheight="7cm"/><para>Padding1cm</para><illustrationheight="5cm"width="5cm"align="center"><plugInGraphicmodule="reportlab_qr_code"function="qr">padding=1cm;text;Padding1cm</plugInGraphic><lineModewidth="0.5"/><lines>&lines5;</lines></illustration><condPageBreakheight="7cm"/><para>Padding1pixel</para><illustrationheight="5cm"width="5cm"align="center"><plugInGraphicmodule="reportlab_qr_code"function="qr">padding=1;text;Padding1pixel</plugInGraphic><lineModewidth="0.5"/><lines>&lines5;</lines></illustration><condPageBreakheight="7cm"/><para>ErrorcorrectionM</para><illustrationheight="5cm"width="5cm"align="center"><plugInGraphicmodule="reportlab_qr_code"function="qr">error_correction=M;text;Errorcorrection</plugInGraphic><lineModewidth="0.5"/><lines>&lines5;</lines></illustration><condPageBreakheight="7cm"/><para>ErrorcorrectionL</para><illustrationheight="5cm"width="5cm"align="center"><plugInGraphicmodule="reportlab_qr_code"function="qr">error_correction=L;text;Errorcorrection</plugInGraphic><lineModewidth="0.5"/><lines>&lines5;</lines></illustration><condPageBreakheight="7cm"/><para>Version10</para><illustrationheight="5cm"width="5cm"align="center"><plugInGraphicmodule="reportlab_qr_code"function="qr">version=10;text;Version10</plugInGraphic><lineModewidth="0.5"/><lines>&lines5;</lines></illustration><condPageBreakheight="7cm"/><parastyle="Normal">Smallradius</para><illustrationheight="5cm"width="5cm"align="center"><plugInGraphicmodule="reportlab_qr_code"function="qr">radius=0.5;text;Smallradius</plugInGraphic><lineModewidth="0.5"/><lines>&lines5;</lines></illustration><condPageBreakheight="7cm"/><parastyle="Normal">Roundwithbetterpath</para><illustrationheight="5cm"width="5cm"align="center"><plugInGraphicmodule="reportlab_qr_code"function="qr">radius=0.5,enhanced_path=1;text;ROUNDWITHBETTERPATH</plugInGraphic><lineModewidth="0.5"/><lines>&lines5;</lines></illustration><condPageBreakheight="7cm"/><parastyle="Normal">Largeradius</para><illustrationheight="5cm"width="5cm"align="center"><plugInGraphicmodule="reportlab_qr_code"function="qr">radius=3.5;text;Largeradius</plugInGraphic><lineModewidth="0.5"/><lines>&lines5;</lines></illustration><condPageBreakheight="7cm"/><para>Inverted</para><illustrationheight="5cm"width="5cm"align="center"><plugInGraphicbaseDir="."module="utils"function="gradient"/><plugInGraphicmodule="reportlab_qr_code"function="qr">padding=0,fg=#ffffff,invert=1;text;Inverted</plugInGraphic><lineModewidth="2"/><strokecolor="#ffffff"/><rectx="0"y="0"width="5cm"height="5cm"fill="0"stroke="1"/></illustration><condPageBreakheight="7cm"/><para>Mask</para><illustrationheight="5cm"width="5cm"align="center"><lineModewidth="0.5"/><lines>&lines5;</lines><plugInGraphicmodule="reportlab_qr_code"function="qr">mask=1,radius=0.5,enhanced_path=1;text;Mask</plugInGraphic><plugInGraphicbaseDir="."module="utils"function="gradient"/></illustration><condPageBreakheight="7cm"/><parastyle="Normal">Hole</para><illustrationheight="5cm"width="5cm"align="center"><plugInGraphicmodule="reportlab_qr_code"function="qr">hole=20%:40%:60%:20%,error_correction=H,radius=0.3,enhanced_path=1;text;HoleinsideQRcode</plugInGraphic><setFontname="Helvetica"size="18"/><drawStringx="1.8cm"y="2.35cm">Logo</drawString><lineModewidth="0.5"/><lines>&lines5;</lines></illustration><condPageBreakheight="7cm"/><parastyle="Normal">Logo</para><illustrationheight="5cm"width="5cm"align="center"><plugInGraphicmodule="reportlab_qr_code"function="qr">padding=2,radius=0.5,hole=35%:35%:30%:30%,fg=#554488,error_correction=H,draw=all-align-eyes,draw=alignpupils,radius=0.25,draw=alignballs,fg=#e24329,radius=1,draw=eyeball2+eyeball3,radius=3.5,fg=#fca326,draw=eyeball1,radius=3.5,fg=#e24329,draw=eyepupils,fg=#44366d,radius=3.5;text;https://about.gitlab.com/</plugInGraphic><lineModewidth="0.5"/><lines>&lines5;</lines><imagefile="gitlab.svg"x="1.8cm"y="1.8cm"width="1.4cm"height="1.4cm"/></illustration></story></document>Output: