pythonpdffpdf

insert a PDF with FPDF


It is possible to insert an image in a pdf document with FPDF using this command :

fpdf.image(name, x = None, y = None, w = 0, h = 0, type = '', link = '')

My question is: is it possible to do the same but instead of inserting an image I want to insert another PDF ( corresponding to a graphic) as an image ( or as a new page if it is the only possibility)

Thank you in advance


Solution

  • I'll try to answer the best I can; please let me know if it is not clear.

    In order to add a PDF inside another PDF, you can use the template method.

    From the docs:

    1. Create an instance of FPDF
    from fpdf import FPDF
    
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font('Arial', 'B', 16)
    
    
    1. Insert the PDF file at the right position and size
    pdf.template('the_pdf_to_insert.pdf', x = 10, y = 10, w = 80, h = 30, type = 'P')
    

    Make sure that the orientation is set the right way. Use either P for portrait or L for landscape.

    1. And the last step, save the file
    pdf.output('document_with_image.pdf')
    

    Hope it helps:)