Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using WeasyPrint with Pyinstaller #530

Closed
JauntyJJS opened this issue Nov 6, 2017 · 3 comments
Closed

Using WeasyPrint with Pyinstaller #530

JauntyJJS opened this issue Nov 6, 2017 · 3 comments
Milestone

Comments

@JauntyJJS
Copy link

JauntyJJS commented Nov 6, 2017

Hi,

I tried to convert a python script that depends on weasyprint into an exe file using pyinstaller.
The good news is that pyinstaller is able to create a folder called "weasyprint" containing the necessary dependencies such as the css files in weasyprint/css and files in weasyprint/tests/resources.

However, the code in html.py assumes that these file dependencies are in the same directory as the exe files.
Around line 36

if hasattr(sys, "frozen"): 
    root = os.path.dirname(sys.executable)
...
HTML5_UA_STYLESHEET = CSS(filename=os.path.join(root, 'css', 'html5_ua.css'))
HTML5_PH_STYLESHEET = CSS(filename=os.path.join(root, 'css', 'html5_ph.css'))

This may not be suitable if someone uses the pyinstaller to create a standalone exe file instead.

I suggest the following modification based on how pyinstaller works.

if hasattr(sys, "frozen"):
    # MEIPASS explanation:
    # https://pythonhosted.org/PyInstaller/#run-time-operation
    root = getattr(sys, '_MEIPASS', None) #To handle standalone exe case 
    if not root:
        root = os.path.dirname(sys.executable) #To handle exe with file dependencies in the same directory
    root = os.path.join(root,'weasyprint')
else:
    root = os.path.dirname(__file__)
HTML5_UA_STYLESHEET = CSS(filename=os.path.join(root, 'css', 'html5_ua.css'))
HTML5_PH_STYLESHEET = CSS(filename=os.path.join(root, 'css', 'html5_ph.css'))

Hope this helps.

PS:
Please note that weasyprint also depends on the file dependencies from the pyphen package as well.
For this package, pyinstaller will not create a folder called "pyphen" by default.
To do so, please write in the *.spec file

import pyphen
pyphen_root = os.path.dirname(pyphen.__file__)
pyphen_dictionaries = Tree(os.path.join(pyphen_root, 'dictionaries'), prefix = 'pyphen/dictionaries')

and put the variable pyphen_dictionary into

exe = EXE(pyz,
          pyphen_dictionaries,
          ... (other necessary inputs)
@liZe
Copy link
Member

liZe commented Nov 16, 2017

Fixed in #540.

@liZe liZe closed this as completed Nov 16, 2017
@liZe liZe added this to the v0.42 milestone Nov 16, 2017
@pfctdayelise
Copy link

@JauntyJJS Sorry for commenting on an old issue, but are you able to share any details about using PyInstaller with WeasyPrint? Like a blog or a gist? It seems like it would be super valuable info to share, especially if you did it on Windows! :)

@Cheaterman
Copy link

@pfctdayelise I don't know if you checked the PR #540 but there's some additional info on there thanks to @johnnymetz. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants