dousui6488 2018-10-23 14:12 采纳率: 100%
浏览 35

Magento 2 - 程序化invoice-pdf创建在第二次循环后停止

I'm trying to export a PDF and a XML file for every invoice matching my criteria. So far everything is working perfectly, except for one really weird behaviour.

In my Controller's execute function I have a foreach loop iterating over my result from DB like so

foreach($result as $row) {
    $this->writeXml($row['document_increment']);
    $this->writePdf($row['document_id'], $row['document_increment']);
}

which calls these two functions

    public function writeXml($document_increment){
       $content =  $this->buildXml();
       $myfile = fopen('/var/www/magento2/var/folder/_' . $document_increment. '.xml', "w");
       fwrite($myfile, $content);
       fclose($myfile);
    }

    public function writePdf($document_id, $document_increment){
       $invoice = $this->invoiceRepository->get($document_id);
       $pdf = $this->invoicePdfGeneratorService->execute($invoice);
       $this->fileFactory->create(
            '_' . $document_increment. '.pdf',
            $pdf->render(),
            \Magento\Framework\App\Filesystem\DirectoryList::VAR_ELO ,
            'application/pdf'
        );
    }

I am fully aware that some of this code is not Magento 2 best practice at all, but this is more of a "can we do it at all?" situation.

Now, I have set up an ajax request to call my Controller manually in order to test it from the backend. When I do that, Magento generates two invoice PDFs and two invoice XML files - and then stops without any error, warning or similar. I have narrowed it down to the writePdf() function. Removing it results in 5 (LIMIT in Query) XML files as is intended.

I have already ruled out the usual

  • execution time in php.ini
  • invalid MySQL result / error in query (since it works fine with XML)

and checked all logs, but there is neither an exception /error / something nor some config that I deemed responsible.

I think this is really weird since it breaks after the second loop. Does anyone have an idea what could cause this? I'm really thankful for every hint.

EDIT

I changed the triggering action from an ajax call to a cron job. Things get even weirder: now it works, the cron saves all PDF and XML files. Can someone explain to me / does someone have any idea why?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么