0

I have written a few classes that use the fpdf2 library (version 2.5.7). I want to merge two of them to create a pdf with the methods from the two classes.

How can I do that?

Here is a basic example of my tentative:

EDIT

class SWISS_BILL(FPDF) :
    def __init__(self, path_image) :
        self.interligne         = 4
        self.path_image         = path_image

    def pdf_swissbill(self, data) :
        self.set_font('Arial', 'B', 16)
        self.cell(40, 10, data)
        
class QR_BILL(FPDF) :
    def __init__(self, ref_y_start) :
        self.loc_y_ref_bill         = ref_y_start
        self.interligne_9pt         = 9     # in pt
        
    def pdf_qr(self, data) :
        self.set_font('Arial', 'B', 16)
        self.cell(40, 10, data)


class TOTAL(QR_BILL, SWISS_BILL) :
    def __init__(self, path_image, ref_y_start, orientation, unit, format) :
        QR_BILL.__init__(ref_y_start)
        SWISS_BILL.__init__(path_image)
        self.orientation        = orientation
        self.unit               = unit
        self.format             = format

        
pdf = TOTAL('path_image', 60, 'P', 'mm', 'A4')
pdf.add_page()
pdf.pdf_qr('bonjour')
pdf.ln(h=60)
pdf.pdf_swissbill('merci')
pdf.output('test_merge_two_class2.pdf')

The error :

    QR_BILL.__init__(ref_y_start)
TypeError: QR_BILL.__init__() missing 1 required positional argument: 'ref_y_start'
Goncalves
  • 159
  • 9
  • 1
    The `__init__()` methods in `SWISS_BILL` and `QR_BILL` need to call `super().__init__()`. And the last call to `super().__init__()` seems wrong, since it will call one of the superclasses that you already initialized, but with wrong arguments. It doesn't jump over them to `FPDF`. – Barmar Dec 28 '22 at 16:11
  • @Barmar Thank you for answer. I have done modifications according to your comment. I am not sure that I understood it well. Can you say ma what it is wrong ? Thank's – Goncalves Dec 28 '22 at 16:21
  • No you didn't. You got rid of all the `super()` calls. If you're going to call `QR_BILL.__init__()` directly, you have to pass the `self` argument: `QR_BILL.__init__(self, ref_y_start)` – Barmar Dec 28 '22 at 16:24
  • You didn't add `super().__init__()` to the earlier `__init__()` methods. So nothing ever calls `FPDF.__init__()`. – Barmar Dec 28 '22 at 16:24
  • 1
    See https://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance – Barmar Dec 28 '22 at 16:25

0 Answers0