gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:10:57 31 4
gpt4 key购买 nike

我在合并 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()

此代码(来 self 的 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 - pyPDF通过django合并并显示为httpresponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7123977/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com