/** * generate the final result * * @return array: * 'is_error': set if there is a fatal error * 'log': array with keys: 'type', 'level', 'timestamp', 'message' * 'download_url: URL to download the result * 'download_name: suggested file name for the download */ public function wrapUp($snapshot_id, $is_test, $is_bulk) { $snapshot = CRM_Donrec_Logic_Snapshot::get($snapshot_id); $reply = array(); // open file $preferredFileName = ts('donation_receipts'); $preferredFileSuffix = ts('.csv', array('domain' => 'de.systopia.donrec')); $temp_file = CRM_Donrec_Logic_File::makeFileName($preferredFileName, $preferredFileSuffix); $handle = fopen($temp_file, 'w'); // get headers $headers = CRM_Donrec_Logic_ReceiptTokens::getFullTokenList(); $headers = $this->flattenTokenData($headers); $headers = array_keys($headers); $header_written = false; // write them all into the file $ids = $snapshot->getIds(); foreach ($ids as $id) { $proc_info = $snapshot->getProcessInformation($id); $csv_data = $proc_info['CSV']['csv_data']; if (!empty($csv_data)) { if (!$header_written) { // extend header by extra fields $headers = array_merge($headers, array_keys($csv_data)); $headers = array_unique($headers); // write header fputcsv($handle, $headers, ';', '"'); $header_written = true; } // create and write a line $line = array(); foreach ($headers as $field) { if (isset($csv_data[$field])) { $line[$field] = $csv_data[$field]; } else { $line[$field] = ''; } } fputcsv($handle, $line, ';', '"'); } } // get process info iterator fclose($handle); // create the file $file = CRM_Donrec_Logic_File::createTemporaryFile($temp_file, $preferredFileName . $preferredFileSuffix); CRM_Core_Error::debug_log_message("de.systopia.donrec: resulting CSV file URL is '{$file}'."); if (!empty($file)) { $reply['download_name'] = $preferredFileName; $reply['download_url'] = $file; } CRM_Donrec_Logic_Exporter::addLogEntry($reply, 'CSV process ended.', CRM_Donrec_Logic_Exporter::LOG_TYPE_INFO); return $reply; }
/** * generate the final result * * @return array: * 'is_error': set if there is a fatal error * 'log': array with keys: 'type', 'level', 'timestamp', 'message' * 'download_url: URL to download the result * 'download_name: suggested file name for the download */ public function wrapUp($snapshot_id, $is_test, $is_bulk) { $reply = array(); // create the zip file $config = CRM_Core_Config::singleton(); $preferredFileName = ts("donation_receipts.zip", array('domain' => 'de.systopia.donrec')); $archiveFileName = CRM_Donrec_Logic_File::makeFileName(ts("donation_receipts", array('domain' => 'de.systopia.donrec')), ".zip"); $zip = new ZipArchive(); $snapshot = CRM_Donrec_Logic_Snapshot::get($snapshot_id); $ids = $snapshot->getIds(); $toRemove = array(); if ($zip->open($archiveFileName, ZIPARCHIVE::CREATE) === TRUE) { foreach ($ids as $id) { $proc_info = $snapshot->getProcessInformation($id); if (!empty($proc_info)) { $filename = isset($proc_info['PDF']['pdf_file']) ? $proc_info['PDF']['pdf_file'] : FALSE; if ($filename) { $toRemove[$id] = $filename; $opResult = $zip->addFile($filename, basename($filename)); CRM_Donrec_Logic_Exporter::addLogEntry($reply, "adding <span title='{$filename}'>created PDF file</span> to <span title='{$archiveFileName}'>ZIP archive</span> ({$opResult})", CRM_Donrec_Logic_Exporter::LOG_TYPE_DEBUG); } } } if (!$zip->close()) { CRM_Donrec_Logic_Exporter::addLogEntry($reply, 'zip->close() returned false!', CRM_Donrec_Logic_Exporter::LOG_TYPE_ERROR); } } else { CRM_Donrec_Logic_Exporter::addLogEntry($reply, sprintf('PDF processing failed: Could not open zip file '), CRM_Donrec_Logic_Exporter::LOG_TYPE_FATAL); return $reply; } $file = CRM_Donrec_Logic_File::createTemporaryFile($archiveFileName, $preferredFileName); CRM_Core_Error::debug_log_message("de.systopia.donrec: resulting ZIP file URL is '{$file}'."); if (!empty($file)) { $reply['download_name'] = $preferredFileName; $reply['download_url'] = $file; } // remove loose pdf files or store them CRM_Donrec_Logic_Exporter::addLogEntry($reply, 'Removing temporary PDF files.', CRM_Donrec_Logic_Exporter::LOG_TYPE_DEBUG); foreach ($toRemove as $file) { unlink($file); } CRM_Donrec_Logic_Exporter::addLogEntry($reply, 'PDF generation process ended.', CRM_Donrec_Logic_Exporter::LOG_TYPE_INFO); return $reply; }
/** * generate the final result * * @return array: * 'is_error': set if there is a fatal error * 'log': array with keys: 'type', 'level', 'timestamp', 'message' * 'download_url: URL to download the result * 'download_name: suggested file name for the download */ public function wrapUp($snapshot_id, $is_test, $is_bulk) { $reply = array(); // create the zip file $config = CRM_Core_Config::singleton(); $preferredFileName = ts("donation_receipts", array('domain' => 'de.systopia.donrec')); $preferredSuffix = ts('.zip', array('domain' => 'de.systopia.donrec')); $archiveFileName = CRM_Donrec_Logic_File::makeFileName($preferredFileName, $preferredSuffix); $fileURL = $archiveFileName; $outerArchive = new ZipArchive(); $snapshot = CRM_Donrec_Logic_Snapshot::get($snapshot_id); $ids = $snapshot->getIds(); $toRemove = array(); // Sort array by page count $pageCountArr = array(); foreach ($ids as $id) { $proc_info = $snapshot->getProcessInformation($id); if (!empty($proc_info)) { $pageCount = isset($proc_info['PDF']['pdf_pagecount']) ? $proc_info['PDF']['pdf_pagecount'] : FALSE; $filename = isset($proc_info['PDF']['pdf_file']) ? $proc_info['PDF']['pdf_file'] : FALSE; if ($pageCount) { $pageCountArr[$pageCount][] = array($pageCount, $id, $filename); } } } // add files to sub-archives // open main archive and add sub-archives if ($outerArchive->open($fileURL, ZIPARCHIVE::CREATE) === TRUE) { foreach ($pageCountArr as $entry) { foreach ($entry as $item) { if ($item[0] && $item[2]) { // if page count and file name exists $folder = sprintf(ts('%d-page', array('domain' => 'de.systopia.donrec')), $item[0]) . DIRECTORY_SEPARATOR; $opResult = $outerArchive->addFile($item[2], $folder . basename($item[2])); CRM_Donrec_Logic_Exporter::addLogEntry($reply, "adding <span title='{$item[2]}'>created {$item[0]}-page PDF file</span> ({$opResult})", CRM_Donrec_Logic_Exporter::LOG_TYPE_DEBUG); } } } if (!$outerArchive->close()) { CRM_Donrec_Logic_Exporter::addLogEntry($reply, 'zip->close() returned false!', CRM_Donrec_Logic_Exporter::LOG_TYPE_ERROR); } } else { CRM_Donrec_Logic_Exporter::addLogEntry($reply, sprintf('PDF processing failed: Could not open zip file '), CRM_Donrec_Logic_Exporter::FATAL); return $reply; } $file = CRM_Donrec_Logic_File::createTemporaryFile($fileURL, $preferredFileName . $preferredSuffix); CRM_Core_Error::debug_log_message("de.systopia.donrec: resulting ZIP file URL is '{$file}'."); if (!empty($file)) { $reply['download_name'] = $preferredFileName . $preferredSuffix; $reply['download_url'] = $file; } // remove loose pdf files or store them CRM_Donrec_Logic_Exporter::addLogEntry($reply, 'Removing temporary files.', CRM_Donrec_Logic_Exporter::LOG_TYPE_DEBUG); foreach ($toRemove as $file) { unlink($file); } CRM_Donrec_Logic_Exporter::addLogEntry($reply, 'PDF generation process ended.', CRM_Donrec_Logic_Exporter::LOG_TYPE_INFO); return $reply; }
/** * Creates a PDF file from the specified values * * @param array associative array of values that will be * assigned to the template * @param array of configuration parameters * @return filename or False */ public function generatePDF($values, &$parameters) { $smarty = CRM_Core_Smarty::singleton(); $config = CRM_Core_Config::singleton(); // assign all values foreach ($values as $token => $value) { $smarty->assign($token, $value); } // callback for custom variables CRM_Utils_DonrecCustomisationHooks::pdf_unique_token($smarty, $values); // get template $html = $this->_template->msg_html; // --- watermark injection --- // identify pdf engine $pdf_engine = $config->wkhtmltopdfPath; if (!empty($pdf_engine)) { $wk_is_enabled = TRUE; $watermark_css = '<style> {literal} .watermark { position: fixed; z-index: 999; color: rgba(128, 128, 128, 0.20); -ms-transform: rotate(-45deg); /* IE 9 */ -webkit-transform: rotate(-45deg); /* Chrome, Safari, Opera */ transform: rotate(-45deg); font-size: 100pt!important; } .watermark-center { left: 10px; top: 400px; } {/literal} </style> '; } else { $wk_is_enabled = FALSE; $watermark_css = '<style> {literal} .watermark { position: fixed; z-index: 999; opacity: 0.10; -ms-transform: rotate(-45deg); /* IE 9 */ -webkit-transform: rotate(-45deg); /* Chrome, Safari, Opera */ transform: rotate(-45deg); font-size: 100pt!important; } .watermark-center { left: 30px; top: 650px; } {/literal} </style> '; } $smarty->assign('wk_enabled', $wk_is_enabled); // prepare watermark $watermark_site = '<div class="watermark watermark-center">{if $watermark}{$watermark}{/if}</div>'; // find </style> element $matches = array(); preg_match('/<\\/style>/', $html, $matches, PREG_OFFSET_CAPTURE); if (count($matches) == 1) { $head_offset = $matches[0][1]; $html = substr_replace($html, $watermark_css, $head_offset + strlen($matches[0][0]), 0); } else { if (count($matches) < 1) { CRM_Core_Error::debug_log_message('de.systopia.donrec: watermark css could not be created (</style> not found). falling back to <body>.'); $matches = array(); preg_match('/<body>/', $html, $matches, PREG_OFFSET_CAPTURE); if (count($matches) == 1) { $head_offset = $matches[0][1]; $html = substr_replace($html, $watermark_css, $head_offset, 0); } else { CRM_Core_Error::debug_log_message('de.systopia.donrec: watermark could not be created. pdf rendering cancelled.'); return FALSE; } } } // find <body> element $matches = array(); preg_match('/<body[^>]*>/', $html, $matches, PREG_OFFSET_CAPTURE); if (count($matches) == 1) { $body_offset = $matches[0][1]; $html = substr_replace($html, $watermark_site, $body_offset + strlen($matches[0][0]), 0); } else { if (count($matches) < 1) { CRM_Core_Error::debug_log_message('de.systopia.donrec: watermark could not be created for site one (<body> not found). pdf rendering cancelled.'); return FALSE; } } // --- watermark injection end --- // compile template $html = $smarty->fetch("string:{$html}"); // reset template variables $smarty->clearTemplateVars(); // set up file names $filename_export = CRM_Donrec_Logic_File::makeFileName(ts("donationreceipt-", array('domain' => 'de.systopia.donrec')) . "{$values['contributor']['id']}-" . date('YmdHis'), ".pdf"); // render PDF receipt $result = file_put_contents($filename_export, CRM_Utils_PDF_Utils::html2pdf($html, null, true, $this->_template->pdf_format_id)); if ($result) { return $filename_export; } else { $parameters['error'] = "Could not write file {$filename_export}"; return FALSE; } }