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条)

报告相同问题?

悬赏问题

  • ¥20 materialstudio计算氢键脚本问题
  • ¥15 配置FPT报错,该如何处理
  • ¥15 请大家看一下这个代码咋写,一点思路都没有,最好能做一下,不要伪代码,有偿
  • ¥15 有偿请人帮写个安卓系统下禁止装软件及禁止拷入文件的程序
  • ¥100 用 H.265 对音视频硬编码 (CUDA)
  • ¥20 mpich安装完成后出问题
  • ¥15 stm32循迹小车代码问题
  • ¥15 输入一堆单词,使其去重输出
  • ¥15 qc代码,修改和添加东西
  • ¥50 Unity的粒子系统使用shadergraph(内置管线)制作的一个顶点偏移shader,但是粒子模型移动时,顶点也会偏移