c.setLineWidth(10) c.setStrokeColor('yellow') c.setFillColor('lightgreen')With these settings, the full code is here. c.rotate(-45)c.drawImage('D:\\top2.jpg',0,0)c.rotate(45) Drawing rectangle We can set the width, colour of the border of the rectangle. Here after rotating the object we can restore by using the same offset. Here we have fixed the margin by using translate() method, so we used -0.8*inch as X value to keep the image at Left edge.įrom reportlab.pdfgen import canvasfrom import inchfrom import letter,A4my_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf'c = canvas.Canvas(my_path,bottomup=1,pagesize=letter)c.translate(inch,inch) #starting point of coordinates c.drawImage('D:\\top2.jpg',-0.8*inch,9.3*inch) c.showPage() # saves current pagec.save() # stores the file and close the canvas We can use Image from URL c.drawImage(' ',-0.8*inch,9.3*inch) RotateSometime we have to rotate the text or image on the canvas. from reportlab.pdfgen import canvasfrom import inchfrom import letter,A4my_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf'# file pathc = canvas.Canvas(my_path,bottomup=1,pagesize=letter)c.translate(inch,inch) #starting point of coordinate to one tStrokeColorRGB(1,0,0) # red colour of tLineWidth(10) #width of the line c.line(0,8*inch,7*inch,8*inch) # draw line c.showPage() # saves current pagec.save() # stores the file and close the canvas Adding image to PDF file We have to specify the x and y coordinates along with the image.Ĭ.drawImage('D:\\top2.jpg',-0.8*inch,9.3*inch) We used setLineWidth(10) to set the thickness of the line.īy using translate() we have set the coordinates by keeping one inch as margin. To set the colour of the line we used setStrokeColurRGB(). Here we will start with using the dimension unit inch from import inchfrom import letter, A4We can set the margins of the page like this c.translate(inch,inch) # set the starting point of coordinate Drawing Lines To draw a line we need two coordinates. from reportlab.pdfgen import canvasfrom import inchfrom import lettermy_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf'# file pathc = canvas.Canvas(my_path,pagesize=letter)c.translate(inch,inch) # starting point of coordinate to one tFillColorRGB(0,0,1) # fill colourl1=c.getAvailableFonts() # list of available fontsj=1 # to adjust Y coordinate against each row for my_font in l1: # loop through all fonts c.setFont(my_font,16) # set font family and size c.drawString(inch,j*inch*0.5,my_font) j=j+1c.showPage() # saves current pagec.save() # stores the file and close the canvas Understanding GeometryWe can use various canvas methods like transform, translate, scale, rotate to manage the layout. Here is a script to display a list of available fonts. We can get a list of supported fonts by using getAvailableFonts() method. c.setFont("Helvetica", 20) # font family and size#c.setFillColorCMYK(.2.2,0.39) # font colour as CMYK#c.setFillColorRGB(0,0,1) # font colour as RGB#c.setFillColor('lightgreen')#lawngreen, lightblue,tFillColor('lightblue') #lavendarblushc.drawString(200,200,"Hello World") # write text in page Font styleThere are some default font family supported by ReportLab. Before using drawString() we can set these values. Font colour and font sizeWe can manage the font size and font colour. from reportlab.pdfgen import canvasmy_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf'c = canvas.Canvas(my_path,bottomup=0)c.drawString(200,200,"Hello World") # write text in page c.showPage() # saves current pagec.save() # stores the file and close the canvasAbove code will create the my_pdf.pdf file in the path and write the string Hello World at x=200 and y= 200 position. The option buttomup=0 is the default setting and by setting the value to 1 we can start our coordinate system from top left.Ĭhange the value of my_path as per your system ( requirement ). How to create PDF file Import canvas from reportlab and we are using my_pdf.pdf as our file name. The bottomup argument switches the coordinate system from bottom left to to top left of the page. X coordinate increases towards right and y coordinate increases towards up direction. pip install reportlabCreate your first pdf file.Ĭanvas is like a blank white paper where the x, y coordinates starts from bottom left. Installing ReportLabAt your command prompt use this command. Creating PDF file and adding text line image rectangle and watermark by using Python ReportLab
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |