• TwitterFacebookGoogle PlusLinkedInRSS FeedEmail

Python Reportlab Template

02.10.2019 
Python Reportlab Template Average ratng: 5,0/5 7269 votes

I'm tryng to use Reportlab to output an invoice on a simple Django app. It seems quite difficult to do simple things like this. Here's my code: def print_pdf(fattura. Generate PDF from HTML using Django and Reportlab. Ask Question. Up vote 6 down vote favorite. I am coming back with a new question which I am unable to answer, having scratched my head the whole day on it. I want to generate a PDF from a webpage by clicking on a 'Download PDF' button. I tried several modules including Reportlab.

Reportlab is a very flexible PDF creation package for Python. You can layout your documents using absolute positioning or by using Flowable objects, such as a Paragraph, a Table or Frame. You can even mix the two together!

In this article, we will be looking at how to create some custom Flowables. For example, what do you do if you need to add a line to demarcate the start of a new section in your document? There isn’t really a built-in Flowable for that, so we’ll design our own. We will also design a flowable that has a line and a box with text inside of it. Let’s get started! Creating a Line Flowable Creating a line flowable is actually quite simple.

Basically you just need to sub-class Reportlab’s Flowable class and tell it to draw line. The following is based on an example from the Reportlab from reportlab. Pagesizes import letter from reportlab. Styles import getSampleStyleSheet from reportlab. Units import inch from reportlab. Platypus import (Flowable, Paragraph, SimpleDocTemplate, Spacer ) ######################################################################## class MCLine (Flowable ): ' ' Line flowable - draws a line in a flowable ' ' #- def init ( self, width, height= 0 ): Flowable.

For example, installing AutoCAD 2018 as a point product requires product key 001J1, but installing AutoCAD 2018 from the AutoCAD Design Suite Premium 2018 requires product key 768J1. Hsmworks 2015 keygen.

init ( self ) self. Width = width self. Height = height #- def repr ( self ): return 'Line(w=%s)'% self.

Python reportlab tutorial

Width #- def draw ( self ): ' ' draw the line ' ' self. Line ( 0, self. Height, self. Height ) #- def createpdf ( ): ' ' Create a pdf ' ' story= doc = SimpleDocTemplate ( 'test.pdf',pagesize=letter ) styles=getSampleStyleSheet ( ) spacer = Spacer ( 0, 0.25.inch ) ptext = '%s'% 'Section #1' story. Append (Paragraph (ptext, styles 'Normal' ) ) story.

Append (spacer ) line = MCLine ( 500 ) story. Append (line ) story. Append (spacer ) ptext = '%s'% 'Section #2' story. Append (Paragraph (ptext, styles 'Normal' ) ) doc. Build (story ) #- if name 'main': createpdf ( ) If you run this code, you should end up with a PDF that looks something like the following: The code in the createpdf function creates a document based off a template that’s included with Reportlab.

Then we create some Flowables and add them to a normal Python list. We want a little space around the two fake “sections” that we create, so we add a Spacer Flowable before and after the line Flowable. Then we build the document and voila!

We have a newly minted PDF! Creating a Bordered Textbox + Line Flowable Recently I needed to create a text box with a border plus a line that went from the top of the box off to the right and I needed to be able to add it to my document as a flowable. It kind of looked like this piece of ASCII art: - foobar - It took a bit of experimentation, but I eventually came up with the following solution: from reportlab. Pagesizes import letter from reportlab. Platypus import Flowable, SimpleDocTemplate, Spacer from reportlab. Units import inch ######################################################################## class BoxyLine (Flowable ): ' ' Draw a box + line + text - foobar - ' ' #- def init ( self, x= 0, y= -15, width= 40, height= 15, text= ' ): Flowable.

init ( self ) self. Width = width self. Height = height self. Text = text #- def draw ( self ): ' ' Draw the shape, text, etc ' ' self.

Height ) self. X, 0, 500, 0 ) self. DrawString ( self. Text ) doc = SimpleDocTemplate ( 'test2.pdf',pagesize=letter ) story= box = BoxyLine (text= 'foo' ) story.

Append (box ) story. Append (Spacer ( 0, 1.inch ) ) box = BoxyLine (text= 'bar' ) story. Append (box ) doc. Build (story ) Let’s break this down a bit.

Template

Python Reportlab Template

First off, we once again sub-class the Flowable class. This time we add some additional parameters so we can tell it to change the size of the box and the width of the line as well as display a bit of text. Then in the draw method we finagle the text into the correct position. If you mess around with the size of the box, then you might need to change the positioning of the line or the text.

I ended up enhancing this slightly so that I could use a Paragraph object instead of the canvas’s drawString method. Here’s how that works: from reportlab.

Python Reportlab

Pagesizes import letter from reportlab. Styles import getSampleStyleSheet from reportlab.

Units import inch, mm from reportlab. Platypus import (Flowable, Paragraph, SimpleDocTemplate, Spacer ) ######################################################################## class BoxyLine (Flowable ): ' ' Draw a box + line + text - foobar - ' ' #- def init ( self, x= 0, y= -15, width= 40, height= 15, text= ' ): Flowable. init ( self ) self. Width = width self. Height = height self. Text = text self. Styles = getSampleStyleSheet ( ) #- def coord ( self, x, y, unit= 1 ): ' ' Helper class to help position flowables in Canvas objects ' ' x, y = x.

unit, self. Height - y. unit return x, y #- def draw ( self ): ' ' Draw the shape, text, etc ' ' self.

Height ) self. X, 0, 500, 0 ) p = Paragraph ( self. Text, style= self. Styles 'Normal' ) p. WrapOn ( self.

Python Reportlab Pdf

DrawOn ( self. Canv,. self.

Coord ( self. X +2, 10, mm ) ) doc = SimpleDocTemplate ( 'test3.pdf',pagesize=letter ) story= box = BoxyLine (text= 'foo' ) story. Append (box ) story. Append (Spacer ( 0, 1.inch ) ) box = BoxyLine (text= 'bar' ) story. Append (box ) doc.

Python Reportlab Template

Build (story ) The primary advantage of using a Paragraph instead of drawString is that you can now control what font you’re using and the size of the font using Reportlab’s HTML-like tags: txt = 'This is a 10 point font' I personally find that simpler to use then using the Canvas’s font related methods. Wrapping Up Now you know how to use Reportlab’s Flowable class to create your own custom Flowables. This gives you additional flexibility when creating your own PDF documents. Additional Reading. A Simple Step-by-Step.

Reportlab:. Reportlab:. Reportlab –.