在docker环境中的wkhtmltopdf中的subprocess错误

当我尝试在Docker环境中运行wkhtmltopdf时,获取下面的错误。

subprocess.CalledProcessError: Command '['wkhtmltopdf', '--encoding', 'utf8', '--margin-top', '10', '--quiet', '/tmp/wkhtmltopdf85qv7fvc.html', '-']' died with <Signals.SIGABRT: 6>.

代码如下所示。

它正在使用Ubuntu 16.04stream浪汉机器。 但是,当我将它移动到docker环境时,它会失败,出现上述错误。 起初,我正在使用Python3.6的图像,然后更改为Ubuntu 16.04的图像,可能wkhtmltopdf需要一个更完善的Linux环境。 但是还是没有运气。

 from django.http import HttpRequest from wkhtmltopdf.views import PDFTemplateResponse def generate_invoice_pdf(download_pdf=False, **kwargs): """ Render html to PDF """ file_name = kwargs['file_name'] template = kwargs['template'] context = { "first_Name": "John", "last_name": "Doe" } # Create request object request = HttpRequest() params = { 'request': request, 'template': template, 'filename': os.path.basename(file_name), 'context': context, 'cmd_options': {'margin-top': 10, }, 'show_content_in_browser': True } response = PDFTemplateResponse(**params) # write the rendered content to a file with open(file_name, "wb") as f: f.write(response.rendered_content) # Part throwing the error if download_pdf: return response else: msg = 'PDF Generated: {}'.format(file_name) return msg 

问题是wkhtmltopdf需要DISPLAY / Xserver。

使用openlabs/docker-wkhtmltopdf作为基础映像可以解决您的问题。