I'm using a for loop to go down a list and create variables for the pdfkit module and it works just fine for the first two items on the list then has an error on the Third. This is my code:
import pdfkit
import time
link1 = "https://www."
link2 = ".com"
pdf = ".pdf"
for line in open('links.txt'):
print(line.strip("\n\r"))
newlink = link1 + line.strip("\n\r") + link2
print(newlink)
newpdf = line.strip("\n\r") + pdf
print(newpdf)
pdfkit.from_url(newlink, newpdf)
print('Finished')
And its pulling from This list:
bing
yahoo
google
It Successfully completes the first 2 items and prints a pdf on them then I get an error that says,
Traceback (most recent call last): File new.py, line 14 in module pdfkit.from_url(newlink, newpdf)
File "/usr/local/lib/python2.7/dist-packages/pdfkit/api.py", line 26 in from_return r.to_pdf(output_path)
File "/usr/local/lib/python2.7/dist-packages/pdfkit/pdfkit.py," line 156, in traise IOError('wkhtmltopdf reported an error:\n' + stderr)
IOError:wkhtmltopdf reported an error:
Does anybody know why I'm getting this error and how to fix it?
I wasn't able to find a fix for the network error on wkhtml
, yet.
But, I instead found an alternate plugin that works, called weasyprint
.
Here is an alternate version of your code with weasyprint implemented.
from weasyprint import HTML
link1 = "https://www."
link2 = ".com"
pdf = ".pdf"
for line in open('links.txt'):
print(line.strip("\n\r"))
newlink = link1 + line.strip("\n\r") + link2
print("newlink "+newlink)
newpdf = line.strip("\n\r") + pdf
print(newpdf)
HTML(newlink).write_pdf(newpdf)
print('Finished')
Hopefully this helps.