[Python] Creating PDF with Python using FPDF char by char

Discussão em 'Python' iniciado por Stack, Junho 24, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to create a pdf with python and I want to put a text in pdf char by char. I can't find out how to do it and when it saves output pdf all of the characters are on each other. this is my code snippet:

    from fpdf import FPDF

    pdf = FPDF('P', 'mm', (100,100))
    # Add a page
    pdf.add_page()

    # set style and size of font
    # that you want in the pdf
    pdf.add_font('ariblk', '', "ArialBlack.ttf", uni=True)
    pdf.set_font("ariblk",size = int(50*0.8))
    text = [['a','b','c','d','e','w','q'],['f','g','h','i','j','k','l']]
    print("creating pdf...")
    line = 0
    for w in range(0,len(text)):
    for h in range(0,len(text[w])):
    # create a cell
    r = int (50)
    g = int (100)
    b = int (10)
    pdf.set_text_color(r, g, b)
    text_out = text[w][h]
    pdf.cell(0,line, txt = text_out, ln = 2)


    # save the pdf with name .pdf
    pdf.output(name = "img/output.pdf", dest='F')
    print("pdf created!")


    and this is what my code output is:

    (this is copy-paste from the output pdf): iljfbeqaghdckw (this is a screenshot of the output): [​IMG]

    Continue reading...

Compartilhe esta Página