I am trying to generate a PDF using DomPDF Library installed using Composer through cPanel.
When I try to generate the PDF using Browser, the PDF gets generated perfectly and stored to the directory.
Location of Cron Script
/public_html/admin/crons/generate_pdf.php
Cron Command
php -q /home/userdirectory/public_html/admin/crons/generate_pdf.php
generate_pdf.php calls a function Generate_PDF() from a library located at
public_html/admin/requisites/_library.php
Storage Directory of Generated PDFs
/public_html/admin/pdf/
This function Generate_PDF() calls the
../../../vendor/autoload.php
to load DomPDF Class
Issue : As I am using Cron, relative paths are not working here. Thus, I am unable to include the DomPDF Class for PDF Generation
generate_pdf.php (Cron Script)
require('/home/userdirectory/public_html/admin/config.php');
// config.php will include the _library.php as well
$START_DATE = WEEK_AHEAD;
$END_DATE = WEEK_AHEAD; //date("Y-m-d",strtotime(WEEK_AHEAD.' + 1 days'));
$UPCOMING_BOOKINGS = Get_Bookings_by_Date($con,$START_DATE,$END_DATE,'',1,'',true);
for($B=0;$B<count($UPCOMING_BOOKINGS);$B++)
{
$BOOKING_ID = $UPCOMING_BOOKINGS[$B]['booking_id'];
$GENERATE_PLAN = Generate_PDF($con,$BOOKING_ID);
// This function above is available in _library.php
echo $GENERATE_PLAN['pdf_url'];
}
_library.php
function Generate_PDF($con,$booking_id)
{
require '/home/userdirectory/vendor/autoload.php';
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$options = $dompdf->getOptions();
$options->setDefaultFont('Poppins');
//$options->set('isPhpEnabled', 'true');
$dompdf->setOptions($options);
$html = '';
$html .= '<html><head>
<style type="text/css">
// styling for HTML PDF
</style>
';
$html .= '</head><body>';
$html .= 'All usual HTML code here... which is working fine when calling the script from Browser';
$dompdf->loadHtml($html);
// Render the HTML as PDF
$dompdf->render();
$output = $dompdf->output();
$FILE_NAME = 'Planner - '.$BOOKINF_ID;
// Save to PDF Directory
file_put_contents(PDF_DIRECTORY.$FILE_NAME.'.pdf', $output);
$PDF_URL = PDF_DIRECTORY_URL.$FILE_NAME.'.pdf';
// PDF_DIRECTORY_URL is defined in the config.php file which is simply to get a direct link for accessing the generated PDF
return array("pdf_url"=>$PDF_URL);
}
I have tried other options like dirname(__FILE__)
as well as usage of absolute paths, but none of them work.
Please help me to get this work
With cron there could be several issues, sometimes even if you call the php executable it might be a lack of permissions or a wrong usage of PATH-variables.
A simple way to eliminate that issues, is using wget
from within crontab.
Could also provide simple http-auth if required.
5 3 * * * userToRunAs wget --http-user=user --http-passwd=password -O "/var/log/logFile_$(date +\%s).log" -o /dev/null https://domain/path/cronjob.php
based on the "sensitivity" of the cronjob, you may also use a .htaccess
file to restrict execution to your local host.
Advantage is, that you can leave all paths and everything as it should be for "browser based access". (Because that's what wget is doing)