python pdfkit不呈现封面

时间:2018-08-07 18:12:11

标签: python wkhtmltopdf pdfkit django-wkhtmltopdf

我正在使用pdfkit从html字符串创建pdf。

这是我的代码:

cover = '<html> <head></head><body><h1>hello<h1></body> </html>'
body = "<html> <head></head><body><h1>hello<h1></body> </html>"
pdf = dfkit.from_string(body, False, cover=cover, cover_first=True)

如果我不使用掩护,效果很好,如果我使用掩护,则会引发以下错误:

    Traceback (most recent call last):
psf_1         |   File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 35, in inner
psf_1         |     response = get_response(request)
psf_1         |   File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 158, in _get_response
psf_1         |     response = self.process_exception_by_middleware(e, request)
psf_1         |   File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 156, in _get_response
psf_1         |     response = response.render()
psf_1         |   File "/usr/local/lib/python3.5/dist-packages/django/template/response.py", line 106, in render
psf_1         |     self.content = self.rendered_content
psf_1         |   File "/usr/local/lib/python3.5/dist-packages/rest_framework/response.py", line 72, in rendered_content
psf_1         |     ret = renderer.render(self.data, accepted_media_type, context)
psf_1         |   File "/usr/local/lib/python3.5/dist-packages/rest_framework/renderers.py", line 724, in render
psf_1         |     context = self.get_context(data, accepted_media_type, renderer_context)
psf_1         |   File "/usr/local/lib/python3.5/dist-packages/rest_framework/renderers.py", line 681, in get_context
psf_1         |     'content': self.get_content(renderer, data, accepted_media_type, renderer_context),
psf_1         |   File "/usr/local/lib/python3.5/dist-packages/rest_framework/renderers.py", line 422, in get_content
psf_1         |     content = renderer.render(data, accepted_media_type, renderer_context)
psf_1         |   File "/usr/local/lib/python3.5/dist-packages/rest_framework/renderers.py", line 105, in render
psf_1         |     allow_nan=not self.strict, separators=separators
psf_1         |   File "/usr/local/lib/python3.5/dist-packages/rest_framework/utils/json.py", line 28, in dumps
psf_1         |     return json.dumps(*args, **kwargs)
psf_1         |   File "/usr/lib/python3.5/json/__init__.py", line 237, in dumps
psf_1         |     **kw).encode(obj)
psf_1         |   File "/usr/lib/python3.5/json/encoder.py", line 200, in encode
psf_1         |     chunks = list(chunks)
psf_1         |   File "/usr/lib/python3.5/json/encoder.py", line 436, in _iterencode
psf_1         |     o = _default(o)
psf_1         |   File "/usr/local/lib/python3.5/dist-packages/rest_framework/utils/encoders.py", line 68, in default
psf_1         |     return super(JSONEncoder, self).default(obj)
psf_1         |   File "/usr/lib/python3.5/json/encoder.py", line 179, in default
psf_1         |     raise TypeError(repr(o) + " is not JSON serializable")
psf_1         | TypeError: OSError('wkhtmltopdf reported an error:\nLoading pages (1/6)\n[>                                                           ] 0%\r[===>                                                        ] 5%\r[======>                                                     ] 10%\r[=======>                                                    ] 
12%\rError: Failed loading page http:/head>\r\n                                   <body>\r\n                                   <div class="container">\r\n                                      hello\r\n                                   </div>\r\n                                   </body>\r\n                                   </html> (sometimes it will work just to ignore this error with --load-error-handling ignore)\n[=========>                                                  ] 
15%

我认为这可能是因为我使用的是字符串而不是html的URL。

因此,我添加了一个返回html的终结点,即使它仍然引发相同的错误。

这是它的url版本:django端点

options = {'encoding': "UTF-8"}
cover = '{}/api/blahblah/blah'.format('localhost:8088')
body = "<html> <head></head><body><h1>hello<h1></body> </html>"
pdf = dfkit.from_string(body, False, options =options, cover=cover, cover_first=True)

如果尝试在post man上将其击中,则端点在外部可以正常工作。但是,如果将它与pdfkit一起使用,则会引发ConnectionRefusedError。

如果我一起取下盖子,它可以正常工作,它将创建pdf。但是,需要掩护。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

解决了这个问题。我完全忘记了我在docker容器中运行它,所以localhost从未解决。