How to generate a pdf from text or html in tkinter

A simple script to generate a pdf from a text or an html. You need to install pdfkit

pip install pdfkit

https://github.com/formazione/python_book

https://github.com/formazione/pypdf

The app to create pdf from text or html
the outcome
import tkinter as tk
import pdfkit
import os


def pdf(event):
    filename = "my.pdf"
    content = txbx.get("0.0", tk.END)

    # you can go to the next line with this code
    content = content.replace("\n", "<br>")
    pdfkit.from_string(content, filename)
    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, height=20, insertbackground="white")
txbx['font'] = "Arial 14"
txbx['bg'] = "black"
txbx['fg'] = "white"
txbx['borderwidth'] = 2
txbx.pack(fill=tk.BOTH, expand=1)
txbx.focus()
# Command that creates the pdf from the text
txbx.bind("<Control-b>", pdf)

root.mainloop()
The video with the exaplanations

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

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.