pdf_constancias_gema_split.php 8.6 KB

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