开发者

Generating PDF Forms from Raw User Input (Python)

So I'm a University student with a part time job - but I do have some Python experience. Anyway I thought I'd try and impress my boss by making a form generator. My job is at a go kart place where we need people to fill in a bunch of safety information, so my aim is to have this as a GUI or webpage on the computer where we'd just write in their name and other details and it'd produce a form with all of their information on that they'd just have to sign.

I've had a look around and noticed that there's quite a few tools but I'm not sure if they're explicitly what I want - all looking quite complicated.

I've made a simple GUI before but ideally I'd just have them do it on a 开发者_如何转开发.html page.

Can anyone offer me some advice as to how to go about it?

Thanks!

Ps. I know this place is mostly for code, so I apologise if I've broken the etiquette by making this style of post.


You can use reportlab, here is a tutorial. And it could be installed with easy_install


As an alternative to generating the whole pdf from scratch in code (e.g. using reportlab as previously described -- which is a perfectly reasonable solution and reportlab is great library for doing this), you can fill in an existing pdf form generated by some editing software (e.g. word or something similar -- I use Scribus NG.. but that's a nightmare to learn and I don't really recommend it for the casual user).

The advantage of filling in an existing form is that you get to use a proper editing tool for the form which makes it easier to create more professional looking forms (also they can be edited by a graphic designer). The disadvantages are that it's a little bit more complicated, and it's not a pure python solution.

You fill in the form by creating an fdf file and execing an external tool called pdftk from python. Writing the fdf file is an exercise I'll leave up to you! The fdf is basically a list of key-values where the key is the form field id (i.e. the text field in the forms id) and the value is the value written to that field.

If you want to go do it this way I suggest that you first manually write your fdf file then manually run pdftk .. get it working, then automate with python. It's not a beautiful solution but the results do look good.

Here's the code I use to exec pdftk which will help a little bit with getting your command line syntax right.

def pdftk(pdf_in, fdf_in, pdf_out):
    """
    Run pdftk to fill in pdf_out from the pdf form template and data given.
    (only tested on linux)

    """
    cmd_line = [pdftk_executable,
                pdf_in,
                "fill_form",
                fdf_in,
                "output",
                pdf_out,
                "verbose"]
    # print " ".join(cmd_line)
    try:
        pdftk_output = subprocess.check_output(cmd_line)
    except subprocess.CalledProcessError as e:
        pdftk_output = e.output
        pdftk_error = True
    return
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜