Tag Archives: Reportlab

Book Contest: ReportLab: PDF Processing with Python

I recently released a new book entitled ReportLab: PDF Processing with Python. In celebration of a successful launch, I have decided to do a little contest.

Rules

  • Post a comment telling me why you would want a copy
  • The most clever or heartfelt commenter will be chosen by me

The contest will run starting now until Friday, August 17th @ 11:59 p.m. CST.

Runners up will receive a free copy of the eBook. The grand prize will be a signed paperback copy + the eBook version!

ReportLab: PDF Publishing with Python is now Available!

My latest book, ReportLab: PDF Processing with Python is now available for purchase.

ReportLab has been around since the year 2000 and has remained the primary package that Python developers use for creating reports in the PDF format. It is an extremely powerful package that works across all the major platforms. This book will also introduce the reader to other Python PDF packages.

You can get the book at the following online retailers:

Creating Interactive PDF Forms in ReportLab with Python

The ReportLab toolkit allows you to create interactive fillable forms. The PDF standard actually has a fairly rich set of interactive elements. ReportLab doesn’t support all of these elements, but it does cover most of them. In this section, we will look at the following widgets:

  • checkbox
  • radio
  • choice
  • listbox
  • textfield

All of these widgets are created by calling various methods on the canvas.acroform property. Note that you can only have one form per document. Let’s take a look at the widgets that ReportLab supports! Continue reading Creating Interactive PDF Forms in ReportLab with Python

Filling PDF Forms with Python

Fillable forms have been a part of Adobe’s PDF format for years. One of the most famous examples of fillable forms in the United States are documents from the Internal Revenue Service. There are lots of government forms that use fillable forms. There are many different approaches for filling in these forms programmatically. The most time consuming method I have heard about is to just recreate the form in ReportLab by hand and then fill it in. Frankly I think this is probably the worst idea, except when your company is in charge of creating the PDFs itself. Then that might be a viable option because you then have complete control over the PDF creation and the inputs that need to go into it.


Creating a Simple Form

We need a simple form to use for our first example. ReportLab has built-in support for creating interactive forms, so let’s use ReportLab to create a simple form. Here is the code: Continue reading Filling PDF Forms with Python

Exporting Data from PDFs with Python

There are many times where you will want to extract data from a PDF and export it in a different format using Python. Unfortunately, there aren’t a lot of Python packages that do the extraction part very well. In this chapter, we will look at a variety of different packages that you can use to extract text. We will also learn how to extract some images from PDFs. While there is no complete solution for these tasks in Python, you should be able to use the information herein to get you started. Once we have extracted the data we want, we will also look at how we can take that data and export it in a different format.

Let’s get started by learning how to extract text!


Extracting Text with PDFMiner

Probably the most well known is a package called PDFMiner. The PDFMiner package has been around since Python 2.4. It’s primary purpose is to extract text from a PDF. In fact, PDFMiner can tell you the exact location of the text on the page as well as father information about fonts. For Python 2.4 – 2.7, you can refer to the following websites for additional information on PDFMiner:

PDFMiner is not compatible with Python 3. Fortunately, there is a fork of PDFMiner called PDFMiner.six that works exactly the same. You can find it here: https://github.com/pdfminer/pdfminer.six Continue reading Exporting Data from PDFs with Python

Adding SVGs to PDFs with Python and ReportLab

ReportLab has native support for generating SVGs, but not for embedding SVGs in their PDFs. Fortunately, Dinu Gherman created the svglib package, a pure-Python package that can read SVG files and convert them to other formats that ReportLab can use. The official website for svglib is on Github.

The svglib package will work on Linux, Mac OS and Windows. The website states that it works with Python 2.7 – 3.5, but it should work in newer versions of Python as well.

You can use svglib to read your existing SVG giles and convert them into ReportLab Drawing objects. The svglib package also has a command-line tool, svg2pdf, that can convert SVG files to PDFs. Continue reading Adding SVGs to PDFs with Python and ReportLab

ReportLab Book Kickstarter’s – 2 Days Left

There is only a little over 2 days left for my ReportLab book Kickstarter. This is your only chance to purchase a signed copy of the book and it’s also probably the cheapest way of getting the eBooks too!

I currently have 7 chapters done with number 8 nearing completion. There are over 170 pages in these chapters alone. I hope you’ll check it out as ReportLab is a fun way to use Python to design dynamic reports in a PDF format.

ReportLab Book Chapter Sampler

I thought it would be fun to create a sample of the book so you can get an idea of what the book will be like. So I created a PDF that contains the first 3 chapters of the book for you.

Download Sample

Note that the format of this sample is not quite right as I had to generate it from a more complete version, so the PDF’s table of contents shows more than what is actually in the document.

Also I just broke through the 100 page boundary over the weekend. I am finishing up chapter 5 and will be cranking out another couple of chapters this week if all goes well.

Thanks for your support!
Mike

ReportLab Book Cover Story

I really like coming up with fun covers for my books. I also like to find new artists for each book so that they all end up looking unique. I do plan to re-use one or two artists at some point though.

Anyway, for the ReportLab book I happened to stumble across Therese Larsson’s website and I really liked how she did her lighting in her artwork. She is from Sweden and has worked with some fairly big companies, including Disney, Google, and Adidas. You can read more about her on Behance.

I ended up commissioning the cover from her and I described what I wanted. Here is the initial sketch:

ReportLab Cover Sketch

Continue reading ReportLab Book Cover Story

ReportLab 101: The textobject

The ReportLab toolkit provides multiple ways for you to generate text on your PDFs. The most popular examples that I have seen are using canvas methods or using PLATYPUS. The canvas method that you will likely see the most is drawString. Here is an example:

from reportlab.pdfgen import canvas
 
c = canvas.Canvas("hello.pdf")
c.drawString(100, 100, "Welcome to Reportlab!")
c.showPage()
c.save()

Basically all that does is draw a string at the x/y coordinates given. Using PLATYPUS is significantly more complicated:

from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
 
def hello():
    doc = SimpleDocTemplate("hello_platypus.pdf",
                            pagesize=letter,
                            rightMargin=72,
                            leftMargin=72,
                            topMargin=72,
                            bottomMargin=18)
    styles = getSampleStyleSheet()
 
    flowables = []
 
    text = "Hello, I'm a Paragraph"
    para = Paragraph(text, style=styles["Normal"])
    flowables.append(para)
 
    doc.build(flowables)
 
if __name__ == '__main__':
    hello()

You will note that most of the time when you use PLATYPUS, you will need to use a template, a style and a paragraph or some other Flowable. But let’s go back to the canvas. It actually has another method of generating text that ReportLab calls the textobject. Frankly I have never had the need for one of these as ReportLab’s Paragraph class gives you more than enough control over the presentation of your text. But if you depend on using the low level canvas for generating your PDFs, then you will like to know that a textobject will make PDF generation faster because it doesn’t use individual calls to drawString. Continue reading ReportLab 101: The textobject