PyPdfmaker 0.2 – work in progress

A tool to create quickly a pdf, through html, with python and pdfkit. It’s a work in progress and this is the code until now.

import tkinter as tk
import pdfkit
import os




def html_to_pdf(input, output, type='string'):
    """Convert HTML/webpage to PDF. For linux, install: sudo apt-get install wkhtmltopdf.

    Args:
        input (str): HTML Text, URL or file.
        output (str): Output file (.pdf).
        type (str): Types can be 'string', 'url' or 'file'.

    """
    if type == 'url':
        pdfkit.from_url(input, output)
    elif type == 'file':
        pdfkit.from_file(input, output)
    else:
        pdfkit.from_string(input, output) 


def style():
    s = """
<style>
body {
    padding:20;
    margin:20px;
    color: white;
    background: black;
    font-size: 2em;
    }</style>"""
    return s


def pdf():
    options = {
    'page-size': 'Letter',
    'margin-top': '0.0in',
    'margin-right': '0.0in',
    'margin-bottom': '0.0in',
    'margin-left': '0.0in',
    'encoding': "UTF-8",
    'custom-header': [
        ('Accept-Encoding', 'gzip')
    ],
    'cookie': [
        ('cookie-name1', 'cookie-value1'),
        ('cookie-name2', 'cookie-value2'),
    ],
    'outline-depth': 10,
    }

    x = "my.pdf"
    content = txbx.get("0.0", tk.END)
    content = style() + content
    pdfkit.from_string(content, x, options=options)
    print("pdf created")
    os.startfile("my.pdf")

class MyDialog:
    def __init__(self, parent, tag):
        self.tag = tag
        top = self.top = tk.Toplevel(parent)
        l = tk.Label(top, text="Value")
        l.pack()
        self.e = tk.Entry(top, width=90)
        self.e.pack(padx=5)
        self.e.focus()
        self.e.bind("<Return>", lambda x: self.ok())

        b = tk.Button(top, text="OK", command=self.ok)
        b.pack(pady=5)

    def ok(self):
        print("value is", self.e.get())
        txbx.insert(tk.END, f"<{self.tag}>{self.e.get()}</{self.tag}>\n")
        self.top.destroy()


root = tk.Tk()
root.title("Pdf generator 0.2 wip")
# 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()
txbx.bind("<Control-b>", lambda x: pdf())
txbx.bind("<Control-h>", lambda x: add_tag("h1"))
txbx.bind("<Control-p>", lambda x: add_tag("p"))


def add_tag(tag):
    if tag == "br":
        txbx.insert(tk.END, "<br>")
    elif tag == "img":
        txbx.insert(tk.END, "<img src='' />")
    else:
        MyDialog(root, tag)

menu = tk.Menu()
menu.add_command(label="<H1>", command=lambda: add_tag("h1"))
menu.add_command(label="<P>", command=lambda: add_tag("p"))
menu.add_command(label="<br>", command=lambda: add_tag("br"))
menu.add_command(label="<img>", command=lambda: add_tag("img"))
menu.add_command(label="PDF", command=pdf)
root.configure(menu=menu)

root.mainloop()

Video


Subscribe to the newsletter for updates
Tkinter templates
My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts