Automating pagedjs-cli with PHP
Hello, my apologies if this question has been answered before or it is annoyingly simple. We are loving PagedJS and hoping it will be a solution to a long-running problem we have rendering multiple-page PDF documents.
Background
All our system runs on AWS and would like to use pagedjs-cli to print all our PDFs automatically. However we have an old, legacy, LAMP based system that we have to continue to work with for now. This means pagedjs-cli cannot be installed directly on our primary server. Instead we hope to use an other, up-to-date linux sever as a PDF "printer" i.e., our primary server generates the necessary HTML for the document to be printed and then sends a PHP request to the printer to generate the PDF that it then sends back to the primary server. We have something similar working right now using PHP ChromeToPdf.
To this end we have set up a standard Amazon Linux 2023 server on which we have installed Apache, PHP, git, node and npm (the last two following the instructions at https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html). Finally we have installed pagedjs-cli as instructed by https://pagedjs.org/documentation/2-getting-started-with-paged.js. Let's call this our "PagedJSprinter".
The problem
We have created a testFile.php on PagedJSprinter at /var/www/html/printer/ with the following content:
<?php
$url = 'http://oursite/path/to/html/files/filename.html';
$pdfFileName = 'filename_'.date("His",time()+36000).'.pdf';
$command = '/home/ec2-user/.nvm/versions/node/v18.18.0/bin/pagedjs-cli '.$url.' -o '.$pdfFileName.' 2>&1';
$output = shell_exec($command);
echo $output;
?>
When we run this test via the command line (e.g. $ php testFile.php
) it works perfectly i.e., it generates the PDF we want from html residing on our primary server.
However we cannot get this to run when called from our primary server. To test this we manually call the test file by entering it's URL into a browser e.g., http://PagedJSprinter/printer/testFile.php
The output is:
sh: line 1: pagedjs-cli: command not found
If we $command in testFile.php to:
$command = '/home/ec2-user/.nvm/versions/node/v18.18.0/bin/pagedjs-cli '.$url.' -o '.$pdfFileName.' 2>&1';
Then the output becomes:
sh: line 1: /home/ec2-user/.nvm/versions/node/v18.18.0/bin/pagedjs-cli: Permission denied
We are aware that when run in terminal the user is ec2-user and when run from the browser the user is apache. pagedjs-cli is installed in the /home/ec2-user/ and the testFile.php is in /var/www/html/printer. ec2-user has been added to the apache group and the user:group for all files and directories in both /home/ec2-user /var/www/html have been set to ec2-user:apache
Thank you for any help you might be able to give us.
Martin