如何使用Django中的数据库条目生成pdf文件?

时间:2019-04-03 17:41:36

标签: django python-3.x

我正在尝试使用Django模型的数据库条目生成pdf(基本上,数据是通过html表单输入的,并通过某些视图提交到数据库中),并将其存储在同一模型的文件字段中。 但是发布的与此主题相关的解决方案均未显示如何完成此任务(或者也许我找不到)。

这是我与将表单项存储在数据库中有关的视图

 def apply_purchase(request):
        current_user = get_object_or_404(User, username=request.user.username)
        print(current_user)
        user_details = ExtraInfo.objects.all().filter(user=current_user).first()
        print(user_details)
        user_type = HoldsDesignation.objects.all().filter(user=current_user).first()
        print(user_type)
        usertype=str.split(str(user_type))
        print(usertype)
        # Academics Admin Check
        user=usertype[0]
        #desig_id = Designation.objects.all().filter(name='Faculty')
        #print(desig_id)

        if(user == "student"):
            return HttpResponse('You are not authorised to view this page')

        if request.method == 'POST':
            item_name=request.POST.get('item_name')
            quantity=request.POST.get('quantity')
            expected_cost=int(request.POST.get('expected_cost'))

            if  expected_cost >=25000 and expected_cost <= 250000 :
                local_comm_mem1_id=request.POST.get('local_comm_mem1_id')
                local_comm_mem2_id=request.POST.get('local_comm_mem2_id')
                local_comm_mem3_id=request.POST.get('local_comm_mem3_id')

            nature_of_item1= 1 if request.POST.get('nature_of_item1') == 'on' else 0
            nature_of_item2= 1 if request.POST.get('nature_of_item2') == 'on' else 0
            purpose=request.POST.get('purpose')



     expected_purchase_date=request.POST.get('expected_purchase_date')
            a = apply_for_purchase.objects.create(
                    item_name=item_name,
                    quantity=int(quantity),
                    expected_cost=expected_cost,
                    nature_of_item1=nature_of_item1,
                    nature_of_item2=nature_of_item2,
                    purpose=purpose,
                    # budgetary_head_id = budgetary_head_id,
                    # inspecting_authority_id=inspecting_authority_id,
                    expected_purchase_date= expected_purchase_date,
                    indentor_name=user_details,

            )
            a.save()

我是django的新手,所以我们将不胜感激

1 个答案:

答案 0 :(得分:1)

根据documentation,您应该能够使用ReportLab实现此目的。为了以所需的方式输出PDF,您必须自定义PDF的绘制方式。

这是ReportLab的短代码snippet,希望您会需要它。

from django.http import HttpResponse
from rlextra.rml2pdf import rml2pdf
import cStringIO

def getPDF(request):
    """Returns PDF as a binary stream."""

    # Use your favourite templating language here to create the RML string.
    # The generated document might depend on the web request parameters,
    # database lookups and so on - we'll leave that up to you.
    rml = getRML(request)  

    buf = cStringIO.StringIO()

    rml2pdf.go(rml, outputFileName=buf)
    buf.reset()
    pdfData = buf.read()

    response = HttpResponse(mimetype='application/pdf')
    response.write(pdfData)
    response['Content-Disposition'] = 'attachment; filename=output.pdf'
    return response