Abhishek Kashyap
Abhishek Kashyap

Reputation: 23

pdfkit [WinError 740] The requested operation requires elevation python3

I would like to convert an HTML page into a PDF file, based on the given URL. I have tried using pdfkit, but it throws the following error:

[WinError 740] The requested operation requires elevation.

Code:

import pdfkit
path_wkthmltopdf = "D:\\w.exe"
config = pdfkit.configuration(wkhtmltopdf = path_wkthmltopdf )
pdfkit.from_url("http://www.google.com", 'd:\\out.pdf', configuration=config)

Output error:

n [42]: import pdfkit
path_wkthmltopdf = "D:\\w.exe"
config = pdfkit.configuration(wkhtmltopdf = path_wkthmltopdf )

pdfkit.from_url("http://www.google.com", 'd:\\out.pdf', configuration=config)
Traceback (most recent call last):

  File "<ipython-input-42-58323936ac63>", line 5, in <module>
    pdfkit.from_url("http://www.google.com", 'd:\\out.pdf', configuration=config)

  File "C:\Users\31081\AppData\Local\conda\conda\envs\ml\lib\site-packages\pdfkit\api.py", line 26, in from_url
    return r.to_pdf(output_path)

  File "C:\Users\31081\AppData\Local\conda\conda\envs\ml\lib\site-packages\pdfkit\pdfkit.py", line 129, in to_pdf
    stderr=subprocess.PIPE)

  File "C:\Users\31081\AppData\Local\conda\conda\envs\ml\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 143, in __init__
    super(SubprocessPopen, self).__init__(*args, **kwargs)

  File "C:\Users\31081\AppData\Local\conda\conda\envs\ml\lib\subprocess.py", line 729, in __init__
    restore_signals, start_new_session)

  File "C:\Users\31081\AppData\Local\conda\conda\envs\ml\lib\subprocess.py", line 1017, in _execute_child
    startupinfo)

OSError: [WinError 740] The requested operation requires elevation

Upvotes: 1

Views: 1523

Answers (3)

Chris
Chris

Reputation: 34740

Cause

You are most likely getting that error as a result of downloading the Installer version, but not installig it. Thus, when running your code with wkhtmltopdf in the configuration pointing to that executable (which requires elevation of privilege), you get the following error:

OSError: [WinError 740] The requested operation requires elevation

Solution

You could install wkhtmltopdf by running that Installer version and choosing the destination folder (let's say C:\Program Files). You will find the wkhtmltopdf.exe file that you need to add to your configuration inside the bin folder. Hence, you should use as follows:

config = pdfkit.configuration(wkhtmltopdf=r'C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe')
pdfkit.from_url('http://google.com', 'out.pdf', configuration=config)

Another solution would be to download the 7z Archive version, extract the files, in which you'll find wkhtmltopdf.exe under the bin folder as well.

Upvotes: 0

Muhammad Salman
Muhammad Salman

Reputation: 31

I encountered with this problem too, I solved it by running .exe after run this it will create new dir, go inside this dir then bin dir you will find a exe file there like C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe So, replace "D:\w.exe" to above kind of path in your code then it will work

Upvotes: 2

Chalk Master
Chalk Master

Reputation: 1

I ran into this issue when I had not gone through the full installation of the wkpdftohtml library. Once it was unpacked, this ran without need for elevation.

Upvotes: -1

Related Questions