douxi3085 2017-05-03 15:55
浏览 39

在显示之前检查PDF是否已创建?

I am creating a PDF, from a template, using JasperPHP.

I then immediately try to display it.

How can I ensure that it is completely created, before displaying?

I am checking that the file exists but the PDF is corrupt, at this point, so I am assuming it is still being created.

I have also tried using flock, to open it with an exclusive lock, but this seems to work, even when the PDF is not completely rendered.

This is what I am doing (the dd is just for testing, it never gets here):

/**
     * Check that we can lock a file
     * this ensures that it has been completely created.
     *
     * @param $filename
     * @return bool
     */
    function stillWriting($filename)
    {
        $stillWriting = true;

        if($fp = fopen($filename, "r+"))
        {
            if (flock($fp, LOCK_EX)) // Try to get an exclusive lock on the file
            {
                flock($fp, LOCK_UN); // Unlock - we're just checking
                $stillWriting = false;
            }else{
                dd('failed');
            }
            fclose($fp);
        }
        return $stillWriting;
    }

Mick

Edit - added more detail

This is where the PDF gets created:

exec($this->the_command, $output, $return_var);

I am using https://github.com/cossou/JasperPHP

It executes JasperStart [https://sourceforge.net/projects/

This is where the PDF is displayed:

return Response::make(file_get_contents($this->outputFilename .'.pdf'), 200, [ 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'inline; filename="' . $this->outputFilename .'.pdf' .'"' ]);

The actual problem that I am getting is that Chrome says "Failed to load PDF document", I am guessing from this, that it is a timing problem. The file opens correctly If I then go to Windows and double click.

  • 写回答

1条回答 默认 最新

  • douchibu7040 2017-05-04 14:18
    关注

    JasperPHP has a flag $background = true.

    If I set this to false it looks like it waits for the compile to finish before continuing.

    Mick

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值