Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. private function getJaspertResponse()
  2. {
  3. // Par de variables, lo ideal es parametrizarlas (que te las manden por el request)
  4. // Un usuario registrado en el servidor de jasper
  5. $user = 'usuarioJasper';
  6. $pass = 'contraseñaJasper';
  7.  
  8. // Si se quiere descargar o no y el nombre que tendría el archivo
  9. $descarga = 1; // 1 para descargar, 0 para abrir en navegador
  10. $nombreInforme = "ReporteReal123";
  11.  
  12. // La url de nuestro reporte, apuntando al servidor jasper
  13. $url = "https://es.stackoverflow.com/soyUnReporteLoJuro.pdf&soyUnParametro=0";
  14.  
  15. // Configuramos el cURL con la ruta, usuario y contraseña
  16. $ch = curl_init();
  17. curl_setopt($ch, CURLOPT_URL, $url);
  18. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  19. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  20. 'Authorization: Basic ' . base64_encode("$user:$pass"),
  21. ));
  22.  
  23. // Ejecutamos
  24. $report_response = curl_exec($ch);
  25.  
  26. // Si no devuelve 200 es que algo falló
  27. if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200){
  28. curl_close($ch);
  29. throw new Exception('Error al obtener informe');
  30. }
  31.  
  32. // Creamos un Response con la respuesta
  33. $response = new Response($report_response);
  34. // Y le cambiamos el tipo de contenido ya que es pdf
  35. $response->headers->set('Content-Type', curl_getinfo($ch, CURLINFO_CONTENT_TYPE));
  36.  
  37. // Esto es si se quiere descargar en vez de abrir en navegador
  38. if ($download){
  39. $response->headers->set('Content-Disposition', "attachment; filename=$nombreInforme");
  40. }
  41. // Cerramos el cURL
  42. curl_close($ch);
  43.  
  44. // Y c'est fini
  45. return $response;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement