public function renderPdf($data, $pageSize = 'a4', $pageOrientation = 'portrait') { $previous_error_reporting = ini_get('error_reporting'); // to avoid the complaining for fixing DOMPDF_FONT_DIR at // siwappConfiguration.class.php ini_set('error_reporting', E_ALL ^ E_NOTICE); $input_data = $this->render($data, true); sfCoreAutoload::getInstance()->unregister(); sfAutoload::getInstance()->unregister(); if (class_exists('sfAutoloadAgain')) { sfAutoloadAgain::getInstance()->unregister(); } require_once sfConfig::get('sf_root_dir') . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'sfDomPDFPlugin' . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'sfDomPDFPlugin.class.php'; $q = new sfDomPDFPlugin($input_data); sfCoreAutoload::getInstance()->register(); sfAutoload::getInstance()->register(); if (class_exists('sfAutoloadAgain')) { sfAutoloadAgain::getInstance()->register(); } $q->setProtocol('http://'); $q->setHost($_SERVER['HTTP_HOST']); $q->setPaper($pageSize, $pageOrientation); $q->render(); ini_set('error_reporting', $previous_error_reporting); return $q->getPdf(); }
/** * Render the presentation. * * When the controller render mode is sfView::RENDER_CLIENT, this method will * render the presentation directly to the client and null will be returned. * * @return string A string representing the rendered presentation, if * the controller render mode is sfView::RENDER_VAR, otherwise null. */ public function render($templateVars = null) { $template = $this->getDirectory() . '/' . $this->getTemplate(); $actionStackEntry = $this->getContext()->getActionStack()->getLastEntry(); $actionInstance = $actionStackEntry->getActionInstance(); $moduleName = $actionInstance->getModuleName(); $actionName = $actionInstance->getActionName(); $retval = null; $context = $this->getContext(); //exception, if template is missing $this->preRenderCheck(); // get the render mode $mode = $context->getController()->getRenderMode(); // template variables if ($templateVars === null) { $actionStackEntry = $context->getActionStack()->getLastEntry(); $actionInstance = $actionStackEntry->getActionInstance(); $templateVars = $actionInstance->getVarHolder()->getAll(); } // assigns some variables to the template $this->attributeHolder->add($this->getGlobalVars()); $this->attributeHolder->add(array('dir' => $this->getDirectory())); $this->attributeHolder->add($retval !== null ? $vars : $templateVars); try { $content = $this->renderFile($template); $filename = array_key_exists('filename', $templateVars) && $templateVars['filename'] !== null ? $templateVars['filename'] : $actionStackEntry->getModuleName() . '_' . $actionStackEntry->getActionName(); $orientation = array_key_exists('orientation', $templateVars) && $templateVars['orientation'] !== null ? $templateVars['orientation'] : 'portrait'; $papersize = array_key_exists('papersize', $templateVars) && $templateVars['papersize'] !== null ? $templateVars['papersize'] : 'a4'; // $q = new sfDomPDFPlugin($content); // $q->execute(); // $q->getPDF()->stream($filename.'.pdf'); $q = new sfDomPDFPlugin($content); $q->setPaper($papersize, $orientation); if ($pdf = $q->execute()) { $response = $this->getContext()->getResponse(); $response->setHttpHeader('Pragma', ''); $response->setHttpHeader('Cache-Control', ''); $response->setHttpHeader('Content-Type', 'application/pdf'); $response->setHttpHeader('Content-Disposition', 'attachment; filename="' . $filename . '.pdf"'); $response->setContent($pdf); } } catch (Exception $e) { $context->getResponse()->addHttpMeta('Content-Disposition', ''); //fixme return parent::render(); } }