0

我正在使用 symfony 1.4。我有申请清单。从那里我生成了一个 pdf 动作。生成后,我想用新标签或窗口打开 pdf。我怎么能那样做。

4

1 回答 1

1

如果您想在单击链接后在新窗口/选项卡中显示生成的 PDF,请尝试:

<a href="<?php echo url_for('@your_action_route') ?>" target="_blank">PDF</a>

或者

<?php echo link_to('PDF','@your_action_route',array('target' => '_blank')) ?>

如果您想从操作中回显链接:

sfApplicationConfiguration::getActive()->loadHelpers(array('Url'));

然后根据需要使用link_tourl_for(上面的示例)。


如果您想通过操作提供 PDF,而不是作为下载:

新建路由

pdf:
  url: /pdf/:filename.pdf/*
  param: { module: mymodule, action: show }

然后在 mymodule/actions/actions.class.php 中创建这个动作:

  public function executeServepdf(sfWebRequest $request)
  {
    $this->setLayout(false);
    sfConfig::set('sf_web_debug', false);

    $pdf = sfConfig::get('sf_web_dir').
            DIRECTORY_SEPARATOR.
            'uploads'.
            DIRECTORY_SEPARATOR.
            $request->getParameter('filename').'.pdf';

    $this->forward404Unless(file_exists($pdf));

    $this->getResponse()->clearHttpHeaders();
    $this->getResponse()->setHttpHeader('Pragma: public', true);
    $this->getResponse()->setContentType('application/pdf');
    $this->getResponse()->sendHttpHeaders();
    $this->getResponse()->setContent(readfile($pdf));

    return sfView::NONE;
  }

现在,如果您将 PDF 放入 web/uploads/your_file.pdf 目录并输入如下 URL:http://yoursite/pdf/your_file.pdf 您将直接在浏览器窗口中显示您的 PDF。

要将其作为新窗口/选项卡打开,请使用第一个示例中指出的元素target="_blank"属性。<a>

此外,也许这个网站会有所帮助:http ://www.symfony-zone.com/wordpress/2009/08/03/serving-pdf-files-through-symfony-controllers/我使用了它的例子。

于 2013-04-19T13:09:22.537 回答