Reputation: 43
I want to make a script that can download a website as a PDF. the user should be able to enter a URL (https://stackoverflow.com/) and a file path for the PDF to download to (c:\Bob\PDF).
This is my code so far:
import requests
import pdfkit
url = input("Please enter the url of the file you want to download.")
pdf = pdfkit.from_url(url, "file.pdf")
path = input("Please enter the file path that you would like the file to
download to. c:\Bob\PDF is an example of a valid file path.")
print("Download starting.")
r = requests.get(pdf)
with open(path, 'wb') as f:
f.write(r.content)
For some reason, the PDF doesn't download. I think I need to first convert the web page to HTML, and then convert it to a PDF so it can download, but I'm not sure how to go about doing this. Any help is greatly appreciated.
Upvotes: 4
Views: 11020
Reputation: 130
First of all method
from_url from module 'pdfkit'
returns True
when called.
After executing this line pdf = pdfkit.from_url(url, "file.pdf")
value of pdf
is True
or False
depending on downloading and creating the file.
So this line
r = requests.get(pdf)
is evalueated to
r = requests.get(True)
Which cannot be executed properly.
Basically you only need to ask the user for url and path to the file
url = input("Please enter the url of the file you want to download.")
path = input("Please enter the file path ex. C:\Jim\Desktop")
file_name = input("Please enter file name")
if pdfkit.from_url(str(url), str(path + file_name)): # Check if method from_url returned True
print("Sucessfully created pdf from url")
else:
print("Something went wrong")
Upvotes: 7