1

Getting the error below when I try to run wkhtmltopdf in a docker environment.

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

The code is seen below.

It is working in an Ubuntu 16.04 vagrant machine. However, when I move it to a docker environment, it fails with the error above. At first I was using a Python3.6 image then changed to an Ubuntu 16.04 image thinking that maybe wkhtmltopdf requires a fuller linux environment. But still no luck.

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
lukik
  • 3,919
  • 6
  • 46
  • 89
  • `SIGABRT` may indicate it is crashing all together. Go inside the container and then execute the command manually and see what results come up – Tarun Lalwani Sep 25 '17 at 07:19
  • wkhtml is properly installed as `root# wkhtmltopdf -V` gives `wkhtmltopdf 0.12.2.4`. I can also convert e.g. Google page to a PDF from within the console. When I run the `generate_invoice_pdf` command from console I get the same error though – lukik Sep 25 '17 at 07:47

1 Answers1

2

The problem is that wkhtmltopdf requires a DISPLAY / Xserver.

Using openlabs/docker-wkhtmltopdf as a base image could solve your problem.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • From the Docker file in that link, I think I can modify my Docker file to have `build-essential xorg libssl-dev libxrender-dev wget gdebi` and it should have me covered. Let me try – lukik Sep 25 '17 at 07:57
  • 2
    You might be interested in my question [How to use wkhtmltopdf with Docker](https://stackoverflow.com/q/46399914/562769) where I explicitly excluded that image in the question. – Martin Thoma Sep 25 '17 at 07:58
  • And probably also [How can I install the latest wkhtmltopdf on Ubuntu 16.04?](https://askubuntu.com/q/959152/10425) – Martin Thoma Sep 25 '17 at 08:03
  • @MartinThoma You should create a separte answer so people find your, better, solution more easily. – Ron Sep 10 '19 at 08:54