javascriptpythondockerwkhtmltopdfpdfkit

pdfkit and wkhtml2pdf not generating footer in docker environment


I have a Python script which uses Jinja2 to create some html, which is then converted to a pdf using the pip package pdfkit and a local/docker installation of wkhtmltopdf. The html footer contains some simple javascript to update the page number for the footer on each page. This is working great locally, however when I run it in a docker environment (which is run in AWS Fargate) it does not work. I think it is one of two things:

I am asking because I cant figure this out and perhaps someone has had this problem before.

Adding RUN apt-get install -y nodejs to the dockerfile does not work.

Python:

options = {
        "footer-html": "footer.html",
        "page-size": "A4",
        "encoding": "UTF-8",
        "enable-local-file-access": True,
        "margin-bottom": "0.5in", 
        "dpi": 96,
}

with open(output_html_file) as f:
        pdfkit.from_file(
                f,
                output_pdf_path,
                options=options,
        )

HTML with JavaScript (I think the code is fine, it is just not running. Putting here for visibility):

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <script>
        function substitutePdfVariables () {

            function getParameterByName (name) {
                var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
                return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
            }

            function substitute (name) {
                var value = getParameterByName(name);
                var elements = document.getElementsByClassName(name);

                for (var i = 0; elements && i < elements.length; i++) {
                    elements[i].textContent = value;
                }
            }

            ['frompage', 'topage', 'page', 'webpage', 'section', 'subsection', 'subsubsection']
                .forEach(function (param) {
                    substitute(param);
                });

            // Check the page number
            var pageNumber = parseInt(getParameterByName('page'));

            // Hide the footer when page number is 1
            var footer = document.getElementById('auto-footer');
            if (pageNumber === 1) {
                footer.style.display = 'none';
            }
        }
    </script>
</head>

<body onload="substitutePdfVariables()">
    <div id="auto-footer" class="row">
        <p><span class="page"></span></p>
    </div>
</body>

Dockerfile:

FROM osgeo/gdal:ubuntu-small-3.6.3

EXPOSE 8080
ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update \
    && apt-get install -y vim python3-pip gcc libgeos-dev wkhtmltopdf git
RUN python3 -m pip install --upgrade pip setuptools wheel
RUN apt-get install -y nodejs
ADD app app

Solution

  • Turns out I had wkhtmltopdf version 0.12.6 installed when I needed it installed with patched qt. I changed the docker file to include this which worked as a solution:

    RUN apt-get install -y wget wkhtmltopdf
    RUN wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.jammy_amd64.deb
    RUN apt install -f -y  ./wkhtmltox_0.12.6.1-2.jammy_amd64.deb