Issue
I am trying to get the wkhtmltopdf package to work on my Laravel 8 app that runs on Cloud Run for GCP. It uses NGINX and Docker. Here is the official documentation of the package: href="https://github.com/barryvdh/laravel-snappy" rel="nofollow noreferrer">https://github.com/barryvdh/laravel-snappy
I am able to get it to work locally on my own windows computer using the binary .exe file meant for windows but I can't seem to get it to work in a production environment on Cloud Run. I have tried several different tutorials and posts, a few different binary files too, but none of them work and none seem to be specifically for Cloud Run. I am very certain that Cloud Run apps run on Debian 11 (bullseye) OS. That is what it says on App Engine in GCP.
In the Dockerfile, I initially move all of my files into a directory called /app:
FROM php:7.4-fpm-alpine
RUN mkdir -p /run/nginx
COPY docker/nginx.conf /etc/nginx/nginx.conf
RUN mkdir -p /app
COPY . /app
Then, I install all necessary packages and install composer and it's packages:
RUN apk upgrade --update --no-cache && apk add \
nginx \
wget \
libpng-dev \
libzip-dev \
libxml2-dev \
nodejs \
npm \
&& docker-php-ext-install \
gd \
zip \
soap \
exif \
mysqli \
pdo \
pdo_mysql
RUN sh -c "wget http://getcomposer.org/composer.phar && chmod a+x composer.phar && mv composer.phar /usr/local/bin/composer"
RUN cd /app && \
/usr/local/bin/composer install --no-dev
RUN cd /app && \
/usr/local/bin/composer dump-autoload -o
So far, I have tried a few different methods. See methods listed below.
First method The first method involves installing the wkhtmltopdf package using composer. Whenever I try this method, it outright is unable to find the file. First, we install the package using composer:
composer require barryvdh/laravel-snappy
In the Dockerfile, I can also copy the binary file and make it executable fine with no errors. Here is my Dockerfile:
RUN cp /app/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf-amd64
RUN chmod +x /usr/local/bin/wkhtmltopdf-amd64
Then we must define the path to the binary file. Here is my config/snappy.php file:
'binary' => '/usr/local/bin/wkhtmltopdf-amd64',
Which returns the following error:
stderr: "sh: /usr/local/bin/wkhtmltopdf-amd64: not found
In conclusion to the first method, I'm certain that the file is there but it is not compatible to be executed on Cloud Run's environment.
Second Method I have even tried moving the binary file into the same directory (in my case, they app directory) instead of the usr local bin directory to confirm that the binary file is there but Cloud Run is unable to execute it:
RUN cp /app/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 /app/wkhtmltopdf/wkhtmltopdf-amd64
RUN chmod +x /app/wkhtmltopdf/wkhtmltopdf-amd64
I also even moved the binary file(s) into my working directory and then committed it to my repository (I know, that is not a good practice but wanted to see if it would work) so that I know for certain the file is located there. Here is the config/snappy.php file:
'binary' => '/app/wkhtmltopdf/wkhtmltopdf-amd64',
Which returns the following error:
stderr: "sh: /app/wkhtmltopdf/wkhtmltopdf-amd64: not found
In conclusion to the second method, I moved the binary file(s) to a location where I know for certain it can be found in because I am able to locate other files from config/snappy.php in the same directory.
Third Method I have also tried several different versions of the wkhtmltopdf binary file (specifically, only the Debian 11 Bullseye architectures) from here: https://wkhtmltopdf.org/downloads.html In this method, I download the file and then commit it to my repository (which is where I think I'm going wrong). I am able to get it to find the actual binary file itself using the path in the config/snappy.php. The file is found. But, none of the binary files I have tried seem to work live on my Cloud Run app. Whenever I run any of those binary files on my Cloud Run app, I get the following error:
line 2: syntax error: unexpected newline
So when it is able to find the .deb file (or any other binary file I have tried for the matter) defined in config/snappy.php it is unable to execute the file and generate the PDF when it is live in production. So I must be doing this wrong. I have been unable to find a solution that is specific for Cloud Run apps online. I'm starting to think that I'm just using the wrong binary file that is compatible with Cloud Run or that I'm downloading and installing the package incorrectly and should be installing it in the Dockerfile somehow. I have seen methods of installing wkhtmltopdf in Docker but none worked for me and/or none were compatible with my setup.
Solution
I ended up switching packages and used barryvdh/dompdf instead of the h4cc/wkhtmltopdf package: https://github.com/barryvdh/laravel-dompdf It started working right away. If anyone is running into the same issue with the wkhtmltopdf package in a production environment, I'd suggest just using the vanilla dompdf package instead (which I believe is a part of the wkhtmltopdf package). The dompdf package worked correctly right away with out the inconvenience of configuring it to work on a production server.
This solution does not necessarily solve the original problem. It's more of a work around. The reason why my team used wkhtmltopdf in the first place is because we could not generate QR codes in dompdf but we found a way to do this with wkhtmltopdf. Later on, my team member found a way to generate QR codes and use them using dompdf because we could not get wkhtmltopdf to work in production.
Out of curiosity, I would still like to see a real solution to this problem. It may help someone who is required to use wkhtmltopdf.
Answered By - Vinny Answer Checked By - Marilyn (WPSolving Volunteer)