douzhenchun6782 2013-10-01 17:41
浏览 31
已采纳

在symfony2应用程序中重定向到下一页时异步PDF生成?

Im working on a symfony2 project with sonata admin bundle.

I have a large pdf with images to generate after form input and writing to database. I start the pdf generation at the end of persisting data. Afterward the user get redirected.

My plan: I wanna trigger the pdf generation after persisting the data into database. But the redirect is waiting for the pdf document is ready. That takes to long. So the redirect should be done and at the same time the pdf generation should be triggered an running in background.

If the pdf is created successfully a notice message should appear in the header.

PDF-Generation is done with DOMPDF. So it's done directly done in php and not with separate command line tool.

Someone any ideas how to implement this?

I'm thinking about an ajax request after the redirect is done. Is that a good approach?

  • 写回答

2条回答 默认 最新

  • duanquannan0593 2013-11-06 17:35
    关注

    I've finally fix my issue the following way:

    1. Register EventListener: Using EventDispatcher which notify a special event where the subject (offer, order…) get's injected. Based on the subject the pdf document will be rendered.

      public function onOfferUpdate(OfferEvent $event)
      {
        $offer = $event->getOffer();
        $em = $this->container->get('doctrine')->getEntityManager();
      
        $job = new Job('acme:pdfgenerator:write', array('--entity="'.get_class($offer).'"', '--id='.$offer->getId()));
        $job->addRelatedEntity($offer);
        $em->persist($job);
        $em->flush();
      }
      
    2. Create the acme:pdfgenerator:write command: Create a command which get's executed into terminal which handle the final pdf creation. This command get's called with the given subject id. After lookup into database for the valid subject it will generate the pdf and place it to the filesystem.

    3. Using JobQueue: Install and start a simple JobQueue (I use JMSJobQueueBundle).

      app/console jms-job-queue:run  # To start listing to the job queue
      
    4. Optional: I've implement an ajax request which fetch the status of given job. So the user get's a flash message if document creation finished.

    Works now like a charm. The pdf creation will be executed in a seperate process and the user is able to see the next page without waiting couple of seconds.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率