You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 KiB
24 KiB
Reportlab Cheatsheet¶
In [110]:
from reportlab.pdfgen.canvas import Canvas from reportlab.lib.pagesizes import letter, A4 from reportlab.lib.units import inch, mm
ReportLab PDF Library¶
What is the ReportLab PDF Library?
This is a software library that lets you directly create documents in Adobe's Portable Document Format (PDF)using the Python programming language. It also creates charts and data graphics in various bitmap and vectorformats as well as PDF.
Canvas¶
In [111]:
Canvas?
Init signature: Canvas( filename, pagesize=None, bottomup=1, pageCompression=None, invariant=None, verbosity=0, encrypt=None, cropMarks=None, pdfVersion=None, enforceColorSpace=None, initialFontName=None, initialFontSize=None, initialLeading=None, cropBox=None, artBox=None, trimBox=None, bleedBox=None, ) Docstring: This class is the programmer's interface to the PDF file format. Methods are (or will be) provided here to do just about everything PDF can do. The underlying model to the canvas concept is that of a graphics state machine that at any given point in time has a current font, fill color (for figure interiors), stroke color (for figure borders), line width and geometric transform, among many other characteristics. Canvas methods generally either draw something (like canvas.line) using the current state of the canvas or change some component of the canvas state (like canvas.setFont). The current state can be saved and restored using the saveState/restoreState methods. Objects are "painted" in the order they are drawn so if, for example two rectangles overlap the last draw will appear "on top". PDF form objects (supported here) are used to draw complex drawings only once, for possible repeated use. There are other features of canvas which are not visible when printed, such as outlines and bookmarks which are used for navigating a document in a viewer. Here is a very silly example usage which generates a Hello World pdf document. Example:: from reportlab.pdfgen import canvas c = canvas.Canvas("hello.pdf") from reportlab.lib.units import inch # move the origin up and to the left c.translate(inch,inch) # define a large font c.setFont("Helvetica", 80) # choose some colors c.setStrokeColorRGB(0.2,0.5,0.3) c.setFillColorRGB(1,0,1) # draw a rectangle c.rect(inch,inch,6*inch,9*inch, fill=1) # make text go straight up c.rotate(90) # change color c.setFillColorRGB(0,0,0.77) # say hello (note after rotate the y coord needs to be negative!) c.drawString(3*inch, -3*inch, "Hello World") c.showPage() c.save() Init docstring: Create a canvas of a given size. etc. You may pass a file-like object to filename as an alternative to a string. For more information about the encrypt parameter refer to the setEncrypt method. Most of the attributes are private - we will use set/get methods as the preferred interface. Default page size is A4. cropMarks may be True/False or an object with parameters borderWidth, markColor, markWidth and markLength if enforceColorSpace is in ('cmyk', 'rgb', 'sep','sep_black','sep_cmyk') then one of the standard _PDFColorSetter callables will be used to enforce appropriate color settings. If it is a callable then that will be used. File: ~/.local/lib/python3.7/site-packages/reportlab/pdfgen/canvas.py Type: type Subclasses:
In [112]:
# Make a Canvas object, bottomup=0 will put the 0,0 at the lop-left (instead of bottom-left) c = Canvas("pdf/reportlab-cheatsheet.pdf", pagesize=A4, bottomup=0)
Shapes¶
In [113]:
c.circle?
Signature: c.circle(x_cen, y_cen, r, stroke=1, fill=0) Docstring: draw a cirle centered at (x_cen,y_cen) with radius r (special case of ellipse) File: ~/.local/lib/python3.7/site-packages/reportlab/pdfgen/canvas.py Type: method
In [114]:
c.circle(20*mm, 20*mm, 10*mm, stroke=0, fill=1)
In [115]:
c.rect?
Signature: c.rect(x, y, width, height, stroke=1, fill=0) Docstring: draws a rectangle with lower left corner at (x,y) and width and height as given. File: ~/.local/lib/python3.7/site-packages/reportlab/pdfgen/canvas.py Type: method
In [116]:
c.rect(40*mm, 40*mm, 10*mm, 10*mm, stroke=1, fill=0)
In [117]:
c.roundRect?
Signature: c.roundRect(x, y, width, height, radius, stroke=1, fill=0) Docstring: Draws a rectangle with rounded corners. The corners are approximately quadrants of a circle, with the given radius. File: ~/.local/lib/python3.7/site-packages/reportlab/pdfgen/canvas.py Type: method
In [118]:
c.roundRect(60*mm, 60*mm, 50*mm, 50*mm, 5*mm, stroke=1, fill=0)
In [119]:
c.showPage()
In [ ]:
Fonts¶
In [120]:
# Reportlab comes with a set of fonts c.getAvailableFonts()
Out[120]:
['Courier', 'Courier-Bold', 'Courier-BoldOblique', 'Courier-Oblique', 'Helvetica', 'Helvetica-Bold', 'Helvetica-BoldOblique', 'Helvetica-Oblique', 'Symbol', 'Times-Bold', 'Times-BoldItalic', 'Times-Italic', 'Times-Roman', 'ZapfDingbats']
In [131]:
c.setFont('Courier', 72)
In [121]:
# Or you can import fonts from reportlab.pdfbase.ttfonts import TTFont, pdfmetrics fontpath = "fonts/OSP-DIN.ttf" font = TTFont('OSP-DIN', fontpath) pdfmetrics.registerFont(font)
In [122]:
c.setFont('OSP-DIN', 72)
In [123]:
c.drawCentredString(105*mm, 50*mm, "Hello :)!")
In [124]:
c.showPage()
In [ ]:
Colors¶
In [126]:
# Choose a RGB/CMYK color # Fill-color c.setFillColorRGB(255,0,0) c.setFillColorCMYK(0,1,0,0) # Stroke-color c.setStrokeColorRGB(255,255,0) c.setStrokeColorCMYK(0,0,1,0)
In [134]:
# Or choose one of the embedded colors from Reportlab from reportlab.lib.colors import pink, magenta, red, blue c.setFillColor(magenta) c.setStrokeColor(red)
In [128]:
c.setFont('OSP-DIN', 72) c.drawCentredString(105*mm, 50*mm, "Hello :)!") c.rect(105*mm, 150*mm, 50*mm, 50*mm, stroke=1, fill=1)
In [129]:
c.showPage()
In [ ]:
Lines¶
In [ ]:
In [ ]:
In [ ]:
In [ ]:
Text¶
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
Images¶
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
Save PDF¶
In [130]:
c.save()
In [ ]:
In [ ]: