pdf_constancias_split.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. //ini_set('max_execution_time', 150);
  3. ini_set('display_errors', 1);
  4. ini_set('display_startup_errors', 1);
  5. error_reporting(E_ALL);
  6. setlocale(LC_TIME, 'es_MX.UTF-8');
  7. require_once("../../include/nocache.php");
  8. require_once("../../include/bd_pdo.php");
  9. require_once("../../include/util.php");
  10. require_once("../../include/constantes.php");
  11. require_once("../../classes/ValidaSesion.php");
  12. include_once('../../include/xTemplate/xtemplate.class.php'); // including mpdf.php
  13. include_once('../../include/mpdf/autoload.php'); // including mpdf.php
  14. include_once('../include/util.php');
  15. $pag = "../constancias.php";
  16. //--- Objeto para validar usuario. El id de usuario lo lee desde sesión
  17. $objSesion = new ValidaSesion($pdo, 303, CONSTANCIA);
  18. if(!$objSesion->tieneAcceso()){
  19. header("Location: ".$pag."?error=1");
  20. exit();
  21. }
  22. unset($objValida);
  23. if(!isset($_POST["tipo"]) || !isset($_POST["evento"]) || !isset($_POST["participantes"]) || !isset($_POST["desc"]) || !isset($_POST["fecha"]) || !isset($_POST["num_firmas"], $_POST["firma"], $_POST["puesto"], $_POST["motivo"])){
  24. header("Location: ".$pag."?error=0");
  25. exit();
  26. }
  27. $sist = CONSTANCIA;
  28. $tipo_id = filter_input(INPUT_POST, "tipo", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  29. $evento = trim(filter_input(INPUT_POST, "evento", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW)));
  30. $motivo = trim(filter_input(INPUT_POST, "motivo", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW)));
  31. $participantes = $_POST["participantes"];//array
  32. $desc = $_POST["desc"]; //filter_input(INPUT_POST, "desc", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW));
  33. $fecha = trim(filter_input(INPUT_POST, "fecha", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW)));//limpia texto
  34. $numFirmas = filter_input(INPUT_POST, "num_firmas", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  35. $firma = $_POST["firma"];//array
  36. $lleva_firma = $_POST["lleva_firma"];//array
  37. $puesto = $_POST["puesto"];
  38. $hay_marca = false;
  39. //hay archivo?
  40. if(!empty($_FILES['archivo']['name'])){
  41. $archivo = $_FILES['archivo']['name'];
  42. // Check file size
  43. if ($_FILES["archivo"]["size"] > 500 * 1024) {//en bytes
  44. header("Location:".$pag."error=4");
  45. exit();
  46. }
  47. $dot_arr = explode('.', $archivo);
  48. $ext = end($dot_arr);
  49. unset($dot_arr);
  50. $target_logo = "logo_".$sist."_".date("dmyhis").".".$ext;
  51. }
  52. $firmasArr = array();
  53. try {
  54. $pdo->beginTransaction();
  55. for($i=0; $i < $numFirmas; $i++){
  56. $stmt = $pdo->prepare('Select * from fs_constanciafirma(:id, NULL )');//firmas
  57. $stmt->bindParam(":id", $firma[$i]);
  58. if(!$stmt->execute()){
  59. $pdo->rollBack();
  60. header("Location:".$pag."?error=2");
  61. //print_r($stmt->errorInfo());
  62. exit();
  63. }
  64. $firma_rs = $stmt->fetch();
  65. $stmt->closeCursor();
  66. $firmasArr[] = array("id"=>$firma_rs["ConstanciaFirma_id"] ,"grado"=>$firma_rs["ConstanciaFirma_grado"] ,
  67. "nombre"=> $firma_rs["ConstanciaFirma_nombre"] /*mb_convert_case(mb_strtolower($firma_rs["ConstanciaFirma_nombre"]), MB_CASE_TITLE, "UTF-8")*/,
  68. "puesto"=>$puesto[$i], "firma"=> $firma_rs["ConstanciaFirma_imagen"], "lleva_firma"=>$lleva_firma[$i]/*($lleva_firma[$i]===1)?true:false*/
  69. );
  70. $hay_marca = $hay_marca || (isset($firmasArr[$i]["lleva_firma"]) && $firmasArr[$i]["lleva_firma"]==1);
  71. }
  72. $stmt = $pdo->prepare('Select * from fs_constancialogtipo(:id)');
  73. $stmt->bindParam(":id", $tipo_id);
  74. if(!$stmt->execute()){
  75. header("Location:".$pag."?error=2");
  76. //print_r($stmt->errorInfo());
  77. exit();
  78. }
  79. $tipo_rs = $stmt->fetch();
  80. $stmt->closeCursor();
  81. $tipo = $tipo_rs["ConstanciaLogTipo_desc"];
  82. $defaultConfig = (new Mpdf\Config\ConfigVariables())->getDefaults();
  83. $fontDirs = $defaultConfig['fontDir'];
  84. $defaultFontConfig = (new Mpdf\Config\FontVariables())->getDefaults();
  85. $fontData = $defaultFontConfig['fontdata'] +
  86. [ 'tipo' => [
  87. 'R' => 'IndivisaDisplaySans-Thin.ttf',
  88. ]] +
  89. [ 'texto-a' => [
  90. 'R' => 'IndivisaTextSans-Light.ttf',
  91. ]] +
  92. ['indivisa-text' => [
  93. 'R' => 'IndivisaTextSans-Regular.ttf',
  94. 'B' => 'IndivisaTextSans-Bold.ttf',
  95. 'BI' => 'IndivisaTextSans-BoldItalic.ttf',
  96. ]];
  97. $stylesheet = "<style>";
  98. //$stylesheet .= file_get_contents('../../css/indivisa.css'); // external css
  99. $stylesheet .= file_get_contents('../css/constancia.css'); // external css
  100. $stylesheet .= "</style>";
  101. //----------- Archivos y zip ---
  102. $tmp_folder = "./zip/pdf_".fechaGuion($fecha);
  103. if (!file_exists($tmp_folder)) {
  104. mkdir($tmp_folder, 0777, true);
  105. }
  106. $zip = new ZipArchive();
  107. $zip_filename = "zip/".$tipo."s_".fechaGuion($fecha)."_".date("his").".zip";
  108. if ($zip->open($zip_filename, ZipArchive::CREATE)!==TRUE) {
  109. $pdo->rollBack();
  110. exit("cannot open <$zip_filename>\n");
  111. }
  112. if($hay_marca==1){
  113. //Marca de agua
  114. if (file_exists($_SERVER['DOCUMENT_ROOT']. '/const/export/zip/marca.png')) {
  115. unlink($_SERVER['DOCUMENT_ROOT']. '/const/export/zip/marca.png');
  116. }
  117. $marca = mb_strtoupper($evento,'UTF-8') . ' ' . fechaGuion($fecha) . ' | '; //No acepta caracteres especiales
  118. generaMarcaDeAgua($marca, "zip/");
  119. }
  120. $header = '<div><img id="logo" src="../img/lasalle_pdf.jpg">';
  121. if(isset($target_logo)){
  122. $target_dir = "../img/logos/";
  123. if(file_exists($target_dir.$target_logo)){
  124. unlink($target_dir.$target_logo);
  125. }
  126. if (!move_uploaded_file($_FILES["archivo"]["tmp_name"], $target_dir.$target_logo)) {
  127. $pdo->rollBack();
  128. header("Location:".$pag."?error=5");
  129. exit();
  130. }
  131. $header .= '<img id="logo_secondary" src="'.$target_dir.$target_logo.'">';
  132. }
  133. $header .= '</div>';
  134. //Footer
  135. $footer = "";
  136. $num = 1;
  137. foreach($participantes as $part){
  138. //$nombre = mb_convert_case(mb_strtolower($part), MB_CASE_TITLE, "UTF-8");
  139. $nombre = $part;
  140. $mpdf = new \Mpdf\Mpdf([
  141. 'mode'=>'utf-8', 'format'=>'Letter',
  142. 'margin_header'=> 21, 'margin_top'=>52, 'margin_left'=> 35, 'margin_right'=> 30, 'margin_footer'=>10,
  143. 'orientation' => 'P',
  144. 'fontDir' => array_merge($fontDirs, [
  145. __DIR__ . '/../../fonts/indivisaFont/ttf',
  146. ]),
  147. 'fontdata' => $fontData,
  148. 'default_font' => 'indivisa-text'
  149. ]);
  150. $xtpl = new XTemplate('../tpl/constancia.tpl.html');
  151. $xtpl->assign("TIPO", $tipo);
  152. $xtpl->assign("NOMBRE", $nombre);
  153. $xtpl->assign("TEXTO", $desc);
  154. $xtpl->parse("main");
  155. if(isset($target_logo))
  156. $footer = generaFooter($pdo, $sist, $pag, $fecha, $numFirmas, $firmasArr, $nombre, $motivo, $tipo_id, $evento, $desc, date('y'), $target_logo);
  157. else
  158. $footer = generaFooter($pdo, $sist, $pag, $fecha, $numFirmas, $firmasArr, $nombre, $motivo, $tipo_id, $evento, $desc, date('y'), NULL);
  159. /*echo $stylesheet;
  160. echo $header;
  161. echo $xtpl->text("main");
  162. echo $footer;
  163. exit();*/
  164. $mpdf->SHYlang = 'es';
  165. $mpdf-> SetTitle($tipo . '-' . $nombre);
  166. $mpdf-> SetAuthor('Facultad de Ingeniería. © Universidad La Salle A.C. '.date("Y").' Todos los derechos Reservados.');
  167. $mpdf->SetHTMLHeader($header);
  168. $mpdf->WriteHTML($stylesheet);
  169. $mpdf->WriteHTML($xtpl->text("main"));
  170. $mpdf->SetHTMLFooter($footer);
  171. $mpdf->SetProtection(array('print', 'print-highres'), '', md5(time()));
  172. //$mpdf->Output("nombramientos.pdf", 'I');exit();//
  173. $mpdf->Output($tmp_folder."/".$tipo."_".$num.".pdf", 'F');
  174. $zip->addFile(
  175. $tmp_folder."/".$tipo."_".$num.".pdf",
  176. $tipo."s_".fechaGuion($fecha)."/".$tipo."_".$nombre.".pdf"
  177. );
  178. $num++;
  179. }//for participante
  180. if($zip->numFiles<=0){
  181. $pdo->rollBack();
  182. echo "Error al guardar en el zip";
  183. $zip->close();
  184. unlink($zip_filename);
  185. exit();
  186. }
  187. $zip->close();
  188. removeFolder($tmp_folder);
  189. header("Content-Type: application/zip");
  190. header("Content-Disposition: attachment; filename=".basename($zip_filename));
  191. header("Pragma: no-cache");
  192. header("Expires: 0");
  193. ob_end_clean();
  194. flush();
  195. readfile($zip_filename);
  196. $pdo->commit();
  197. unlink("zip/marca.png");
  198. //unlink($zip_filename);
  199. } catch(PDOException $e) {
  200. $pdo->rollBack();
  201. header("Location:".$pag."?error=2");
  202. //print_r($e);
  203. }
  204. function removeFolder($folderName) {
  205. if (is_dir($folderName))
  206. $folderHandle = opendir($folderName);
  207. if (!$folderHandle)
  208. return false;
  209. while($file = readdir($folderHandle)) {
  210. if ($file != "." && $file != "..") {
  211. if (!is_dir($folderName."/".$file))
  212. unlink($folderName."/".$file);
  213. else
  214. removeFolder($folderName.'/'.$file);
  215. }
  216. }
  217. closedir($folderHandle);
  218. rmdir($folderName);
  219. return true;
  220. }
  221. ?>