django - pyPDF通过django合并并显示为httpresponse

原文 标签 django pdf django-admin pypdf

我在合并pyPDF逻辑以将两个pdf文件合并到django站点时遇到麻烦。我已经编写了可以在本地服务器上的python文件中运行时合并文件的代码(但我需要明确标识要合并的文件:

from pyPdf import PdfFileReader, PdfFileWriter

output = PdfFileWriter()
input1 = PdfFileReader(file("abc_form0.pdf", "rb"))
input2 = PdfFileReader(file("abc_form1.pdf", "rb"))
total_pages = input1.getNumPages()
total_pages1 = input2.getNumPages()

for page in xrange(total_pages):
    output.addPage(input1.getPage(page))

for page in xrange(total_pages1):
    output.addPage(input2.getPage(page))


outputStream = file("output.pdf", "wb")
output.write(outputStream)
outputStream.close()


此代码(来自我的django网站)可显示单个PDF。但是,当选择多个PDF时,仅显示最终的PDF,因此需要集成pyPDF来创建一个包含所有请求文件的文件。

class ABCAdmin(admin.ModelAdmin):

    actions = ['print_selected_pdf']

    def create_pdf(self, request, queryset):
        response = HttpResponse(mimetype="application/pdf")
        response['Content-Disposition'] = 'attachment; filename=form.pdf'
        for obj in queryset:
            response.write(obj.form)
            ABC.objects.filter(pk=obj.pk).update(user=request.user,pdf_printed="1",request_time=time.strftime("%H:%M:%S"),request_date=datetime.datetime.today())
            return response

    def print_selected_pdf(self, request, queryset):
        # prints the pdfs for those that are selected,
        # regardless if the pdf_printed field is true or false
        qs = queryset.filter(pdf_printed__exact=0)
        return self.create_pdf(request, qs)


我正在努力寻找一种将这两种方法结合起来的方法。我尝试了以下操作,但出现IO错误[Errno 2]没有此类文件或目录:'obj.form'。所以它不是在obj.form中读取-我需要找到一种方法来使其正确读取这些对象。无论如何,这就是我尝试过的方法。

def create_pdf(self, request, queryset):
    response = HttpResponse(mimetype="application/pdf")
    response['Content-Disposition'] = 'attachment; filename=form.pdf'
    for obj in queryset:
        output = PdfFileWriter()
        input = PdfFileReader(file("obj.form","rb"))
        total_pages = input.getNumPages()
        for page in xrange(total_pages):
            output.addPage(input.GetPage(page))
        outputStream = file("output.pdf", "wb")
        response.write(outputStream)
        outputStream.close()
        ABC.objects.filter(pk=obj.pk).update(user=request.user,pdf_printed="1",request_time=time.strftime("%H:%M:%S"),request_date=datetime.datetime.today())
        return response

def print_selected_pdf(self, request, queryset):
    # prints the pdfs for those that are selected,
    # regardless if the pdf_printed field is true or false
    qs = queryset.filter(pdf_printed__exact=0)
    return self.create_pdf(request, qs)


与往常一样,感谢您可能提供的任何帮助或提示!

最佳答案

我在输出流中使用stringIO来解决此问题。我还必须定义要添加到输出中的PDF表单的每一页。这可以迭代我需要的任何数量的表格。

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

相关文章:

django - Django在ForeignKey上正确使用select_related或prefetch_related

python - 在Geodjango中按几何交集关联两个模型

html - 新标签页超链接到PDF

django - Django Admin:如何设置内联元素的顺序?

django - Django-用户管理员-将组添加到list_display

javascript - 作为道具传递的数据未在django和reactjs中显示

angularjs - 使用angularjs在浏览器的新窗口中打开PDF

c# - ABCpdf转换html锚以跳到同一PDF的另一页

python - Django Admin-父ModelChoiceField上的动态子ModelChoiceField查询集

python - 使用基于自定义用户的类的django登录