Well, well, criticize, I'm new to programming. Tell me how to make a multipage PDF, there are solutions in Google, for a specific text, and for me it is parsed from XML.

The table is continuous, not divided into pages. Help please, I can not solve this problem.

#!/usr/bin/python3 # -*- coding: utf-8 -*- from lxml import etree, objectify from reportlab.lib.styles import getSampleStyleSheet from reportlab.lib.units import inch, mm from reportlab.pdfgen.canvas import Canvas from reportlab.platypus import Paragraph, Table, TableStyle from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont from reportlab.lib import colors from reportlab.lib.pagesizes import letter pdfmetrics.registerFont(TTFont('FreeSans', 'FreeSans.ttf')) class PDFOrder(object): def __init__(self, xml_file, pdf_file): """Constructor""" self.xml_file = xml_file self.pdf_file = pdf_file self.xml_obj = self.getXMLObject() def coord(self, x, y, unit=1): x, y = x * unit, self.height - y * unit return x, y def createPDF(self): """ Создаём PDF на основании XML данных """ self.canvas = Canvas(self.pdf_file, pagesize=letter) width, self.height = letter styles = getSampleStyleSheet() xml = self.xml_obj order_number='<font name="FreeSans"><b>Отчет биллинговой системы </b></font>' p = Paragraph(order_number, styles["Normal"]) p.wrapOn(self.canvas, width, self.height) p.drawOn(self.canvas, *self.coord(18, 10, mm)) data = [] data.append(["Дата платежа", "Дата регистрации", "Адрес", "Лиц.счет", "Сумма"]) for payment in xml.answer.findall('payment'): row = [] row.append(payment.get('payment_date')) row.append(payment.get('recieved_date')) row.append(payment.get('address')) row.append(payment.get('account')) row.append(payment.get('amount')) data.append(row) data.append([]) t = Table(data) t.setStyle(TableStyle(( ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black), ('BOX', (0, 0), (-1, -1), 0.25, colors.black), ('TEXTCOLOR', (0, 0), (-1, -1), colors.blue), ('ALIGN', (0, 0), (-1, -1), 'CENTER'), ('FONTNAME', (0, 0), (-1, -1), 'FreeSans'), ('FONTSIZE', (0, 0), (-1, -1), 8) ))) t.wrapOn(self.canvas, width, self.height) t.drawOn(self.canvas, *self.coord(18, 385, mm)) #---------------------------------------------------------------------- def getXMLObject(self): """ Открываем XML документ и возвращаем lxml XML документ """ with open(self.xml_file) as f: xml = f.read() xml = bytes(bytearray(xml, encoding='utf-8')) return objectify.fromstring(xml) #---------------------------------------------------------------------- def savePDF(self): """ Сохраняем PDF """ if __name__ == "__main__": xml = "payments.xml" pdf = "letter2.pdf" doc = PDFOrder(xml, pdf) doc.createPDF() doc.savePDF()[ 
  • one
    1- it doesn't matter what the input is xml. Examples of creating a multi-page pdf with reportlab will not stop working wherever the input text appears 2 - you don't need all the code that you have to put in the question. Create a minimal example that demonstrates the problem of a minimal reproducible example Explicitly describe what you wanted to get and what exactly is happening instead. Explicitly minimal input example specify. - jfs
  • This is part of the code. I would like to understand how to divide the table into several pages. There was an idea to attach to _len and the number of lines, on the first page, let's say 20, on the other 60. But the lines can have different heights (depending on the data). I think this is a wry decision. The second option is to specify the allowable area to fill the page, in particular the bottom. How can I not understand this, or am I going in the wrong direction? - Dmitriy From
  • I would like to get a table that automatically went to another page and had a subtitle. - Dmitriy From

0