我编写了将html页面转换为pdf的脚本,该脚本生成了一个pdf,但它给出了错误"由于网络错误退出,代码为1:ContentNotFoundError">,我认为我们可以使用--load-error-handling ignore
忽略它,但我不知道如何将它与python一起使用。我正在使用Anaconda Spyder Python 3.7。我的脚本如下:
import pdfkit
try:
import urlparse
from urllib import urlencode
except: # For Python 3
import urllib.parse as urlparse
from urllib.parse import urlencode
# url = "http://stackoverflow.com/search?q=question"
# params = {'lang':'en','tag':'python'}
photoid = 3
seaid_destination = 4
search = "merge_recog"
url = "http://localhost:8080/" + search + "?"
params = {'photoid':photoid,'seaid_destination':seaid_destination}
url_parse = urlparse.urlparse(url)
query = url_parse.query #retrieve query item
url_dict = dict(urlparse.parse_qsl(query)) #convert the list to dict
url_dict.update(params) #add params to dict
url_new_query = urlencode(url_dict) #convert it in "percent-encoded"
url_parse = url_parse._replace(query=url_new_query) #replace old query with new query
new_url = urlparse.urlunparse(url_parse) #construct new url
print(new_url)
config = pdfkit.configuration(wkhtmltopdf="/usr/local/bin/wkhtmltopdf")
pdfkit.from_url(new_url, search+"_"+str(photoid)+"_"+str(seaid_destination)+'.pdf',
configuration = config)
这里也有同样的问题。正如您所注意到的,根据他们的文档,唯一可用的配置选项是wkhtmltopdf和meta_tag_prefix。
不幸的是,我最终做了:
try:
pdfkit.from_file(path, new_path)
except OSError as e:
if 'Done' not in str(e):
raise e
假设单词";完成";当pdfkit无法生成输出文件时不会出现,我确保单词"的存在;完成";在异常字符串中。解决方案相当糟糕。我希望他们能尽快解决。