N
N
Nikolai Vinogradov2016-08-04 13:46:01
Python
Nikolai Vinogradov, 2016-08-04 13:46:01

How to transfer data to different pages of a PDF file?

Good afternoon. Faced such a problem: you need to generate a pdf file with different templates for pages. There is this code:

self.line(self.data['sendr'], 10, 250,
              250, {'min_font': 7, 'max_string': 70})
    self.line(self.data['list_num'], 10, 450,
              25, {'min_font': 7, 'max_string': 70})
    self.line(self.data['send_date'], 10, 250,
              45, {'min_font': 7, 'max_string': 70})
    self.line(self.data['delivery_rate_sum_end'], 10, 450,
              450, {'min_font': 7, 'max_string': 70})

    self.can.save()
    self.packet.seek(0)

    new_pdf = PdfFileReader(self.packet)
    current_folder = os.path.dirname(os.path.abspath(__file__))
    existing_pdf = PdfFileReader(
        file("{0}/original/blank_103_1.pdf".format(current_folder), "rb"))
    output = PdfFileWriter()

    page = existing_pdf.getPage(0)
    page.mergePage(new_pdf.getPage(0))
    output.addPage(page)
    print "Page 1 complete"

    # four row page mail part
    for item in range(3, int(self.data['email_count']) - 2, 4):
        self.line(self.data['sends'][item]['rcpn'], 10, 350,
                  250, {'min_font': 7, 'max_string': 70})
        self.line(self.data['sends'][item]['mail_count'], 10, 450,
                  250)

        new_pdf2 = PdfFileReader(self.packet)
        existing_pdf2 = PdfFileReader(
            file("{0}/original/blank_103_2.pdf".format(current_folder), "rb"))

        page2 = existing_pdf2.getPage(0)
        page2.mergePage(new_pdf2.getPage(0))
        output.addPage(page2)
        print "Page ", item, " complete"

    # last page part
    self.line(self.data['sendr'], 10, 100,
              25, {'min_font': 7, 'max_string': 70})
    self.line(self.data['list_num'], 10, 70,
              45, {'min_font': 7, 'max_string': 70})
    self.line(self.data['send_date'], 10, 150,
              45, {'min_font': 7, 'max_string': 70})
    self.line(self.data['delivery_rate_sum_end'], 10, 450,
              450, {'min_font': 7, 'max_string': 70})

    new_pdf1 = PdfFileReader(self.packet)
    existing_pdf1 = PdfFileReader(
        file("{0}/original/blank_103_4.pdf".format(current_folder), "rb"))

    page1 = existing_pdf1.getPage(0)
    page1.mergePage(new_pdf1.getPage(0))
    output.addPage(page1)
    print "Last page complete"

    filename = int(time.time())
    outputStream = file(
        "{0}/pdf/f103/{1}.pdf".format(current_folder, filename), "wb")
    output.write(outputStream)
    outputStream.close()

It generates templates as it should, but for some reason the data goes to those on the first page. If we try to add
self.can.save()
    self.packet.seek(0)

Before each part, I get the error ValueError: redefining named object: u'toUnicodeCMap:AAAAAA+ArialMT'. If you put self.packet.seek(0) after each part, and before the end of self.canvas.save(), everything just hangs. Has anyone encountered a similar problem? I am using pypdf and reportlab.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Vinogradov, 2016-08-04
@Kibastus

The issue was resolved, for each block you need to create your own canvas and packet, and then everything is normally transferred to the desired sheets.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question