from reportlab.pdfgen.canvas import Canvas from reportlab.lib.pagesizes import letter, A4 from reportlab.lib.units import mm, inch from reportlab.lib.colors import pink, black, red, blue, green def hello(canvas): # Select unit canvas.translate(mm, mm) # Select font canvas.setFont("Helvetica", 14) # Select colors canvas.setStrokeColorRGB(0.2,0.5,0.3) canvas.setFillColorRGB(1,0,1) # draw some lines canvas.line(100*mm,100*mm,200*mm,200*mm) canvas.line(25*mm,15*mm,50*mm,290*mm) # canvas.line(x1,y1,x2,y2) # draw a rectangle canvas.rect(10*mm,65*mm,60*mm,80*mm, stroke=0, fill=1) # make text go straight up canvas.rotate(90) # change color canvas.setFillColorRGB(0,0,0.77) # say hello (note after rotate the y coord needs to be negative!) canvas.drawString(20*mm, 150*-mm, "Hello World") # Undo the rotation canvas.rotate(-90) # Insert another string canvas.drawString(10*mm, 10*mm, "Hello You") # draw a grid canvas.setStrokeColor(pink) canvas.grid([50*mm, 75*mm, 100*mm, 125*mm], [30*mm, 60*mm, 90*mm, 120*mm, 150*mm]) # draw a circle canvas.setFillColor(blue) canvas.circle(150*mm, 250*mm, 20*mm, stroke=0, fill=1) # Canvas shapes # ------------- # canvas.grid(xlist, ylist) # canvas.bezier(x1, y1, x2, y2, x3, y3, x4, y4) # canvas.arc(x1,y1,x2,y2) # canvas.rect(x, y, width, height, stroke=1, fill=0) # canvas.ellipse(x1,y1, x2,y2, stroke=1, fill=0) # canvas.wedge(x1,y1, x2,y2, startAng, extent, stroke=1, fill=0) # canvas.circle(x_cen, y_cen, r, stroke=1, fill=0) # canvas.roundRect(x, y, width, height, radius, stroke=1, fill=0) textobject = canvas.beginText(25*mm, 25*mm) canvas.drawText(textobject) def fonts(canvas): canvas.translate(mm, mm) text = "Oh hello, you can use me in Reportlab !" x = 50*mm y = 5*mm for font in canvas.getAvailableFonts(): canvas.setFont(font, 10) canvas.drawString(x, y, text) canvas.setFont("Helvetica", 10) canvas.drawRightString(x-10, y, font+":") y = y+13 page = Canvas("hello.pdf", pagesize=A4, bottomup=0, verbosity=1) # filename # pagesize=(595.27,841.89), # = numbers in points # bottomup=1, # Some graphics systems (like PDF and PostScript)place (0,0) at the bottom left of the page # bottomup = 0 --> top left # bottomup = 1 --> bottom left # !! The bottomup argument is deprecated and may be dropped in future # pageCompression=0, # pageCompression=1 --> compression is enabled # encoding=rl_config.defaultEncoding, # verbosity=0, # verbosity=1 --> print log information while making a pdf # encrypt=None width, height = A4 # add hello page hello(page) # add a new page page.showPage() # add font page fonts(page) # add a new page page.showPage() # save PDF page.save()