无法使用Cron在PHP中生成PDF(DomPDF)。

huangapple go评论62阅读模式
英文:

Unable to Generate PDF in PHP using Cron (DomPDF)

问题

我正在尝试使用通过cPanel安装的Composer安装的DomPDF库生成PDF。

当我尝试使用浏览器生成PDF时,PDF会完美生成并存储到目录中。

Cron脚本的位置

/public_html/admin/crons/generate_pdf.php

Cron命令

php -q /home/userdirectory/public_html/admin/crons/generate_pdf.php

generate_pdf.php 调用库中的 Generate_PDF() 函数,该库位于

public_html/admin/requisites/_library.php

生成的PDF的存储目录

/public_html/admin/pdf/

这个函数 Generate_PDF() 调用了

../../../vendor/autoload.php

以加载 DomPDF 类

问题:由于我正在使用Cron,相对路径在这里无法正常工作。因此,我无法包含用于生成PDF的DomPDF类。

> generate_pdf.php (Cron脚本)

require('/home/userdirectory/public_html/admin/config.php');
// config.php 还将包含 _library.php

$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);
// 此函数在 _library.php 中可用

echo $GENERATE_PLAN['pdf_url'];

> _library.php

function Generate_PDF($con, $booking_id) {

require('/home/userdirectory/vendor/autoload.php');

// 实例化并使用 dompdf 类

$dompdf = new Dompdf();
$options = $dompdf->getOptions();
$options->setDefaultFont('Poppins');
$dompdf->setOptions($options);

$html = '';
$html .= '<html><head>
<style type="text/css">

// 用于HTML PDF的样式

</style>

$html .= '</head><body>';

$html .= '在此处放置所有常规HTML代码...当从浏览器调用脚本时正常工作';

$dompdf->loadHtml($html);

// 将HTML呈现为PDF

$dompdf->render();

$output = $dompdf->output();
$FILE_NAME = 'Planner - ' . $BOOKINF_ID;

// 保存到PDF目录

file_put_contents(PDF_DIRECTORY . $FILE_NAME . '.pdf', $output);

$PDF_URL = PDF_DIRECTORY_URL . $FILE_NAME . '.pdf';

// PDF_DIRECTORY_URL 在 config.php 文件中定义,用于获取访问生成的PDF的直接链接

return array("pdf_url" => $PDF_URL);

我尝试过其他选项,比如 dirname(__FILE__) 以及使用 绝对路径,但都没有生效。

请帮我解决这个问题。

英文:

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(&#39;/home/userdirectory/public_html/admin/config.php&#39;);
// config.php will include the _library.php as well

$START_DATE = WEEK_AHEAD;
$END_DATE = WEEK_AHEAD; //date(&quot;Y-m-d&quot;,strtotime(WEEK_AHEAD.&#39; + 1 days&#39;));

$UPCOMING_BOOKINGS = Get_Bookings_by_Date($con,$START_DATE,$END_DATE,&#39;&#39;,1,&#39;&#39;,true);

for($B=0;$B&lt;count($UPCOMING_BOOKINGS);$B++)
{
    $BOOKING_ID = $UPCOMING_BOOKINGS[$B][&#39;booking_id&#39;];
    $GENERATE_PLAN = Generate_PDF($con,$BOOKING_ID);
// This function above is available in _library.php
    
    echo $GENERATE_PLAN[&#39;pdf_url&#39;];
}

> _library.php

function Generate_PDF($con,$booking_id)
{
    
    require &#39;/home/userdirectory/vendor/autoload.php&#39;;
    
    // instantiate and use the dompdf class
    
    $dompdf = new Dompdf();
    $options = $dompdf-&gt;getOptions();
    $options-&gt;setDefaultFont(&#39;Poppins&#39;);
    //$options-&gt;set(&#39;isPhpEnabled&#39;, &#39;true&#39;); 
    $dompdf-&gt;setOptions($options);
    
    $html = &#39;&#39;;
    $html .= &#39;&lt;html&gt;&lt;head&gt;
      &lt;style type=&quot;text/css&quot;&gt;
      
      // styling for HTML PDF
      
      &lt;/style&gt;
    &#39;;
    
    $html .= &#39;&lt;/head&gt;&lt;body&gt;&#39;;
    
    $html .= &#39;All usual HTML code here... which is working fine when calling the script from Browser&#39;;        
    
    $dompdf-&gt;loadHtml($html);

    // Render the HTML as PDF
    $dompdf-&gt;render();
    
    $output = $dompdf-&gt;output();
    $FILE_NAME = &#39;Planner - &#39;.$BOOKINF_ID;
    // Save to PDF Directory
    
    file_put_contents(PDF_DIRECTORY.$FILE_NAME.&#39;.pdf&#39;, $output);
    
    $PDF_URL = PDF_DIRECTORY_URL.$FILE_NAME.&#39;.pdf&#39;;

// 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(&quot;pdf_url&quot;=&gt;$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

答案1

得分: 2

使用cron可能会出现几个问题,有时即使调用php可执行文件,也可能是权限不足或路径变量使用错误。

消除这些问题的简单方法是在crontab中使用wget
如果需要,还可以提供简单的HTTP身份验证。

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

根据cron作业的“敏感性”,您还可以使用.htaccess文件来限制仅允许在本地主机执行。

优点是,您可以保持所有路径和其他内容不变,以便进行“基于浏览器的访问”。(因为这就是wget正在做的事情)

英文:

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 &quot;/var/log/logFile_$(date +\%s).log&quot; -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)

huangapple
  • 本文由 发表于 2023年6月15日 06:40:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定