Showing posts with label Bottle-framework. Show all posts
Showing posts with label Bottle-framework. Show all posts

Wednesday, May 1, 2013

PDF in a Bottle - creating PDF using xtopdf, ReportLab, Bottle and Python

By Vasudev Ram





pdf_bottle.py is a program I wrote that allows you to create a PDF file from text, over the web, by entering your text into a form and submitting it.

Here is the program:
# pdf_bottle.py

# Description: Program to generate PDF from text, over the web,
# using xtopdf, ReportLab and the Bottle web framework.
# It can be used to create short, simple PDF e-books.
# Author: Vasudev Ram - http://dancingbison.com
# Copyright 2013 Vasudev Ram 
# Tested with Python 2.7.

# Version: 0.1

# Dependencies:
# xtopdf - https://bitbucket.org/vasudevram/xtopdf
# bottle - http://bottlepy.org
# ReportLab - http://www.reportlab.com/ftp/reportlab-1.21.zip
# Python - http://python.org

from PDFWriter import PDFWriter

from bottle import route, request, run

@route('/edit_book')
def edit_book():
    return '''
    <form action="/https/jugad2.blogspot.com/save_book" method="post">
    PDF file name: <input type="text" name="pdf_file_name" />

    Header: <input type="text" name="header" />

    Footer: <input type="text" name="footer" />

    Content:
    <textarea name="content" rows="15"   cols="50"></textarea>

    <input type="submit" value="Submit" />

    </form>
'''

@route('/save_book', method='POST')
def save_book():
    try:
        pdf_file_name = request.forms.get('pdf_file_name')
        header = request.forms.get('header')
        footer = request.forms.get('footer')
        content = request.forms.get('content')

        pw = PDFWriter(pdf_file_name)
        pw.setFont("Courier", 12)
        pw.setHeader(header)
        pw.setFooter(footer)

        lines = content.split('\n')
        for line in lines:
            pw.writeLine(line)

        pw.savePage()
        pw.close()
        return "Done"
    except Exception:
        return "Not done"

def main():
    run(host='localhost', port=9999)

if __name__ == "__main__":
    main()

To run it, you need to have Python, the open source version of ReportLab, my xtopdf toolkit and the Bottle Python web framework installed.

Here is a guide to installing and using xtopdf.

For help with installing the other products, consult their respective sites, linked above.

Then run the program with this command:

python pdf_bottle.py

Then, in a browser window, go to localhost:9999

Enter the details - PDF file name, header, footer and text content - in the form, then click Submit.

The PDF file will be generated in the same directory from where you ran the Python program.

This is the first version, and has been tested only a bit. It you find any issues, please mention them in the comments.

Various improvements are possible, including sending the generated PDF to the user's browser, or providing a link to download it (better), and I'll work on some of them over time.

P.S. Excerpt from the Bottle framework site:

[
Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module and has no dependencies other than the Python Standard Library.

Routing: Requests to function-call mapping with support for clean and dynamic URLs.
Templates: Fast and pythonic built-in template engine and support for mako, jinja2 and cheetah templates.
Utilities: Convenient access to form data, file uploads, cookies, headers and other HTTP-related metadata.
Server: Built-in HTTP development server and support for paste, fapws3, bjoern, Google App Engine, cherrypy or any other WSGI capable HTTP server.
]

- Vasudev Ram - Dancing Bison Enterprises

Wednesday, June 6, 2012

Bottle Python framework is useful for small web apps

By Vasudev Ram









Bottle is a small Python web framework.



It can be useful for creating small / experimental web apps in Python.

It's easy to get started with. Installation is simple.

Creating small apps is also fairly easy.

The Hello World app is just this:

from bottle import route, run

@route('/hello/:name')
def index(name='World'):
    return 'Hello %s!' % name

run(host='localhost', port=8080)

I have been trying it out over the last few days and found it a good fit for the uses described above.

Like many web frameworks these days, it comes with a built-in development web server, so you don't have to spend time fiddling around trying to make it work with Apache or other servers - at least, not until you are ready to deploy your app in production.

Actually, as per the docs, running Bottle with CherryPy, Fapws3, Flup or Paste is simple. For example, this is how to use Paste (and it's similar for the others):

from bottle import PasteServer
...
run(server=PasteServer)

And the docs specify how to make it work on Apache with mod_wsgi.

I like the way the routes (URL-to-function mappings) are specified in Bottle.


- Vasudev Ram - Dancing Bison Enterprises