Convert String to PDF in Python — Instantly!

Convert String to PDF in Python — Instantly!

fpdf Package can be used to create PDF files in Python.

Use the following command to install the fpdf package.

pip install fpdf

Sample Python Code:

from fpdf import FPDF

def createPDF( text, pdf_filename ):
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font( "Arial", size=12 )
    
    # Enable auto page break
    pdf.set_auto_page_break( auto=True, margin=15 )

    # Output text with multi_cell for handling line breaks
    pdf.multi_cell( 0, 10, text )

    pdf.output( pdf_filename )

tempText = '''- 1. How to change the Advertisement Location?
- To change the Advertisement Location, follow the below steps:
- Open your Account.
- Click the Change Advertisement button.
- 2 , Select Profile Details button on the Home Page.
- 4 , Expand the Advertisement section.
- Click the Update button to change the Advertisement Location.
- Update the Location field as per your design.
- 2 , How to change the Advertisement Size?
- To change the Advertisement Size, follow the below steps:
- Open your Account.
- Click the Change Advertisement button.
- 2 . Select Profile Details button on the Home Page.
- Expand the Advertisement section.
- 6 , Click the Update button to change the Advertisement Size.
- 5 , Update the Size field as per your design.'''
createPDF( tempText, "output.pdf" )
print( "PDF created successfully!" )

Leave a Reply