acabando presupuesto

This commit is contained in:
2025-10-16 21:46:02 +02:00
parent ff9c04afb6
commit ea8a005cde
11 changed files with 349 additions and 139 deletions

View File

@ -1,32 +1,47 @@
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;
import java.util.Map;
@RestController
@RequestMapping("/api/pdf")
public class PdfController {
private final PdfService pdfService;
public PdfController(PdfService pdfService) { this.pdfService = pdfService; }
public PdfController(PdfService pdfService) {
this.pdfService = pdfService;
}
@PostMapping("/{type}/{templateId}")
@GetMapping(value = "/{type}/{id}", produces = "application/pdf")
public ResponseEntity<byte[]> generate(
@PathVariable("type") DocumentType type,
@PathVariable String templateId,
@RequestBody Map<String,Object> model,
@PathVariable("type") String type,
@PathVariable String id,
@RequestParam(defaultValue = "inline") String mode,
Locale locale) {
var spec = new DocumentSpec(type, templateId, locale, model);
var pdf = pdfService.generate(spec);
if (type.equals(DocumentType.PRESUPUESTO.toString()) && id == null) {
throw new IllegalArgumentException("Falta el ID del presupuesto 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 {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
var fileName = type.name().toLowerCase() + "-" + templateId + ".pdf";
return ResponseEntity.ok()
.header("Content-Type", "application/pdf")
.header("Content-Disposition", "inline; filename=\"" + fileName + "\"")
.body(pdf);
}
}