pyPDF merging and displaying as httpresponse through django

旧巷老猫 提交于 2019-12-05 19:05:18
Joseph

I used stringIO in the output stream to get around this. I also had to define each page of the PDF form to be added to the output. This works to iterate for any number of forms that I need to be requested.

def create_form(self, request, queryset):
    response = HttpResponse(mimetype="application/pdf")
    response['Content-Disposition'] = 'attachment; filename=form.pdf'
    output = PdfFileWriter()
    for obj in queryset:
        input = PdfFileReader(cStringIO.StringIO(obj.form))
        output.addPage(input.getPage(0))
        output.addPage(input.getPage(1))
        output.addPage(input.getPage(2))
        output.addPage(input.getPage(3))
        ABC_Self.objects.filter(pk=obj.pk).update(user=request.user,pdf_printed="1",request_time=time.strftime("%H:%M:%S"),request_date=datetime.datetime.today())
    outputStream = cStringIO.StringIO()
    output.write(outputStream)
    response.write(outputStream.getvalue())
    return response

All you need to do is provide the full absolute path to your PDF files, e.g.

/home/joseph/form.pdf

or

c:/home/joseph/form.pdf

etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!