Welcome folks today in this blog post we will be exporting raw text to pdf document
in tkinter using fpdf
library. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the below library
using the below command as shown below
pip install fpdf
Now we need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import tkinter as tk from tkinter import filedialog import fpdf # Create the main window window = tk.Tk() window.title("Text to PDF Converter") # Create a Text widget to display the text entered by the user text = tk.Text(window) text.pack() # Create a Button widget to trigger the conversion button = tk.Button(window, text="Convert to PDF", command=convert_to_pdf) button.pack() # Run the main loop window.mainloop() |
As you can see we are importing the tkinter
and fpdf
library. And then we are displaying the input
field where the user will enter the raw
text and then we have the button
to export the text to the pdf
document. And if we click the button
then we will be showing the filedialog
to save the pdf
document with the custom filename. And then we are adding the text field
and button
widgets to the screen using the pack()
method.
And now we need to write the convert_to_pdf()
function where we will be converting the raw text to pdf
document as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
def convert_to_pdf(): # Get the text from the Text widget text_content = text.get("1.0", "end") # Create a PDF object pdf = fpdf.FPDF() # Add a page pdf.add_page() # Set the font pdf.set_font("Arial", size=12) # Add the text to the PDF pdf.multi_cell(0, 10, txt=text_content) # Save the PDF to a file filepath = filedialog.asksaveasfilename(defaultextension=".pdf") pdf.output(filepath) |
As you can see inside the above function we are getting the data
submitted inside the input field
and then we are making a new pdf
document using the fpdf()
constructor and then inside it we are adding the new page inside the pdf
document using the add_page()
method and then we are setting the font
inside the pdf document using the set_font()
method and then we are inserting the text
inside the pdf document using the multi_cell()
method here inside this method we are passing the x
and y
coordinates and then thirdly we are passing the actual
text to be inserted inside the pdf document. And lastly we are saving the pdf
document by showing a custom filedialog
as shown below
FULL SOURCE CODE
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import tkinter as tk from tkinter import filedialog import fpdf # Create the main window window = tk.Tk() window.title("Text to PDF Converter") # Create a Text widget to display the text entered by the user text = tk.Text(window) text.pack() # Create a function to convert the text to a PDF document def convert_to_pdf(): # Get the text from the Text widget text_content = text.get("1.0", "end") # Create a PDF object pdf = fpdf.FPDF() # Add a page pdf.add_page() # Set the font pdf.set_font("Arial", size=12) # Add the text to the PDF pdf.multi_cell(0, 10, txt=text_content) # Save the PDF to a file filepath = filedialog.asksaveasfilename(defaultextension=".pdf") pdf.output(filepath) # Create a Button widget to trigger the conversion button = tk.Button(window, text="Convert to PDF", command=convert_to_pdf) button.pack() # Run the main loop window.mainloop() |