package es.uv.saic.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

import jakarta.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import es.uv.saic.domain.Document;
import es.uv.saic.domain.Indicador;
import es.uv.saic.dto.IndicadorEnquestaDTO;
import es.uv.saic.dto.IndicadorEnquestaValorDTOImp;
import es.uv.saic.dto.InstanciaTascaDTO;
import es.uv.saic.dto.OrganDTO;
import es.uv.saic.dto.PdfDTO;
import es.uv.saic.dto.ProcesDTO;
import es.uv.saic.dto.TascaDTO;
import es.uv.saic.dto.TascaInformeTransferDTO;
import es.uv.saic.feign.CoreClient;
import es.uv.saic.service.DocumentService;
import es.uv.saic.service.PlantillaService;
import feign.Headers;
import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.core.io.internal.ByteArrayOutputStream;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.images.FileImageProvider;
import fr.opensagres.xdocreport.document.images.IImageProvider;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;


@RestController
public class DownloadController {

	@Autowired 
	private PlantillaService pls;

	@Autowired
	private DocumentService ds;
	
	@Value("${saic.data.filePath}")
	private String filePath;
	
	@Value("${saic.data.docsPath}")
	private String docsPath;
	
	@Value("${saic.data.templates.fileNotFound}")
	private String fileNotFound;
	
	@Value("${saic.data.templates.filePath}")
	private String templatePath;
	
	@Value("${saic.data.templates.logoPath}")
	private String logoPath;

	@Value("${saic.url.core.domain}")
	private String uri;

	@Autowired
	private CoreClient core;

	/*
	 * Download a file associated with a task instance
	 * @param model
	 * @param idInstanciaTasca The ID of the task instance
	 * @param response HttpServletResponse
	 * @return A FileSystemResource representing the file to download
	 */
	@GetMapping(value="/download/{fileName}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
	public ResponseEntity<byte[]> download(@PathVariable("fileName") BigInteger idInstanciaTasca, HttpServletResponse response) throws FileNotFoundException {		
		
		try {
			InstanciaTascaDTO i = core.findInstanciaTascaById(idInstanciaTasca);
			FileSystemResource file = null;
			if(i.getTasca().getIdTipus() == 22){
				file = new FileSystemResource(i.getEvidencia());
				String extension = "."+FilenameUtils.getExtension(i.getEvidencia());
				String name = "-"+i.getTasca().getNomEvidenciaCas().replace(" ", "_");
				response.setHeader("Content-Disposition", "attachment; filename="+i.getTasca().getCodiEvidencia()+name+extension);
			}
			else{
				file = new FileSystemResource(this.filePath+i.getEvidencia());
				response.setHeader("Content-Disposition", "attachment; filename="+i.getEvidencia());
			}
			
			if (!file.exists()) {
				return ResponseEntity.ok(new FileSystemResource(this.fileNotFound).getInputStream().readAllBytes());
			}

			return ResponseEntity.ok(file.getInputStream().readAllBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}

	// PARA BORRAR
	/*
	 * Download a document by its ID
	 * @param model
	 * @param idDocument The ID of the document to download
	 * @param response HttpServletResponse
	 * @return A FileSystemResource representing the document to download
	 */
	@GetMapping(value="/download/document/{id}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
	@ResponseBody
	public ResponseEntity<byte[]> downloadDocument(@PathVariable("id") Integer idDocument, HttpServletResponse response) throws FileNotFoundException {		
		Document document = ds.findById(idDocument);
		FileSystemResource file = new FileSystemResource(document.getRuta());
		if(!file.exists()) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
		}
		
		response.setHeader("Content-Disposition", "attachment; filename="+file.getFilename());
		try {
			return ResponseEntity.ok(file.getInputStream().readAllBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/*
	 * Download the latest report for a given process and degree
	 * @param model
	 * @param idTitulacio The ID of the degree
	 * @param nomProces The name of the process
	 * @param response HttpServletResponse
	 * @return A FileSystemResource representing the report to download
	 */
	@GetMapping(value="/download/report/{t}/{p}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
	@ResponseBody 
	public ResponseEntity<byte[]> downloadReport(@PathVariable("t") Integer idTitulacio, @PathVariable("p") String nomProces,
			HttpServletResponse response) throws IOException, XDocReportException {
		
		OrganDTO titulacio = core.findOrganById("T", idTitulacio);
		
		ProcesDTO procesDTO = new ProcesDTO(nomProces, titulacio.getLugar(), 
														titulacio.getLugar2(), 
														titulacio.getTambit());
		TascaInformeTransferDTO it = core.getLastByProcName(procesDTO);
		
		if(it != null) {
			if((new File(this.filePath+it.getEvidencia())).exists()) {
				response.setHeader("Content-Disposition", "attachment; filename="+Integer.toString(idTitulacio)+"_"+nomProces+".pdf");
				return ResponseEntity.ok(new FileSystemResource(this.filePath+it.getEvidencia()).getInputStream().readAllBytes());
			}
		}
		
		return ResponseEntity.ok(new FileSystemResource(this.filePath+it.getEvidencia()).getInputStream().readAllBytes());
	}


	/*
	 * Download a populated template for a given task instance
	 * @param model
	 * @param idTascai The ID of the task instance
	 * @param response HttpServletResponse
	 * @return A byte array representing the populated template to download
	 */
	@GetMapping(value="/download/template/{id}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
	@ResponseBody 
	public ResponseEntity<byte[]> downloadTemplate(@PathVariable("id") BigInteger idTascai, HttpServletResponse response) throws IOException, XDocReportException {
		XDocReportRegistry.getRegistry().clear();
		String reportId = "none";
		InstanciaTascaDTO it = core.findInstanciaTascaById(idTascai);
		TascaDTO tasca = it.getTasca();
		 
		/* Check if specific template exists */
		Integer idTitulacio = it.getInstancia().getTitulacio();
		String templatePath = this.templatePath+tasca.getCodiEvidencia().replace(".", "_")+".docx";
		if(it.getInstancia().getTambit().equals("G") | idTitulacio == 1) {
			File f = new File(this.templatePath+"/T1/"+tasca.getCodiEvidencia().replace(".", "_")+".docx");
			if(f.exists() && !f.isDirectory()) { 
				templatePath = this.templatePath+"/T1/"+tasca.getCodiEvidencia().replace(".", "_")+".docx";
			}
		}
		else if(it.getInstancia().getTambit().equals("M") | idTitulacio == 2) {
			File f = new File(this.templatePath+"/T2/"+tasca.getCodiEvidencia().replace(".", "_")+".docx");
			if(f.exists() && !f.isDirectory()) { 
				templatePath = this.templatePath+"/T2/"+tasca.getCodiEvidencia().replace(".", "_")+".docx";
			}
		}
		
		File f = new File(templatePath);
		if(!f.exists()) {
			return ResponseEntity.ok(new FileSystemResource(this.fileNotFound).getInputStream().readAllBytes());
		}
				
		InputStream in = new FileInputStream(f);
		IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, reportId, TemplateEngineKind.Velocity);
		FieldsMetadata metadata = new FieldsMetadata();
		metadata.addFieldAsImage("logo");
		report.setFieldsMetadata(metadata);
		IContext context = report.createContext();
		IImageProvider img;
		if(new File(this.logoPath+"C"+Integer.toString(it.getInstancia().getCentre())+".png").exists()) {
			img = new FileImageProvider(new File(this.logoPath+"C"+Integer.toString(it.getInstancia().getCentre())+".png"));
		}
		else {
			img = new FileImageProvider(new File(this.logoPath+"C0.png"));
		}
		context.put("logo", img);
		context.put("centre", it.getInstancia().getNomval());
		context.put("titulacio", it.getInstancia().getNomcas());
		context.put("curs", Integer.toString(it.getInstancia().getCursAvaluat()-1)+" - "+Integer.toString(it.getInstancia().getCursAvaluat()));
		context.put("curs_anterior", Integer.toString(it.getInstancia().getCursAvaluat()-2)+" - "+Integer.toString(it.getInstancia().getCursAvaluat()-1));
		
		Integer idCentre = it.getInstancia().getCentre();
			
		if(tasca.getIdTipus() == 14) { // Iterable template task
			List<OrganDTO> titulacions = new ArrayList<OrganDTO>();
			Integer ambit = idTitulacio/(int)1000;
			titulacions = core.getTitulacionsByTypeCentre(it.getInstancia().getLugar(), ambit);
			List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
			for(OrganDTO x : titulacions) {
				HashMap<String, String> t = getTemplateDataArray(x.getLugar2(), idCentre, it.getInstancia().getCursAvaluat());
				t.put("titulacio", x.getNomCas());
				data.add(t);
			} 
			context.put("data", data);
			getTemplateData(idTitulacio, idCentre, it.getInstancia().getCursAvaluat(), context);
		}
		else { // NO iterable template task
			getTemplateData(idTitulacio, idCentre, it.getInstancia().getCursAvaluat(), context);
		}	
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();  
		report.process(context, out);

	    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+tasca.getCodiEvidencia()+".docx\"");
		return ResponseEntity.ok(out.toByteArray());
		
	}

	/*
	 * Populate the template data into the context
	 * @param idTitulacio The ID of the degree
	 * @param idCentre The ID of the center
	 * @param curs The academic year
	 * @param context The IContext to populate
	 */
	private void getTemplateData(Integer idTitulacio, Integer idCentre, Integer curs, IContext context) {
		
		/*  Indicadores del data warehouse */
		try {
			List<Indicador> indicadores;
			indicadores = core.getFromTitulacion(idTitulacio, curs);
			for(Indicador i : indicadores) { 
				context.put(i.getIndicador(), i.getValor());
			}
		} 
		catch(Exception e) { }
		
		/*  Indicadores de encuestas  */
		List<IndicadorEnquestaValorDTOImp> enquestesT;
		IndicadorEnquestaDTO iEnquestaDTO = new IndicadorEnquestaDTO(idTitulacio, idCentre, curs);
		enquestesT = core.getAllInds2(iEnquestaDTO).stream()
							.map(ind -> (IndicadorEnquestaValorDTOImp) ind)
							.toList();
				
		for(IndicadorEnquestaValorDTOImp i : enquestesT) {
			String indicador = i.getAmbit().equals("t") ? (i.getEnquesta()+"_"+i.getIndicador()) : (i.getEnquesta()+"_"+i.getIndicador()+"_"+i.getAmbit());
			indicador = i.getTipus().equals("avg") ? indicador : (indicador += "_"+i.getTipus());		
			indicador = i.getCursd() == null ? indicador : (indicador += "_"+i.getCursd());
			context.put(indicador, formatValue(i.getValor()));
		}
	}

	/*
	 * Format a value for template insertion
	 * @param v The value to format
	 * @return The formatted value
	 */
	private String formatValue(String v) {	
		if(v == null) return ""; 
		if(v.equals("NP")) return "NP";
		if(v.isEmpty() | v.isBlank()) {
			return "";
		}
		
		try{
	        return String.format("%.2f", Float.parseFloat(v)).replace(",", ".");
	    } 
		catch(NumberFormatException e){ }
		
		try{
	        return Integer.toString(Integer.parseInt(v));
	    } 
		catch(NumberFormatException e){ }
		
		return v;
	}
	
	/*
	 * Get template data as a HashMap
	 * @param idTitulacio The ID of the degree
	 * @param idCentre The ID of the center
	 * @param curs The academic year
	 * @return A HashMap containing the template data
	 */
	private HashMap<String, String> getTemplateDataArray(Integer idTitulacio, Integer idCentre, Integer curs) {
		
		HashMap<String, String> info = new HashMap<String, String>();
		
		/*  Indicadores del data warehouse */
		try {
			List<Indicador> indicadores;
			indicadores = core.getFromTitulacion(idTitulacio, curs);
			for(Indicador i : indicadores) { 
				info.put(i.getIndicador(), i.getValor());
			}
		} 
		catch(Exception e) { }
		
		/*  Indicadores de encuestas  */
		List<IndicadorEnquestaValorDTOImp> enquestesT;	
		IndicadorEnquestaDTO iEnquestaDTO = new IndicadorEnquestaDTO(idTitulacio, idCentre, curs);
		enquestesT = core.getAllInds2(iEnquestaDTO).stream()
										.map(ind -> (IndicadorEnquestaValorDTOImp) ind)
										.toList();
				
		for(IndicadorEnquestaValorDTOImp i : enquestesT) {
			String indicador = i.getAmbit().equals("t") ? (i.getEnquesta()+"_"+i.getIndicador()) : (i.getEnquesta()+"_"+i.getIndicador()+"_"+i.getAmbit());
			indicador = i.getTipus().equals("avg") ? indicador : (indicador += "_"+i.getTipus());	
			indicador = i.getCursd() == null ? indicador : (indicador += "_"+i.getCursd());
			info.put(indicador, formatValue(i.getValor()));
		}	
		
		return info;
	}

	/*
	 * Test endpoint to generate a populated template for a given task (id of task)
	 * @param model
	 * @param idTitulacio The ID of the degree
	 * @param idCentre The ID of the center
	 * @param idTascap The ID of the task
	 * @param idProces The ID of the process
	 * @param response HttpServletResponse
	 * @return A byte array representing the populated template
	 */
	@GetMapping(value="/test/template/{titulacio}/{centre}/{idProces}/{idTascap}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
	@ResponseBody
	public byte[] testTemplate(@PathVariable("titulacio") Integer idTitulacio, @PathVariable("centre") Integer idCentre,
			@PathVariable("idTascap") Integer idTascap, @PathVariable("idProces") Integer idProces, HttpServletResponse response) 
					throws IOException, XDocReportException {
		XDocReportRegistry.getRegistry().clear();
		String reportId = "none";
		
		TascaDTO tasca = core.getByProcesTascap(idProces, idTascap);
		ProcesDTO proces = core.findProcesdById(idProces);
		OrganDTO titulacio = core.findOrganById("T", idTitulacio);
		OrganDTO centre = core.findOrganById("C", idCentre);
		Integer ambit = idTitulacio/(int)1000;
		
		/* Check if specific template exists */
		String templatePath = this.templatePath+tasca.getCodiEvidencia().replace(".", "_")+".docx";
		if(titulacio.getTambit().equals("G") | idTitulacio == 1) {
			File f = new File(this.templatePath+"/T1/"+tasca.getCodiEvidencia().replace(".", "_")+".docx");
			if(f.exists() && !f.isDirectory()) { 
				templatePath = this.templatePath+"/T1/"+tasca.getCodiEvidencia().replace(".", "_")+".docx";
			}
		}
		else if(titulacio.getTambit().equals("M") | idTitulacio == 2) {
			File f = new File(this.templatePath+"/T2/"+tasca.getCodiEvidencia().replace(".", "_")+".docx");
			if(f.exists() && !f.isDirectory()) { 
				templatePath = this.templatePath+"/T2/"+tasca.getCodiEvidencia().replace(".", "_")+".docx";
			}
		}
		
		InputStream in = new FileInputStream(new File(templatePath));
		IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, reportId, TemplateEngineKind.Velocity);
		FieldsMetadata metadata = new FieldsMetadata();
		metadata.addFieldAsImage("logo");
		report.setFieldsMetadata(metadata);
		IContext context = report.createContext();
		IImageProvider img;
		if(new File(this.logoPath+"C"+Integer.toString(idCentre)+".png").exists()) {
			img = new FileImageProvider(new File(this.logoPath+"C"+Integer.toString(idCentre)+".png"));
		}
		else {
			img = new FileImageProvider(new File(this.logoPath+"C0.png"));
		}
		context.put("logo", img);
		context.put("centre", centre.getNomVal());
		context.put("titulacio", titulacio.getNomVal());
		context.put("curs", Integer.toString(proces.getCursAvaluat()-1)+" - "+Integer.toString(proces.getCursAvaluat()));
		context.put("curs_anterior", Integer.toString(proces.getCursAvaluat()-2)+" - "+Integer.toString(proces.getCursAvaluat()-1));
		
		if(tasca.getIdTipus() == 14) { // Iterable template task
			List<OrganDTO> titulacions = new ArrayList<OrganDTO>();
			titulacions = core.getTitulacionsByTypeCentre(centre.getLugar2(), ambit);
			List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
			for(OrganDTO x : titulacions) {
				HashMap<String, String> t = getTemplateDataArray(x.getLugar2(), idCentre, proces.getCursAvaluat());
				t.put("titulacio", x.getNomCas());
				data.add(t);
			}
			context.put("data", data);
			getTemplateData(idTitulacio, idCentre, proces.getCursAvaluat(), context);
		}
		else { // NO iterable template task
			getTemplateData(idTitulacio, idCentre, proces.getCursAvaluat(), context);
		}
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();  
		report.process(context, out);
	    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+tasca.getCodiEvidencia()+".docx\"");

		return out.toByteArray();
	}

	/*
	 * Test endpoint to generate a populated template for a given degree and evidence (Type of task)
	 * @param model
	 * @param idTitulacio The ID of the degree
	 * @param idCentre The ID of the center
	 * @param evidencia The name of the evidence
	 * @param curs The academic year
	 * @param tipusTasca The type of task
	 * @param response HttpServletResponse
	 * @return A byte array representing the populated template
	 */
	@GetMapping(value="/test/template2/{titulacio}/{centre}/{evidencia}/{curs}/{tipusTasca}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
	@ResponseBody
	public byte[] testTemplate(Model model, @PathVariable("titulacio") Integer idTitulacio, @PathVariable("centre") Integer idCentre,
			@PathVariable("evidencia") String evidencia, @PathVariable("curs") Integer curs, @PathVariable("tipusTasca") Integer tipusTasca, 
			HttpServletResponse response) throws IOException, XDocReportException {
		XDocReportRegistry.getRegistry().clear();
		String reportId = "none";
		
		OrganDTO titulacio = core.findOrganById("T", idTitulacio);
		OrganDTO centre = core.findOrganById("C", idCentre);
		Integer ambit = idTitulacio/(int)1000;
		
		/* Check if specific template exists */
		String templatePath = this.templatePath+evidencia.replace(".", "_").replace(" (G)", "").replace(" (M)", "")+".docx";
		if(idTitulacio < 2000 | idTitulacio == 1) {
			File f = new File(this.templatePath+"/T1/"+evidencia.replace(".", "_").replace(" (G)", "").replace(" (M)", "")+".docx");
			if(f.exists() && !f.isDirectory()) { 
				templatePath = this.templatePath+"/T1/"+evidencia.replace(".", "_").replace(" (G)", "").replace(" (M)", "")+".docx";
			}
			ambit = 1;
		}
		else if((idTitulacio >= 2000 & idTitulacio < 3000) | idTitulacio == 2) {
			File f = new File(this.templatePath+"/T2/"+evidencia.replace(".", "_").replace(" (G)", "").replace(" (M)", "")+".docx");
			if(f.exists() && !f.isDirectory()) { 
				templatePath = this.templatePath+"/T2/"+evidencia.replace(".", "_").replace(" (G)", "").replace(" (M)", "")+".docx";
			}
			ambit = 2;
		}
		
		InputStream in = new FileInputStream(new File(templatePath));
		IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, reportId, TemplateEngineKind.Velocity);
		FieldsMetadata metadata = new FieldsMetadata();
		metadata.addFieldAsImage("logo");
		report.setFieldsMetadata(metadata);
		IContext context = report.createContext();
		IImageProvider img;
		if(new File(this.logoPath+"C"+Integer.toString(idCentre)+".png").exists()) {
			img = new FileImageProvider(new File(this.logoPath+"C"+Integer.toString(idCentre)+".png"));
		}
		else {
			img = new FileImageProvider(new File(this.logoPath+"C0.png"));
		}
		context.put("logo", img);
		context.put("centre", centre.getNomVal());
		context.put("titulacio", titulacio.getNomVal());
		context.put("curs", Integer.toString(curs-1)+" - "+Integer.toString(curs));
		context.put("curs_anterior", Integer.toString(curs-2)+" - "+Integer.toString(curs-1));
		
		if(tipusTasca == 14) { // Iterable template task
			List<OrganDTO> titulacions = new ArrayList<OrganDTO>();
			titulacions = core.getTitulacionsByTypeCentre(centre.getLugar2(), ambit);
			List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
			for(OrganDTO x : titulacions) {
				HashMap<String, String> t = getTemplateDataArray(x.getLugar2(), idCentre, curs);
				t.put("titulacio", x.getNomCas());
				data.add(t);
			}
			context.put("data", data);
			getTemplateData(idTitulacio, idCentre, curs, context);
		}
		else { // NO iterable template task
			getTemplateData(idTitulacio, idCentre, curs, context);
		}
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();  
		report.process(context, out);
	    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+evidencia+".docx\"");

		return out.toByteArray();
		
	}
	
	/*
	 * Generate a PDF from the content of a task instance (unused)
	 * @param model
	 * @param idTascai The ID of the task instance
	 * @param response HttpServletResponse
	 * @return A byte array representing the generated PDF
	 */
	@GetMapping(value="/pdf/download/{idTascai}")
	@ResponseBody
	public byte[] downloadTemplatePdf(@PathVariable("idTascai") BigInteger idTascai, HttpServletResponse response) throws IOException, InterruptedException {
		InstanciaTascaDTO it = core.findInstanciaTascaById(idTascai);
		response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+it.getIdInstanciaTasca()+".pdf\"");
		response.setHeader(HttpHeaders.CONTENT_TYPE, "application/pdf");
		return pls.toPDF(it.getText(), Optional.of(idTascai));
	}

	/*
	 * Generate a PDF preview from provided content
	 * @param model
	 * @param content The content to convert to PDF
	 * @param idtascai Optional ID of the task instance for context
	 * @param response HttpServletResponse
	 */
	@PostMapping(value="/pdf/preview")
	@ResponseBody
	public byte[] downloadTemplatePdf(HttpServletResponse response,
			@RequestBody PdfDTO pdf) throws IOException, InterruptedException {
		response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"borrador.pdf\"");
		response.setHeader(HttpHeaders.CONTENT_TYPE, "application/pdf");	
		return pls.toPDF(pdf.getContent(), pdf.getIdtascai());
	}
}
