pythonpy2apppython-pdfkit

Problems when using py2app to freeze a python script which uses pdfkit


I recently created an application using pdfkit that takes a keyword entered by the user and makes various internet searches using that keyword. It then uses pdfkit to generate pdf files of each of the separate searches and saves them to a user-defined directory.

Everything works dandy when I run the code from terminal, however when I attempt to freeze the script using py2app, everything works fine up until it comes to actually saving the pdf's, at which the application does absolutely nothing.

I have tried to include both pdfkit and wkhtmltopdf in the setup.py file which py2app uses to create the application, but to no luck, I have tried listing them under the includes section like this:

'includes':['requests','pdfkit']

In the packages section:

'packages':['requests','pdfkit']

and even in the setup_requires section below:

setup_requires=['py2app', 'wkhtmltopdf']

But still the application does nothing. I presume its something to do with the fact that a dependency doesn't get carried over to the frozen application. However I am starting to rethink this as even when I create the app in alias mode (which claims to keep all dependencies), the same problem occurs.

Is this a known issue? Or has anyone found out a solution to this.

Many thanks. My full setup.py file is below:

    from setuptools import setup

APP = ['pdtest.py']
DATA_FILES = []
OPTIONS = {'argv_emulation': False, 'includes':['requests','pdfkit'],'packages':['requests','pdfkit'], 'iconfile':'icon.icns'}

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app', 'wkhtmltopdf'],
)

Solution

  • Ok found an answer so anyone who is having the same problem can use it.

    Basically the problem lies in the fact that pdfkit is just a wrapper for the module WKHTMLTOPDF.

    So basically all that happens when you call on PDFKIT is that it just asks WKHTMLTOPDF to do the work for it, however on my system for some reason, the script was able to find the WKHTMLTOPDF module when it was being ran from terminal, but was unable to when converted into an applet using py2app.

    What I needed to do was tell the script exactly where WKHTMLTOPDF was located, but first you need to know that information yourself, just type into terminal:

    which wkhtmltopdf
    

    and you should get a path returned. If you don't then maybe you should think about installing it first, that usually helps.

    Then you need to set the configuration file to look in that place for the wkhtmltopdf module, so just add this to your pdfkit script:

    config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
    

    Except replace the path after the "=' with whatever path the earlier which command returned. Then, everytime you call on pdfkit, you need to add this configuration, for example:

    normalpdf = pdfkit.from_url('URL, 'test.pdf',configuration = config)
    

    And that should solve the issue. Hope it helps!