/** * Create a pdf from applicant * @param integer $applicantId * @param string $layout the format for the pdf file */ public function actionPdf($applicantId, $layout) { $applicant = $this->getApplicantById($applicantId); $pdf = new \Jazzee\ApplicantPDF($this->_config->getPdflibLicenseKey(), $layout == 'landscape' ? \Jazzee\ApplicantPDF::USLETTER_LANDSCAPE : \Jazzee\ApplicantPDF::USLETTER_PORTRAIT, $this); $this->setVar('filename', $applicant->getFullName() . '.pdf'); $this->setVar('blob', $pdf->pdf($applicant)); }
/** * PDF file type * Create a single large PDF of all applicants * We do this with temporary files because the add from string method leaks * These both leak * $zip->addFromString($directoryName . '/' . $fullName . '.pdf', $pdfResult); * @param array \Jazzee\Entity\Applicant $applicants * @param \Jazzee\Interfaces\Display $display */ protected function makePdfArchive(array $applicants, \Jazzee\Interfaces\Display $display) { // ensure garbage collection is on, we need it gc_enable(); $directoryName = $this->_application->getProgram()->getShortName() . '-' . $this->_application->getCycle()->getName() . date('-mdy'); $zipFile = $this->_config->getVarPath() . '/tmp/' . uniqid() . '.zip'; $zip = new ZipArchive(); $zip->open($zipFile, ZipArchive::CREATE); $zip->addEmptyDir($directoryName); $tempFileArray = array(); $tmppath = $this->_config->getVarPath() . '/tmp'; foreach (array_chunk($applicants, 20) as $limitedIds) { $applicantsDisplayArray = $this->_em->getRepository('Jazzee\\Entity\\Applicant')->findDisplayArrayByApplication($this->_application, $display, $limitedIds); foreach ($applicantsDisplayArray as $applicantArray) { $pdf = new \Jazzee\ApplicantPDF($this->_config->getPdflibLicenseKey(), \Jazzee\ApplicantPDF::USLETTER_LANDSCAPE, $this); $temp_file_name = tempnam($tmppath, 'JazzeeTempDL'); $tempFileArray[] = $temp_file_name; file_put_contents($temp_file_name, $pdf->pdfFromApplicantArray($this->_application, $applicantArray)); $zip->addFile($temp_file_name, $directoryName . '/' . $applicantArray['fullName'] . '.pdf'); unset($pdf); } } $zip->close(); header('Content-Type: ' . 'application/zip'); header('Content-Disposition: attachment; filename=' . $directoryName . '.zip'); header('Content-Transfer-Encoding: binary'); // we need this b/c otherwise the readfile call (to write the file // to the client) may attempt to slurp the whole archive and run out of memory. if (ob_get_level()) { ob_end_clean(); } header('Content-Length: ' . filesize($zipFile)); readfile($zipFile); unlink($zipFile); foreach ($tempFileArray as $path) { unlink($path); } exit(0); }
/** * List all applicants */ public function actionDownloadPdfArchive() { //use a full applicant display where display is needed $display = new \Jazzee\Display\FullApplication($this->_application); $applicants = empty($this->post['applicantIds']) ? array() : explode(',', $this->post['applicantIds']); // ensure garbage collection is on, we need it gc_enable(); $directoryName = $this->_application->getProgram()->getShortName() . '-' . $this->_application->getCycle()->getName() . date('-mdy'); $zipFile = $this->_config->getVarPath() . '/tmp/' . uniqid() . '.zip'; $zip = new ZipArchive(); $zip->open($zipFile, ZipArchive::CREATE); $zip->addEmptyDir($directoryName); $tmppath = $this->_config->getVarPath() . '/tmp'; if ($this->post['pdftemplate'] == 'portrait' or $this->post['pdftemplate'] == 'landscape') { foreach (array_chunk($applicants, 20) as $limitedIds) { $applicantsDisplayArray = $this->_em->getRepository('Jazzee\\Entity\\Applicant')->findDisplayArrayByApplication($this->_application, $display, $limitedIds); foreach ($applicantsDisplayArray as $applicantArray) { switch ($this->post['pdftemplate']) { case 'portrait': $pdf = new \Jazzee\ApplicantPDF($this->_config->getPdflibLicenseKey(), \Jazzee\ApplicantPDF::USLETTER_PORTRAIT, $this); break; case 'landscape': $pdf = new \Jazzee\ApplicantPDF($this->_config->getPdflibLicenseKey(), \Jazzee\ApplicantPDF::USLETTER_LANDSCAPE, $this); break; } $temp_file_name = tempnam($tmppath, 'JazzeeTempDL'); file_put_contents($temp_file_name, $pdf->pdfFromApplicantArray($this->_application, $applicantArray)); $externalIDSuffix = $applicantArray['externalId'] != null ? ' - ' . $applicantArray['externalId'] : ""; $zip->addFile($temp_file_name, $directoryName . '/' . $applicantArray['lastName'] . '_' . $applicantArray['firstName'] . $externalIDSuffix . '.pdf'); unset($pdf); } } } else { if (!($template = $this->_application->getTemplateById($this->post['pdftemplate']))) { throw new \Jazzee\Exception("Invalid template ID: " . $this->post['pdftemplate'] . ' for ' . $this->_application->getProgram()->getName()); } foreach (array_chunk($applicants, 20) as $limitedIds) { $applicantsPDFTemplateArray = $this->_em->getRepository('Jazzee\\Entity\\Applicant')->findPDFTemplateArrayByApplication($this->_application, $display, $limitedIds); foreach ($applicantsPDFTemplateArray as $applicantArray) { $pdf = new \Jazzee\TemplatePDF($this->_config->getPdflibLicenseKey(), $template, $this); $temp_file_name = tempnam($tmppath, 'JazzeeTempDL'); file_put_contents($temp_file_name, $pdf->pdfFromApplicantArray($this->_application, $applicantArray)); $externalIDSuffix = $applicantArray['externalId'] != null ? ' - ' . $applicantArray['externalId'] : ""; $zip->addFile($temp_file_name, $directoryName . '/' . $applicantArray['lastName'] . '_' . $applicantArray['firstName'] . $externalIDSuffix . '.pdf'); unset($pdf); } } } $zip->close(); $this->setVar('outputType', 'file'); $this->setVar('type', 'application/zip'); $this->setVar('filename', $directoryName . '.zip'); $this->setVar('filePath', $zipFile); $this->loadView('applicants_grid/download'); }