如何配置低质量使用wkhtmltopdf?

时间:2016-11-07 03:57:21

标签: python

这是我的代码,我使用python和flask:

pdf = pdfkit.from_url(url, False, options=options)
return send_file(one_file, attachment_filename=file_name,
                     as_attachment=True, mimetype="application/pdf")

pdf文件的大小太大,发送给用户太长了, 所以我想改变pdf的质量。 我用:

options = {'lowquality': True}

但错误:

Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/yangle/ksxing/exam/view/exams_mongo.py", line 1537, in export_result_pdf
    pdf = pdfkit.from_url(url, False, options=options)
  File "/usr/lib/python2.7/site-packages/pdfkit/api.py", line 24, in from_url
    return r.to_pdf(output_path)
  File "/usr/lib/python2.7/site-packages/pdfkit/pdfkit.py", line 116, in to_pdf
    raise IOError('wkhtmltopdf reported an error:\n' + stderr.decode('utf-8'))
IOError: wkhtmltopdf reported an error:
Loading pages (1/6)
Error: Failed loading page http://true (sometimes it will work just to ignore this error with --load-error-handling ignore)
Exit with code 1 due to network error: HostNotFoundError     ] 55%

怎么做?谢谢

2 个答案:

答案 0 :(得分:0)

你没有在任何地方传递选项。 尝试给出如下例子

options = {
'lowquality': True,
}
pdfkit.from_url('http://google.com', 'out.pdf', options=options)

答案 1 :(得分:0)

  

如果没有值的选项,请使用None,False或“for dict value

请改为尝试'lowquality': None