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 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码