Search code examples
pythondjangodjango-viewsreportlabpypdf

Open pdf file with Django


I'm trying to merge two pdf files in Django with PyPDF2 and 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 :

enter image description here

Thus, I create new file and then I open an existing file file.pdf, and then I merge those two files. At the end I create a file for output 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. 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.

But If I open the new_file.pdf direct with clicking on it, I get everything like it should be.

Any advice what I do wrong?


Solution

  • 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. And then also the encoding was no more needed.