Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private function getJaspertResponse()
- {
- // Par de variables, lo ideal es parametrizarlas (que te las manden por el request)
- // Un usuario registrado en el servidor de jasper
- $user = 'usuarioJasper';
- $pass = 'contraseñaJasper';
- // Si se quiere descargar o no y el nombre que tendría el archivo
- $descarga = 1; // 1 para descargar, 0 para abrir en navegador
- $nombreInforme = "ReporteReal123";
- // La url de nuestro reporte, apuntando al servidor jasper
- $url = "https://es.stackoverflow.com/soyUnReporteLoJuro.pdf&soyUnParametro=0";
- // Configuramos el cURL con la ruta, usuario y contraseña
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Authorization: Basic ' . base64_encode("$user:$pass"),
- ));
- // Ejecutamos
- $report_response = curl_exec($ch);
- // Si no devuelve 200 es que algo falló
- if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200){
- curl_close($ch);
- throw new Exception('Error al obtener informe');
- }
- // Creamos un Response con la respuesta
- $response = new Response($report_response);
- // Y le cambiamos el tipo de contenido ya que es pdf
- $response->headers->set('Content-Type', curl_getinfo($ch, CURLINFO_CONTENT_TYPE));
- // Esto es si se quiere descargar en vez de abrir en navegador
- if ($download){
- $response->headers->set('Content-Disposition', "attachment; filename=$nombreInforme");
- }
- // Cerramos el cURL
- curl_close($ch);
- // Y c'est fini
- return $response;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement