/** * create the PDF file as a ProtectedFile for the given side. * * @param CController $controller * @param array $template_data * @param string $side * * @throws Exception * * @return ProtectedFile|null */ protected function createAndSavePdfForSide(CController $controller, array $template_data, $side) { if ($html = $this->getPDFContentForSide($controller, $template_data, $side)) { $html = '<link rel="stylesheet" type="text/css" href="' . $controller->assetPath . '/css/print.css" />' . "\n" . $html; $this->event->lock(); if (!$this->event->hasPDF('therapy_application')) { $wk = new WKHtmlToPDF(); $wk->setDocuments(1); $wk->setDocRef($this->event->docref); $wk->setPatient($this->event->episode->patient); $wk->setBarcode($this->event->barcodeHTML); $wk->generatePDF($this->event->imageDirectory, 'event', 'therapy_application', $html, false, false); } $this->event->unlock(); if (@$_GET['html']) { return Yii::app()->end(); } $pfile = ProtectedFile::createForWriting('ECForm - ' . $side . ' - ' . $template_data['patient']->hos_num . '.pdf'); if (!@copy($this->event->getPDF('therapy_application'), $pfile->getPath())) { throw new Exception('Unable to write to file: ' . $pfile->getPath()); } if (!$pfile->save()) { throw new Exception('Unable to save file: ' . print_r($pfile->errors, true)); } return $pfile; } return; }
public function actionPDFPrint($id) { if (!($event = Event::model()->findByPk($id))) { throw new Exception("Event not found: {$id}"); } $event->lock(); // Ensure exclusivity of PDF to avoid race conditions $this->pdf_print_suffix .= Yii::app()->user->id . '_' . rand(); if (!$event->hasPDF($this->pdf_print_suffix) || @$_GET['html']) { if (!$this->pdf_print_html) { ob_start(); $this->actionPrint($id); $this->pdf_print_html = ob_get_contents(); ob_end_clean(); } $wk = new WKHtmlToPDF(); $wk->setCanvasImagePath($event->imageDirectory); $wk->setDocuments($this->pdf_print_documents); $wk->setDocref($event->docref); $wk->setPatient($event->episode->patient); $wk->setBarcode($event->barcodeHTML); foreach (array('left', 'middle', 'right') as $section) { if (isset(Yii::app()->params['wkhtmltopdf_footer_' . $section . '_' . $this->event_type->class_name])) { $setMethod = 'set' . ucfirst($section); $wk->{$setMethod}(Yii::app()->params['wkhtmltopdf_footer_' . $section . '_' . $this->event_type->class_name]); } } foreach (array('top', 'bottom', 'left', 'right') as $margin) { if (isset(Yii::app()->params['wkhtmltopdf_' . $margin . '_margin_' . $this->event_type->class_name])) { $setMethod = 'setMargin' . ucfirst($margin); $wk->{$setMethod}(Yii::app()->params['wkhtmltopdf_' . $margin . '_margin_' . $this->event_type->class_name]); } } foreach (PDFFooterTag::model()->findAll('event_type_id = ?', array($this->event_type->id)) as $pdf_footer_tag) { if ($api = Yii::app()->moduleAPI->get($this->event_type->class_name)) { $wk->setCustomTag($pdf_footer_tag->tag_name, $api->{$pdf_footer_tag->method}($event->id)); } } $wk->generatePDF($event->imageDirectory, 'event', $this->pdf_print_suffix, $this->pdf_print_html, (bool) @$_GET['html']); } $event->unlock(); if (@$_GET['html']) { return Yii::app()->end(); } $pdf = $event->getPDF($this->pdf_print_suffix); header('Content-Type: application/pdf'); header('Content-Length: ' . filesize($pdf)); readfile($pdf); @unlink($pdf); }
/** * Prints next pending letter type for requested operations * Operation IDs are passed as an array (operations[]) via GET or POST * Invalid operation IDs are ignored. * * @throws CHttpException */ public function actionPrintLetters() { Audit::add('waiting list', @$_REQUEST['all'] == 'true' ? 'print all' : 'print selected', serialize($_POST)); if (isset($_REQUEST['event_id'])) { $operations = Element_OphTrOperationbooking_Operation::model()->findAll('event_id=?', array($_REQUEST['event_id'])); $auto_confirm = true; } else { $operation_ids = isset($_REQUEST['operations']) ? $_REQUEST['operations'] : null; $auto_confirm = isset($_REQUEST['confirm']) && $_REQUEST['confirm'] == 1; if (!is_array($operation_ids)) { throw new CHttpException('400', 'Invalid operation list'); } $operations = Element_OphTrOperationbooking_Operation::model()->findAllByPk($operation_ids); } $this->layout = '//layouts/print'; $cmd = Yii::app()->db->createCommand('SELECT GET_LOCK(?, 1)'); while (!$cmd->queryScalar(array('waitingListPrint'))) { } $directory = Yii::app()->assetManager->basePath . '/waitingList'; Yii::app()->db->createCommand('SELECT RELEASE_LOCK(?)')->execute(array('waitingListPrint')); $html = ''; $docrefs = array(); $barcodes = array(); $patients = array(); $documents = 0; // FIXME: provide a means by which progress can be reported back to the user, possibly via session and parallel polling? foreach ($operations as $operation) { $letter_status = $operation->getDueLetter(); if ($letter_status === null && $operation->getLastLetter() == Element_OphTrOperationbooking_Operation::LETTER_GP) { $letter_status = Element_OphTrOperationbooking_Operation::LETTER_GP; } set_time_limit(3); $html .= $this->printLetter($operation, $auto_confirm); $docrefs[] = "E:{$operation->event->id}/" . strtoupper(base_convert(time() . sprintf('%04d', Yii::app()->user->getId()), 10, 32)) . '/{{PAGE}}'; $barcodes[] = $operation->event->barcodeHTML; $patients[] = $operation->event->episode->patient; ++$documents; if ($letter_status == Element_OphTrOperationbooking_Operation::LETTER_GP) { // Patient letter is another document $docrefs[] = "E:{$operation->event->id}/" . strtoupper(base_convert(time() . sprintf('%04d', Yii::app()->user->getId()), 10, 32)) . '/{{PAGE}}'; $barcodes[] = $operation->event->barcodeHTML; $patients[] = $operation->event->episode->patient; ++$documents; } } set_time_limit(10); $pdf_suffix = 'waitingList_' . Yii::app()->user->id . '_' . rand(); $wk = new WKHtmlToPDF(); $wk->setDocuments($documents); $wk->setDocrefs($docrefs); $wk->setBarcodes($barcodes); $wk->setPatients($patients); $wk->generatePDF($directory, $pdf_suffix, '', $html); $pdf = $directory . "/{$pdf_suffix}.pdf"; header('Content-Type: application/pdf'); header('Content-Length: ' . filesize($pdf)); readfile($pdf); @unlink($pdf); }