sfWebResponse::setHttpHeader PHP Method

setHttpHeader() public method

Sets a HTTP header.
public setHttpHeader ( string $name, string $value, boolean $replace = true )
$name string HTTP header name
$value string Value (if null, remove the HTTP header)
$replace boolean Replace for the value
    public function setHttpHeader($name, $value, $replace = true)
    {
        $name = $this->normalizeHeaderName($name);
        if (null === $value) {
            unset($this->headers[$name]);
            return;
        }
        if ('Content-Type' == $name) {
            if ($replace || !$this->getHttpHeader('Content-Type', null)) {
                $this->setContentType($value);
            }
            return;
        }
        if (!$replace) {
            $current = isset($this->headers[$name]) ? $this->headers[$name] : '';
            $value = ($current ? $current . ', ' : '') . $value;
        }
        $this->headers[$name] = $value;
    }

Usage Example

 /**
  * This is the final Step in this 3 steps process:
  * <ol> 
  * <li>Step 1 consists in generating the XML bsaed raw data for the report. </li>
  * <li>Step 2 consists in merging this raw data with a XSL-FO template to give it
  * the presentation information. XSL-FO acts as an intermediate language used 
  * to render the final repor in any format with a last transformation. </li>
  * <li>Step 3 consists on rendering the XSL-FO representtion (the intermediate 
  * language) of the report to the PDF output fromat.</li>
  * <ol>
  * 
  * @param DOMDocument   $xslFo    The XSL-FO representation of the processed 
  * report, as we get it just after step 2 in this 3 steps process.
  * @param sfWebResponse $response The Response Object, should be passed all 
  * the way down from the action. So we can change some headers', ContentType
  * to 'application/pdf'.
  * 
  * @see %sf_plugins_dir%/NeatReports/config/sfExportGlobals.yml
  * @return String With the rendered report content. PDF Binary stream returned
  * by ApacheFop.
  */
 public function renderReport(DOMDocument $xslFo, sfWebResponse $response = null)
 {
     $response->setContentType('application/pdf');
     $response->setHttpHeader('Content-Disposition', "attachment; filename=report.pdf");
     $tStamp = microtime(true);
     $executable = stristr(PHP_OS, "WIN") ? 'fop.cmd' : 'fop';
     $output = array();
     $returnVar = 0;
     $foPath = __DIR__ . '/RenderPDF/tmp/report.fo.' . $tStamp;
     $pdfPath = __DIR__ . '/RenderPDF/tmp/report.pdf.' . $tStamp;
     try {
         file_put_contents($foPath, $xslFo->saveXML());
         $command = $this->_fopPath . $executable . ' -fo ' . $foPath . ' -pdf ' . $pdfPath;
         exec($command, $output, $returnVar);
         if ((bool) $returnVar) {
             throw new Exception(implode(PHP_EOL, $output), $returnVar);
         }
     } catch (Exception $e) {
         $ex = new Exception('Error executing Apache FOP, return value is pased to Exception Code.' . ' Command execution output follows:' . PHP_EOL . implode(PHP_EOL, $output), $returnVar, $e);
         throw $ex;
     }
     $out = file_get_contents($pdfPath);
     unlink($foPath);
     unlink($pdfPath);
     return $out;
 }
All Usage Examples Of sfWebResponse::setHttpHeader