我正在使用 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%
我认为这可能是因为我使用字符串作为封面而不是 url 用于 html。
所以,我添加了一个返回 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)
如果尝试从邮递员那里击中端点,则端点在外面可以正常工作。但是如果将它与 pdfkit 一起使用,它会抛出 ConnectionRefusedError。
如果我一起移除封面,它工作正常,它会创建 pdf。但是,需要封面。有什么建议么?