我有JSON文件,包含大量数据和4页PDF模板。我需要使用所有这些数据ang页面模板生成单个PDF文件。现在我的代码看起来像这样:
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()
它会生成一个PDF文件,其中包含所有需要的页面模板,但在所有页面上生成的数据都是从第一页的部件呈现的。如何仅为每个页面传播数据(某些行为第1页,某些数据为第2,3页,...,n,某些数据为最后一页)?对于以pdf编写,我使用pypdf
库。