Tkinter to make pdf fast and free with text or html

You need to install pdfkit with

pip install pdfkit

in the command line or in powershell

If you want a no-brainer and fast way to make a pdf, here is the code:

import tkinter as tk
import pdfkit
import os


def pdf(event):
	x = "my.pdf"
	content = txbx.get("0.0", tk.END)
	pdfkit.from_string(content, x)
	print("pdf created")
	os.startfile("my.pdf")


root = tk.Tk()
# WIDGETS: text box => Text class of tkinter (tk)
label = tk.Label(root, text="CTRL + b to make a page (use also html)")
label.pack()
txbx = tk.Text(root)
txbx['font'] = "Arial 20"
txbx['bg'] = "cyan"
txbx['borderwidth'] = 2
txbx.pack(fill=tk.BOTH, expand=1)
txbx.focus()
txbx.bind("<Control-b>", pdf)

root.mainloop()

Output

You may need to install … wkhtmltopdf

To use pdfkit you may be asked to install wkhtmltopdf.

Then go in the environment variables and do like it is shown below:

 

 

Utilities

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.