[英]Open pdf file with Django
I'm trying to merge two pdf files in Django with PyPDF2 and ReportLab. 我正在尝试将Django中的两个pdf文件与PyPDF2和ReportLab合并。 My view is as follows:
我的看法如下:
@login_required
def export_to_pdf(request, user_id):
member = Member.objects.filter(user_id=user_id).values('user_id',
'user__first_name',
'user__last_name',
'company_name',
'vat_number',
'address',
'postcode',
'city',
'user__email',
'telephone',
'subscription__type__name',
'subscription__type__limit',
).annotate(num_calculations=Count('user__calculations'))[0]
company_address = "{}, {} {}".format(member['address'], member['postcode'], member['city'])
buffer = BytesIO()
# Create the PDF object, using the BytesIO object as its "file."
p = canvas.Canvas(buffer, pagesize=A4)
p.setFont('Helvetica-Bold', 8)
p.drawString(70, 765, "{}".format(member['company_name']))
p.drawString(70, 731, "{}".format(company_address))
p.drawString(70, 714, "{}".format(member['telephone']))
p.drawString(335, 697, "{}".format(member['vat_number']))
p.drawString(335, 697, "{}".format(member['vat_number']))
p.save()
buffer.seek(0)
new_pdf = PdfFileReader(buffer)
existing_pdf = PdfFileReader(open('master/files/file.pdf', "rb"))
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output = PdfFileWriter()
output.addPage(page)
output_stream = open("master/files/new_file.pdf", "wb")
output.write(output_stream)
output_stream.close()
with open('master/files/new_file.pdf', 'r', encoding="Latin-1") as pdf:
response = HttpResponse(pdf.read(), content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename=some_file.pdf'
return response
The project tree is as follows : 项目树如下:
Thus, I create new file and then I open an existing file file.pdf
, and then I merge those two files. 因此,我创建了一个新文件,然后打开了一个现有文件
file.pdf
,然后合并了这两个文件。 At the end I create a file for output new_file.pdf
. 最后,我为输出
new_file.pdf
创建一个文件。
That works fine, but the problem is with the file which is returned. 效果很好,但是问题在于返回的文件。 If I run
export_to_pdf
function I should get new_file.pdf
. 如果我运行
export_to_pdf
函数,我应该得到new_file.pdf
。 I get that file, but the content of that file is only what I created with 我得到了该文件,但是该文件的内容仅是我使用该文件创建的
p = canvas.Canvas(buffer, pagesize=A4)
p.setFont('Helvetica-Bold', 8)
p.drawString(70, 765, "{}".format(member['company_name']))
p.drawString(70, 731, "{}".format(company_address))
p.drawString(70, 714, "{}".format(member['telephone']))
p.drawString(335, 697, "{}".format(member['vat_number']))
p.drawString(335, 697, "{}".format(member['vat_number']))
p.save()
There is no content of the merged file file.pdf
. 合并文件
file.pdf
没有内容。
But If I open the new_file.pdf
direct with clicking on it, I get everything like it should be. 但是,如果我直接通过单击打开
new_file.pdf
,我将获得应有的一切。
Any advice what I do wrong? 有什么建议我做错了吗?
I solved it. 我解决了 In case someone else has the same problem.
万一别人有同样的问题。
I just changed one line 我只换了一条线
with open('master/files/new_file.pdf', 'rb') as pdf:
Thus instead of r
I added rb
. 因此,我添加了
rb
而不是r
。 And then also the encoding was no more needed. 然后也不再需要编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.