Forums

wkhtmltopdf

i tried install wkhtmltopdf but it failed https://github.com/wkhtmltopdf/wkhtmltopdf/blob/master/INSTALL.md#linux how I can do

Does this help?

https://www.pythonanywhere.com/wiki/InstallingNewModules

no because it needs to have wkhtmltopdf binaries need a precompiled static binary because the sudo it's not accept on pythonanywhere

Can you use one of the precompiled binaries? Try the Ubuntu trusty one... http://wkhtmltopdf.org/downloads.html

You could also try using abiword, with a

abiword --to=PDF -o myfile.pdf myfile.html

(ignore any errors printed out, it seems to work anyway.)

Can you use one of the precompiled binaries? Try the Ubuntu trusty one... http://wkhtmltopdf.org/downloads.html

which code i must use with the bash for download or clone and execute the Linux (Ubuntu Trusty) 32-bit   64-bit built on Ubuntu 14.04.1

by the way, you can also check out wheezy print

Hi, i'm trying to get wkhtmltopdf to work through xvfb-run, but getting the error below. Can anyone advise?

wkhtmltopdf.sh

xvfb-run -a -s "-screen 0 1920x1080x16" /home/guidingstarcc/bin/usr/local/bin/wkhtmltopdf "$@"

if i try to run that, i get the following error

./wkhtmltopdf.sh google.com google.pdf
xkbcommon: ERROR: failed to add default include path auto
Qt: Failed to create XKB context!
Use QT_XKB_CONFIG_ROOT environmental variable to provide an additional search path, add ':' as separator to provide several search paths and/or make sure that XKB configuration data directory contains recent en
ough contents, to update please see http://cgit.freedesktop.org/xkeyboard-config/ .
Loading page (1/2)
Segmentation fault

It would be great if wkhtmltopdf could be added to the stnadard image - it is a great tool as unlike many others it can deal with javascript...

note that the Qt error was not related

export QT_XKB_CONFIG_ROOT=/usr/share/X11/xkb & xvfb-run -a -e xvfb-run.log /home/guidingstarcc/bin/usr/local/bin/wkhtmltopdf pythonanywhere.com pa.pdfZ
Loading page (1/2)
Segmentation fault                                           ] 10%

So any guidance out there for getting wkhtmltopdf to work? I am using the 64 bit ubuntu trusty binary

Hi guidingstarcc,

I poked around a bit and I can confirm that the following code works with our experimental docker consoles, but not with the standard console (let us know if you want us to switch that on for you- also note that we don't have docker for webapps or scheduled tasks yet):

wget https://bitbucket.org/wkhtmltopdf/wkhtmltopdf/downloads/wkhtmltox-0.13.0-alpha-7b36694_linux-trusty-amd64.deb

dpkg-deb -x wkhtmltox-0.13.0-alpha-7b36694_linux-trusty-amd64.deb wkhtml-install

xvfb-run --auto-servernum ./wkhtml-install/usr/local/bin/wkhtmltopdf http://google.com google.pdf

and then navigating via the files tab to download and open google.pdf

I also saw the same errors as you with Qt and xkbcommon when doing the conversion.

Hi conrad,

thanks for the assistance!

I need this for use with django-wkhtmltopdf. It runs wkhtmltopdf in a subprocess; I am guessing that is a child of the web application shell/console/runtime, so docker is not available.

Can you confirm? Whats the timeframe on getting docker consoles running for webapps/scheduled tasks?

Subprocesses should be fine if you are running code from within the console. But we have not started implementing anything for webapps/scheduled tasks yet. (ie. your webapps and your scheduled tasks will not be able to use this as of yet).

ok - I needed this for web app so I will use a 3rd party API for now.

Hi, i'm trying to generate a pdf with wkhtmltopdf and it returns a Permission Error [Errno 13] when I try this : pdfkit.from_string(html, file, css = css_path,configuration = config, options = options) Any help ?

what is the exact error?

It's a Permission Denied error

are you trying to write to place that you can't write to?

No, I tried writing to the same places with open and there were no problems. Anyway I switched to weasy print already

libicu52 the error coming after install the package

We recommend weasyprint for PDF-related stuff on PythonAnywhere. PDFKit tries to use a graphical user interface, which is hard to get working.

I should correct what I was saying above; pdfkit uses the WebKit rendering engine and QT; in theory that could work using a virtual frame buffer (eg. a fake display that doesn't actually show anywhere, but does exist in memory). But right now WebKit doesn't work on PythonAnywhere due to the way our virtualization system works.

Does pythonanywhere support wkhtmltopdf now? I really need it working ASAP.

we do have a new virtualization system that may support this. if you'd like we can switch it on for you, and you could try the installation process yourself

I want to install wkhtmltopdf to my server as well.

How to get access to new virtualization system.

Plus, let me know if it's going to harm my current or stop my site which is running since few years.

I've switched on the new virtualization system for your account -- any new consoles you start will use it; websites and scheduled/always-on tasks will pick it up the next time they're started. Right now it won't be used in Jupyter notebooks or in SSH sessions, though that will change in the future.

It won't harm or stop your current site -- its features are superset of the old virtualization system's. We're planning to switch it on for everyone sometime over the coming month or two; the only reason we haven't done that yet is that we're testing how well it scales when there are thousands of people using it rather than just a few hundred.

Thanks for switching my account to new virtualization. Going to test it soon and then I will update if wkhtmltopdf is working fine or not.

Anyway, love the support here. I have seen responding you to Stackoverflow as well regarding Pythonanywhere.com questions.

Thanks for switching my account to new virtualization. Going to test it soon and then I will update if wkhtmltopdf is working fine or not.

Anyway, love the support here. I have seen responding you to Stackoverflow as well regarding Pythonanywhere.com questions. keep up the good work!

Can you switch to the new virtualization for my account please?

okay, I've switched that over for you. You will have to restart your consoles/webapps etc for it to take effect.

Does this mean that you will be able to access wkhtmltopdf through the path?

No, when we switch on the virtualisation option for you, it means that you can try to install and run wkhtmltopdf yourself.

Sorry if I am misunderstanding but why would that matter if wkhtmltopdf is already provided in the usr/bin?

Ah, I think we must have installed it in our latest system image. Try if that works I guess, but if it doesn't take a look at the last line of what I linked.

Well so far I haven't been able to get wkhtmltopdf to work, would the virtualization provide a better result, because frankly I don't fully understand what the problem is.

It doesn't look like anyone has reported success, so it may work or it may not. Let us know if you'd like us to switch your account to the new system so you can try it for yourself.

Yes I would like you to switch my account, please and thank you!

Okay, I've switched that on for you. Let us know how it goes!

Okay thanks!

And... It works!! I was able to download the wkhtmltopdf file myself and it executes fine, all I had to do was configure the path with pdfkit and it's good. Thank you guys for your help!

How do you add configure the path with pdfkit?? Let me know the path please

Could you clarify what you've done so far, and what errors (if any) your code is currently showing?

FileNotFoundError: [Errno 2] No such file or directory: b'/home/Nivas13897/.local/lib/python2.7/site-packages/django_wkhtmltopdf-3.3.0.dist-info.exe' NO MATCH During handling of the above exception, another exception occurred: NO MATCH Traceback (most recent call last): File "/usr/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/usr/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "/usr/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, callback_kwargs) File "./app/views.py", line 1225, in placeOrder getpdf = genorderpdf(ordr,ordername) File "./app/views.py", line 1944, in genorderpdf config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf) File "/home/Nivas13897/.local/lib/python3.8/site-packages/pdfkit/api.py", line 83, in configuration return Configuration(kwargs) File "/home/Nivas13897/.local/lib/python3.8/site-packages/pdfkit/configuration.py", line 24, in init raise IOError('No wkhtmltopdf executable found: "%s"\n' OSError: No wkhtmltopdf executable found: "b'/home/Nivas13897/.local/lib/python2.7/site-packages/django_wkhtmltopdf-3.3.0.dist-info.exe'" If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf![enter image description here][1]


My Code :

path_wkthmltopdf = b'/home/Nivas13897/.local/lib/python2.7/site-packages/django_wkhtmltopdf-3.3.0.dist-info' config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)

The path to wkhtmltopdf that you provide to the configuration needs to be the path to the wkhtmltopdf executable, not to the directory that the django-wkhtmltopdf module is installed.

YES EXACTLY ! That's why i asked in my earlier question "Let me know the wkhtmltopdf executable path", In my local system there is path "C:\Programfiles\wkhtmltopdf\bin\wkhtmltopdf.exe" , Similarly in pythonanywhere i don't know the executable path.Kindly give me the Path ?

My Code :


NEED TO REPLACE PYTHON ANYWHERE EXECUTABLE PATH

path_wkthmltopdf = b'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)

ADDING ERROR LOG :

Traceback (most recent call last): File "/usr/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/usr/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "/usr/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response response = wrapped_callback(request, callback_args, callback_kwargs) File "./app/views.py", line 1225, in placeOrder getpdf = genorderpdf(ordr,ordername) File "./app/views.py", line 1948, in genorderpdf pdfkit.from_file(Html_file_abs+ordername+'.html',pdf_path_abs+ordername+'.pdf',configuration=config) File "/home/Nivas13897/.local/lib/python3.8/site-packages/pdfkit/api.py", line 49, in from_file return r.to_pdf(output_path) File "/home/Nivas13897/.local/lib/python3.8/site-packages/pdfkit/pdfkit.py", line 159, in to_pdf raise IOError("wkhtmltopdf exited with non-zero code {0}. error:\n{1}".format(exit_code, stderr)) OSError: wkhtmltopdf exited with non-zero code -6. error: QXcbConnection: Could not connect to display NO MATCH*

Could you try again in a newly created console?

You mean have to try in new directory?

THE ANSWER IS here : "And... It works!! I was able to download the wkhtmltopdf file myself and it executes fine, all I had to do was configure the path with pdfkit and it's good. Thank you guys for your help!" from myles2 ! ALL I NEED TO KNOW IS WHICH PATH HE CONFIGURED with pdfkit.

Hi @nivas13897 sorry for the late response I did this a while ago so I'm not 100% sure what I did but if you give me a day or so I'll try to get back to you. What I can say is you will need the support team to switch your account to the virtualization system so that you can download it yourself. From what I can tell their latest system image isn't working properly or maybe I missed something.

Thanks for your valuable time and response @myles2. Sure take your time and let me know soon asap.,bcz i'm stucking with further development process.I hope don't need to download now already library is there in pythonanywere '/usr/bin/wkhtmltopdf' but i don't know the path , where is executable file for it to be configured in pdfkit??

In Local it works with : path_wkthmltopdf = b'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe' config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf).

Similarly we need to know the executable file path in pythonanywhere??

Thanks in advance @myles2 and @suppor- pythonanywhere

I mean start a new console and try it there. As has been said repeatedly in this forum thread, you will need to get the binary yourself. @conrad provided instructions further up in the thread.

ok...Thank you waiting for @conrad instructions

Can you switch my account to virtualization @conrad?

The instructions I was talking about are further up in this forum thread and I have already switched your virtualisation system over.

As mentioned here , not possible to install for webapp (Bash Console)??

No, it should be fine in a web app.

Great Thanks ! It works..!! Excellent Support thank you @glenn

Glad we could help!

Hi Glenn,

I was wondering if you could help me - running into a similar problem

2020-07-11 09:22:08,076: OSError: wkhtmltoimage exited with non-zero code -6. error: 2020-07-11 09:22:08,077: QXcbConnection: Could not connect to display 2020-07-11 09:22:08,077: 2020-07-11 09:22:08,077: 2020-07-11 09:22:08,077: You need to install xvfb(sudo apt-get install xvfb, yum install xorg-x11-server-Xvfb, etc), then add option: {"xvfb": ""}.

Many thanks,

I've switched on the new virtualization system for your account, so your code will use it in any new consoles; websites and scheduled/always-on tasks will pick it up the next time they're restarted.

If it still doesn't work, could you share the code you're using and any new error messages?

Thank you Giles, still not working for me unfortunately. Code and errors below

   import random
   import pandas as pd
   import imgkit
   import os
   import time

   def DataFrame_to_slack(data, imgcss, outputfile, imgformat):
    fn = str(random.random() * 100000000).split(".")[0] + ".html"

    pd.set_option('precision', 0)
    data = data.round(0)

    try:
        os.remove(fn)
    except:
        None
    text_file = open(fn, "a")

    # write the CSS
    text_file.write(imgcss)
    # write the HTML-ized Pandas DataFrame
    text_file.write(data.to_html())
    text_file.close()

    # # See IMGKit options for full configuration,
    # e.g. cropping of final image
    imgkitoptions = {"format": imgformat}

    imgkit.from_file(fn, outputfile, options=imgkitoptions)
    os.remove(fn)

Error:

2020-07-11 22:08:47,270: You need to install xvfb(sudo apt-get install xvfb, yum install xorg-x11-server-Xvfb, etc), then add option: {"xvfb": ""}.
2020-07-11 22:45:44,057: Traceback (most recent call last):
2020-07-11 22:45:44,058:   File "/usr/lib/python3.8/site-packages/bottle.py", line 862, in _handle
2020-07-11 22:45:44,058:     return route.call(**args)
2020-07-11 22:45:44,058:   File "/usr/lib/python3.8/site-packages/bottle.py", line 1742, in wrapper
2020-07-11 22:45:44,058:     rv = callback(*a, **ka)
2020-07-11 22:45:44,058:   File "/home/canonburygroup/.local/lib/python3.8/site-packages/truckpad/bottle/cors.py", line 53, in wrapper
2020-07-11 22:45:44,058:     return callback(*args, **kwargs)
2020-07-11 22:45:44,058:   File "/home/canonburygroup/mysite/bottle_app.py", line 207, in payrollrun
2020-07-11 22:45:44,058:     DataFrame_to_slack(gian, imgcss, "gian.png", "png")
2020-07-11 22:45:44,059:   File "/home/canonburygroup/mysite/dftoimg.py", line 62, in DataFrame_to_slack
2020-07-11 22:45:44,059:     imgkit.from_file(fn, outputfile, options=imgkitoptions)
2020-07-11 22:45:44,059:   File "/home/canonburygroup/.local/lib/python3.8/site-packages/imgkit/api.py", line 64, in from_file
2020-07-11 22:45:44,059:     return rtn.to_img(output_path)
2020-07-11 22:45:44,059:   File "/home/canonburygroup/.local/lib/python3.8/site-packages/imgkit/imgkit.py", line 246, in to_img
2020-07-11 22:45:44,059:     raise IOError("wkhtmltoimage exited with non-zero code {0}. error:\n{1}\n\n{2}".format(exit_code, stderr, xvfb_error))
2020-07-11 22:45:44,059: OSError: wkhtmltoimage exited with non-zero code -6. error:
2020-07-11 22:45:44,059: QXcbConnection: Could not connect to display 
2020-07-11 22:45:44,059: 
2020-07-11 22:45:44,059: 
2020-07-11 22:45:44,059: You need to install xvfb(sudo apt-get install xvfb, yum install xorg-x11-server-Xvfb, etc), then add option: {"xvfb": ""}.

Unfortunately this script gets all from my app and needs to run automatically

[edit by admin: formatting]

Could you try adding an from pyvirtualdisplay import Display to the start of your code, and then wrapping the imgkit.from_file inside a with Display():, and see if that helps?

Works great! Thank you very much

Excellent! Thanks for confirming.

I'm using pdfkit for a webapp, i wanted to push it on pythonanywhere but the problem is i have to install wkhtmltopdf. Which I think is not possible for now. Is there any alternative way to install it ? and I cannot use something else other than pdfkit. Thankyou!

I'm using pdfkit for a webapp, i wanted to push it on pythonanywhere but the problem is i have to install wkhtmltopdf. Which I think is not possible for now. Is there any alternative way to install it ? and I cannot use something else other than pdfkit. Thankyou!

wkhtmltopdf is installed in our latest system image, but it only works with our new virtualisation system. We can update both for you. Note that switching to the new system image may break some of your existing code that worked on older versions. You may need to recreate any virtualenvs that you're using and you will need to start new consoles and reload your web apps. You will also need to kill any ipython notebook processes that are running by using the "Fetch processes" button on the Consoles page.

I only have a very basic Django web application running, I hope it will not break it. But anyways i need it. How can I proceed @glenn.

wkhtmltopdf will not work until the changes have been made to your account. If you'd like us to make the changes, let us know.

yes, @glenn please turn on virtualization

No problem. I have updated your account.

I'm getting an error during the execution of pdfkit. code:

pdf = pdfkit.from_url(url, False, options=options)
response = HttpResponse(pdf,content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="reportcard.pdf"
return response

Error

wkhtmltopdf exited with non-zero code -6. error:
QXcbConnection: Could not connect to display

Locally I'm using this version of wkhtmltopdf, and it works fine: wkhtmltopdf 0.12.6

& on pythonanywhere the version is a bit older wkhtmltopdf 0.12.2.4.

I saw on stackoverflow upgrading to the latest version might work: https://stackoverflow.com/a/51789478/8252429

Can you please upgrade the version?

We will do it when we create and deploy our next system image later this year.

My account previously got upgraded for new virtualization but I thought I will check it on the next site update but later due to COVID issue, I stopped my business so I had to shut down this website for few months and then I upgraded my account again. So basically my account switched to basic like free account and then to $5 monthly plan. I'm wondering if I still get the wkhtmltopdf automatically upgraded if not then please enable it for me as well.

Yeah, seems like my account got downgraded again. Please enable new virtualization for me as soon as possible so I can do all the updates this weekend.

Hi I am on the beginner account, and trying out pythonanywhere. I also have got as far as using the system /usr/bin/wkhtmltopdf, but end up with the error wkhtmltopdf exited with non-zero code -6. error: QXcbConnection: Could not connect to display Is it possible to avoid this, or should I switch to weasyprint which I see is installed?

@Manish54213 Your account is still set to run on new virtualization.

Thank you @fjl for confirming you saved my weekend by this quick response and @glenn + all devs for such an awesome update with this virtualization. My app is running perfectly without any issue. Pythonanywhere supporting WKHTMLTOPDF perfectly. Awesome!!!!!!

i have run into the same problem with wkhtmltopdf on our eu.pythonanywhere.com account

unfortunately i can't login with the eu credentials for the forum :(

@fjl is there any chance you might change our eu account (controlaltcoop) to the new virtualization system so we can test if wktohtml works for us?

Sure, no problem! That's done now.

BTW there are separate EU forums at http://eu.pythonanywhere.com/forums/ where you can use your EU account's credentials.

hi i am getting the error as 'OSError: wkhtmltopdf exited with non-zero code -11. error:' have followed all the steps in this post and also from post "https://stackoverflow.com/questions/62783277/how-can-i-create-a-pdf-file-from-jinja-template-in-flask-question-tagged-pytho" can some one please help me.

@rohitratnakumar I switched your account to the new virtualization. Reload your web app or start a fresh console and check if it helped.

Hello, please can this account be switched to the new virtualisation? I have a webapp pdf export function coded with pdfkit, which – no surprise now having read this thread – doesn’t work on PA. Thanks!

FWIW – I saw wkhtmltopdf was installed on PA so went ahead with pdfkit, having baulked at the dependencies of weasleyprint I’d need locally. Might be worth making a note on https://help.pythonanywhere.com/pages/PDF

No problem. I have enabled it for your account.

Thanks Glenn.

Here’s the recipe that worked for me. First, in a bash console –

  • cat /etc/*release ← find out linux distribution, in this case Ubtuntu 16
  • wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.xenial_amd64.deb ← get the corresponding download from https://wkhtmltopdf.org/downloads.html
  • mkdir wkhtml-install ← create a directory to contain the wkhtml executable etc.
  • dpkg-deb -x wkhtmltox_0.12.6-1.xenial_amd64.deb wkhtml-install ← extract the download into the directory
  • Find the executable in there. Within that folder, it’s at usr/local/bin/wkhtmltopdf

Then, in the python web app code –

  • PDFKIT_CONFIGURATION = pdfkit.configuration() if settings.DEBUG else pdfkit.configuration(wkhtmltopdf='/home/<your user name>/wkhtml-install/usr/local/bin/wkhtmltopdf’)
  • Wherever you call pdfkit, add configuration=PDFKIT_CONFIGURATION

e.g. pdfkit.from_string(html, tempfile.name, configuration=PDFKIT_CONFIGURATION)

Thanks!

Hi @glenn i need to run wkhtmltopdf, can you turn on virtualization for my account, thanks !

No problem. It's done for you.

Can you switch my account to virtualization, please @conrad?

No problem, I have updated your account.

thank youu

Hi @gleen,

my application produces a PDF and I have this error:

OSError: wkhtmltopdf exited with non-zero code -6. error: QXcbConnection: Could not connect to display

I think it is the same problem of the other guys, can you turn on virtualization for my account too?

Thank you very much!!

Please can you switch my account to virtualization as well? Thanks!

Sure! That's done now for both of you. It will be enabled in any new consoles you start; websites and always-on/scheduled tasks will pick it up the next time they are restarted.