1

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

Alpha
  • 321
  • 6
  • 16
  • Your question would be helped by including the actual problem you encounter. All we know is that you're "unable" to get this to work. So, do you get an error message? Is something wrong with the PDF? Please add this information to your question, and it might also be helpful if you have any code we can look at. – KIKO Software Jun 14 '23 at 23:23
  • Share the code you tried so that we can assist you – Rinsad Ahmed Jun 15 '23 at 03:40
  • There is no error message as I did not get anything in the cron job email. The PDF works fine when running the script from the browser. – Alpha Jun 15 '23 at 06:10
  • I have added all the code which is in connection with this problem. – Alpha Jun 15 '23 at 06:10
  • Clearly you didn't check for errors. PHP stores errors in a log file. Where those logs are depends on your system. To get an idea see: [Where does PHP store the error log?](https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php-5-apache-fastcgi-and-cpanel) – KIKO Software Jun 15 '23 at 06:16
  • Hi I have been able to get this working. wget --spider --user-agent="Mozilla" browser-url-of-of-the-scripts A user agent was to be defined and the script got running like from the browser – Alpha Jun 15 '23 at 08:25

1 Answers1

2

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)

dognose
  • 20,360
  • 9
  • 61
  • 107