Last active
August 29, 2015 14:06
-
-
Save lossendae/96c6e9257c51fe4a1b3e to your computer and use it in GitHub Desktop.
Symfony 1.x http header for PDF with DOMPDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class showDocumentAction extends sfActions | |
{ | |
/** | |
* Make sure that no other content will be output | |
*/ | |
public function preExecute() | |
{ | |
$this->setLayout(false); | |
sfConfig::set('sf_web_debug', false); | |
$this->getResponse()->clearHttpHeaders(); | |
} | |
/** | |
* @param sfRequest $request | |
* @return int|string | |
* @throws Exception | |
* @throws sfException | |
*/ | |
public function execute($request) | |
{ | |
$this->environment = sfConfig::get('sf_environment'); | |
$module = $request->getParameter('type'); | |
$action = $request->hasParameter('handler') ? $request->getParameter('handler') : 'print'; | |
$html = $this->getController()->getPresentationFor($module, $action); | |
$baseKey = 'mod_' . $module . '_' . $action . '_'; | |
$config = sfConfig::has($baseKey . 'config') ? sfConfig::get($baseKey . 'config') : false; | |
if($request->hasParameter('debug') && $this->environment == 'dev') | |
{ | |
return $this->renderText($html); | |
} | |
$document = new PluginPrefixDomPdf; | |
$output = $this->apply($document->generateFrom($html), $config); | |
// The PDF document should not be saved on the filesystem | |
if($output == 'stream') | |
{ | |
$this->renderText($document->stream($action)); | |
return sfView::NONE; | |
} | |
$document->setTargetFile($action); | |
if($request->hasParameter('overwrite') && file_exists($document->getFile()) && $this->environment == 'dev') | |
{ | |
unlink($document->getFile()); | |
} | |
if(!file_exists($document->getFile())) | |
{ | |
$document->save(); | |
} | |
return $this->output($document->getFile()); | |
} | |
/** | |
* @param DOMPDF $document | |
* @param $overrides | |
* @return string | |
*/ | |
protected function apply(DOMPDF $document, $overrides) | |
{ | |
if($overrides == false || !is_array($overrides)) | |
{ | |
return 'save'; | |
} | |
extract($overrides); | |
if(isset($width) && isset($height)) | |
{ | |
$document->set_paper(array(0, 0, $width, $height)); | |
} | |
if(isset($paper) && isset($orientation)) | |
{ | |
$document->set_paper($paper, $orientation); | |
} | |
return isset($output) ? $output : 'save'; | |
} | |
/** | |
* @param $filePath | |
* @return int | |
* @throws sfError404Exception | |
*/ | |
protected function output($filePath) | |
{ | |
$this->forward404Unless(file_exists($filePath)); | |
$this->getResponse()->setHttpHeader('Pragma', 'public'); | |
$this->getResponse()->setContentType(mime_content_type($filePath)); | |
$this->getResponse()->setHttpHeader('Content-Length', filesize($filePath)); | |
$this->getResponse()->setHttpHeader('Content-Transfer-Encoding', 'binary'); | |
$this->getResponse()->setHttpHeader('Content-Description', 'File Transfer'); | |
$this->getResponse()->setHttpHeader('Cache-Control', 'public, must-revalidate'); | |
$this->getResponse()->setHttpHeader('Content-Disposition', 'inline; filename="' . basename($filePath) . '"'); | |
$this->getResponse()->sendHttpHeaders(); | |
ob_end_flush(); | |
$this->getResponse()->setContent(readfile($filePath)); | |
return sfView::HEADER_ONLY; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment