Python Forum
Universal(MACWINLIN) canvas pdf export
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Universal(MACWINLIN) canvas pdf export
#1
(I did a post earlier on this subject but I really want to find an answer on this)

My app is almost finished! The only problem that remains is exporting my musical score to pdf. I want to build a universal pdf export function that works on macOS, Windows, and Linux. My final executable should contain everything to export pdf so users don't have to install additional things.

I made a simple Tkinter canvas example in which we have to write the pdf export. How do other apps include the libraries needed to export pdf inside the executable? I searched so many times but all answers are using cmd-line apps like 'ps2pdf' or 'ps2pdfwr'. That's not what I want. Please can somebody share their knowledge? I want to know if it is possible in a self-contained way...

code example:
from tkinter import Tk, Canvas, Button
import random
from random import uniform

# root
root = Tk()
root.title('PianoScript')
scrwidth = root.winfo_screenwidth()
scrheight = root.winfo_screenheight()
root.geometry("%sx%s+%s+%s" % (int(scrwidth / 1.5), 
    int(scrheight / 1.25), 
    int(scrwidth / 6), 
    int(scrheight / 12)))
# canvas
canvas = Canvas(root, bg='white', relief='flat')
canvas.place(relwidth=1, relheight=1)
# buttons
button1 = Button(root, text='Generate PDF')
button1.pack()
button2 = Button(root, text='Generate Drawing')
button2.pack()

# Constants
MM = 3.5020519835841313
PAPER_HEIGHT = MM * 297  # a4 210x297 mm
PAPER_WIDTH = MM * 210

def generate_drawing():
    canvas.delete('all')
    canvas.create_rectangle(50,
        50,
        50+PAPER_WIDTH,
        50+PAPER_HEIGHT,
        outline='black',
        fill='white')
    def random_color():
        return ["#"+''.join([random.choice('ABCDEF0123456789') for i in range(6)])]
    for i in range(10):
        canvas.create_polygon(uniform(50,50+PAPER_WIDTH), 
            uniform(50,50+PAPER_HEIGHT), 
            uniform(50,50+PAPER_WIDTH), 
            uniform(50,50+PAPER_HEIGHT), 
            uniform(50,50+PAPER_WIDTH), 
            uniform(50,50+PAPER_HEIGHT), 
            uniform(50,50+PAPER_WIDTH), 
            uniform(50,50+PAPER_HEIGHT), 
            fill=random_color())


def generate_pdf():
    print('Generating PDF...')
    '''
    Here I want code that generates a pdf. We can 
    already use the canvas.postscript() function to
    export the drawing to postscript file.
    '''
    canvas.postscript(file='~/Desktop/randomdrawing.ps',
        x=50,
        y=50,
        width=PAPER_WIDTH,
        height=PAPER_HEIGHT,
        rotate=False)
    ''' solution here? '''


button1.configure(command=generate_pdf)
button2.configure(command=generate_drawing)
root.mainloop()
Reply


Messages In This Thread
Universal(MACWINLIN) canvas pdf export - by philipbergwerf - Aug-11-2022, 08:37 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020