Files
erp-imprimelibros/src/main/java/com/imprimelibros/erp/pdf/PdfController.java

63 lines
2.4 KiB
Java

package com.imprimelibros.erp.pdf;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ContentDisposition;
import java.util.Locale;
@RestController
@RequestMapping("/api/pdf")
public class PdfController {
private final PdfService pdfService;
public PdfController(PdfService pdfService) {
this.pdfService = pdfService;
}
@GetMapping(value = "/{type}/{id}", produces = "application/pdf")
public ResponseEntity<byte[]> generate(
@PathVariable("type") String type,
@PathVariable String id,
@RequestParam(defaultValue = "inline") String mode,
Locale locale) {
if (id == null) {
throw new IllegalArgumentException("Falta el ID para generar el PDF");
}
if (type.equals(DocumentType.PRESUPUESTO.toString())) {
Long presupuestoId = Long.valueOf(id);
byte[] pdf = pdfService.generaPresupuesto(presupuestoId, locale);
var headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_PDF);
headers.setContentDisposition(
("download".equals(mode)
? ContentDisposition.attachment()
: ContentDisposition.inline()).filename("presupuesto-" + id + ".pdf").build());
return new ResponseEntity<>(pdf, headers, HttpStatus.OK);
}/*else if(type.equals(DocumentType.PEDIDO.toString())) {
} */else if (type.equals(DocumentType.FACTURA.toString())) {
Long facturaId = Long.valueOf(id);
byte[] pdf = pdfService.generaFactura(facturaId, locale);
var headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_PDF);
headers.setContentDisposition(
("download".equals(mode)
? ContentDisposition.attachment()
: ContentDisposition.inline()).filename("factura-" + id + ".pdf").build());
return new ResponseEntity<>(pdf, headers, HttpStatus.OK);
}
else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
}
}