Thursday, January 9, 2014

Generate PDF output from Firebird SQL data

By Vasudev Ram



PDF


Firebird is a cross-platform, lightweight, open source, relational database that was derived from Borland's Interbase, a database that was bundled with some versions of Borland's development tools like Delphi and Borland C++.

Firebird on Wikipedia.

I've used it some in the past. It is fairly easy to setup and use.

Here is a demo program that shows how to generate PDF output from a Firebird database, using my xtopdf PDF creation toolkit and the fbd Python driver for Firebird.
# FirebirdToPDF.py
# Author: Vasudev Ram - http://www.dancingbison.com
# Demo program to show how to convert Firebird RDBMS data to PDF.
# Uses xtopdf, Reportlab, Firebird RDBMS and fdb Python driver for Firebird.

import fdb
from PDFWriter import PDFWriter

con = fdb.connect(dsn='localhost:/temp/firebird/test.fdb', 
    user='dummy',  password='dummy')

cur = con.cursor()
select_stmt = 'select * from contacts order by name desc'
cur.execute(select_stmt)
pw = PDFWriter("contacts.pdf")
pw.setFont("Courier", 12)
pw.setHeader("Firebird contacts.fdb to PDF")
pw.setFooter("Generated by xtopdf using fdb Firebird driver for Python")
for (id, name, address) in cur:
    print id, name, address
    pw.writeLine(str(id) + " " + name + " " + address)
pw.close()

# EOF
You can run it with the command: python FirebirdToPDF.py

This is a screenshot of the output PDF created (click to enlarge):


The status page about fbd on the Firebird site is interesting. Excerpts from it (emphasis mine):

[ FDB is a Python library package that implements Python Database API 2.0-compliant support for the open source relational database Firebird®. In addition to the minimal feature set of the standard Python DB API, FDB also exposes the entire native client API of the database engine. Notably:
Automatic data conversion from strings on input.
Automatic input/output conversions of textual data between UNICODE and database character sets.
Support for prepared SQL statements.
Multiple independent transactions per single connection.
All transaction parameters that Firebird supports, including table access specifications.
Distributed transactions.
Firebird BLOB support, including support for stream BLOBs.
Support for Firebird Events.
Support for Firebird ARRAY data type.
Support for all Firebird Services
FDB is implemented in Python on top of Firebird client library using ctypes.

FDB works with Firebird 2.0 and newer, Python 2.7+ and 3.0+. ]

- Vasudev Ram - Dancing Bison Enterprises

Contact Page




No comments: