开发者

Print PDF from django form

开发者 https://www.devze.com 2023-04-12 01:34 出处:网络
I am in the early process of building a PDF builder for one of my fillable PDFs. At this point I am trying to just display the PDF when I click on my \'Print Form\' button but at the moment I don\'t

I am in the early process of building a PDF builder for one of my fillable PDFs.

At this point I am trying to just display the PDF when I click on my 'Print Form' button but at the moment I don't get any errors and it just opens a blank tab (firefox).

Here is my print_rdba from my lib/tools.py:

def print_rdba(client=None, data=None ,investment_form=None):
from django.http import HttpResponse
from clients.models import Transit
from products.models import ProductCategory
import cStringIO as StringIO
from dateutil.parser import parse
from datetime import date
from settings import URL
import re
from dateutil.relativedelta import relativedelta
from rates.models import InterestOption
from products.models import Product

URL = "/media/files/investment_forms/whatever.pdf"
file = ''
if investment_form:
    file = "%s" % URL

fdf = '<?xml version="1.0" encoding="UTF-8"?>\n<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">\n\t<fields>'


fdf += '''</fields>
    <f href="%s" target="_blank"/>
    </xfdf>''' % file

fdf = fdf.replace('^M', '')

response = HttpResponse(fdf.encod开发者_C百科e("ISO-8859-1"), mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=RDBA.xfdf'
return response

On my views to check to make sure the button was pressed:

if request.POST.has_key('submit_rdba_form'):
            return print_rdba(client=primary_member, data = form.data)

Now is there something else I am missing? I am going to be using .xfdf to populate the forms but like I said before, I am having trouble just having the pdf show up.

Thanks


The response your function is returning will not display a pdf, but will be treated as a file attachment (i.e saved to disk). This is because of this line:

response['Content-Disposition'] = 'attachment; filename=RDBA.xfdf'

which tells the browser that a file RDBA.xfdf is to be saved.

Check whether the print_rdba function is called at all. If your button does not have the name submit_rdba_form then request.POST.has_key('submit_rdba_form') will be False.

You may also try putting the markup into a template file, and render the template to the response instead. This is in line with Django's MTV philosophy (model-template-view, similar to MVC, where application logic is separated from presentation) and is better especially since you plan to add forms to the document:

xfdf.html in your templates folder:

<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
    <fields>
    </fields>
    <f href="{{file}}" target="_blank"/>
</xfdf>

New print_rdba

from django.http import HttpResponse
from django.template.loader import get_template
from django.template import Context

def print_rdba(client=None, data=None ,investment_form=None):
    file = "/media/files/investment_forms/whatever.pdf"

    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=RDBA.xfdf'
    template = get_template("xfdf.html")
    xfdf = template.render(Context({"file":file}))
    response.write(xfdf)
    return response
0

精彩评论

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

关注公众号