1
votes

When using Symfony 1.0, is it possible to create a link to a static file i.e. a PDF or DOC file and link to it a custom URL?

I'd like to add a file e.g. example.pdf to the web/uploads/pdf folder on the server but then link to it with a URL such as: http://www.example.com/docs/old/test/example.pdf

This situation has arisen because some printed documentation from before the development of a new web site points to the specified URL.

1

1 Answers

2
votes

Ok, I have figured it out (with a great deal of help from this blog post. Thanks Thaberkern)

Create a route to grab the file name:

legacy_docs:
  url: /docs/:filename.pdf
  param: { module: default, action: display_pdf }

Then create an action to output the pdf:

  public function executeDisplayPdf()
  {
    $filename = $this->getRequestParameter('filename');

    $path_to_pdf = 'http://'.sfConfig::get('website_hostname').'/uploads/'.'/'.$filename.'.pdf';

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

    //set up the view
    $this->setLayout(false);
    sfConfig::set('sf_web_debug', false);

    return sfView::NONE;
  }