Python - Simple PDF Generation

In this tutorial we will create a Simple PDF Generation Using Python. Python has a design philosophy which emphasizes code readability. Python is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing. You can use this to convert the units value. So let's now do the coding.

Getting started

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/. Importing Modules After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy the code that I provided below and paste it inside the IDLE text editor
  1. from tkinter import *
  2. from reportlab.pdfgen import canvas

Setting up the Main Frame

After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.
  1. root = Tk()
  2. root.title("Python - Simple PDF Generator")
  3.  
  4. width = 700
  5. height = 400
  6.  
  7. screen_width = root.winfo_screenwidth()
  8. screen_height = root.winfo_screenheight()
  9. x = (screen_width/2) - (width/2)
  10. y = (screen_height/2) - (height/2)
  11. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  12. root.resizable(0, 0)

Designing the Layout

After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.
  1. #========================================FRAME==================================
  2. Top = Frame(root, width=700, bd=1, relief=SOLID)
  3. Top.pack(side=TOP)
  4. Mid = Frame(root, width=700, pady=30)
  5. Mid.pack(side=TOP)
  6. Content = Frame(root, width=700)
  7. Content.pack(side=TOP)
  8. #========================================LABEL WIDGET===========================
  9. lbl_title = Label(Top, text="Simple PDF Generator", width=700, font=('arial', 18))
  10. lbl_title.pack(fill=X)
  11. lbl_text = Label(Mid, text="Title", font=('arial', 18))
  12. lbl_text.grid(row=0, column=0)
  13. lbl_content = Label(Content, text="Content", font=('arial', 18))
  14. lbl_content.grid(row=0, column=0)
  15.  
  16. #========================================Text WIDGET===========================
  17. title = Entry(Mid, textvariable=TITLE, width=10, font=('arial', 18))
  18. title.grid(row=0, column=1)
  19. content = Text(Content, height=10, width=22)
  20. content.grid(row=1, columnspan=5, padx=10, column=0)
  21.  
  22. #========================================BUTTON WIDGET==========================
  23. btn1 = Button(Content, text="Save", width=20, command=Save)
  24. btn1.grid(row=3, columnspan=5, column=0, pady=10)

Creating the Main Function

This is the main function where the PDF generator is being processed. To do that just simply copy the code below then paste it inside the IDLE text editor.
  1. #========================================VARIABLES==============================
  2. TITLE = StringVar()
  3. CONTENT = StringVar()
  4. #========================================METHODS================================
  5.  
  6. def Save():
  7.     CONTENT = content.get("1.0", "end-1c")
  8.     c = canvas.Canvas("%s" % TITLE.get()+".pdf")
  9.     c.drawString(30,800, "%s" % CONTENT)
  10.     c.save()
  11.     TITLE.set("")
  12.     content.delete('1.0', END)

Initializing the Application

After finishing the function save the application as index.py. This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.
  1. #========================================INITIALIZATION===================================
  2. if __name__ == '__main__':
  3.     root.mainloop()
There you have it we just created a Simple PDF Generation Using Python. I hope that this simple tutorial help you expand your idea about Python programming. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment