[ADD] PDF REST AHORA ACEPTA TAMBIEN FORM DATA

parent 52b19f6d
......@@ -10,6 +10,8 @@ import lombok.AllArgsConstructor;
import web.multitask.trismegistoservices.services.PDFService;
import web.multitask.trismegistoservices.utils.CommonUtils;
import java.util.Map;
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/pdf")
......@@ -19,34 +21,48 @@ public class pdfRest {
PDFService pdfService;
CommonUtils commonUtils;
@PostMapping("/public/html")
public ResponseEntity<?> htmlToPdf(@RequestBody String json) {
@PostMapping(value = "/public/html", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public ResponseEntity<?> htmlToPdf(@RequestParam Map<String, String> params, @RequestBody(required = false) String json) {
try {
String html;
String fileName;
boolean isBase64;
if (json != null && !json.isEmpty()) {
JSONObject jsonBody = new JSONObject(json);
html = jsonBody.getString("html");
fileName = jsonBody.optString("file_name", "pdf.pdf");
isBase64 = jsonBody.optBoolean("base64");
} else {
html = params.get("html");
fileName = params.getOrDefault("file_name", "pdf.pdf");
isBase64 = Boolean.parseBoolean(params.get("base64"));
}
JSONObject jsonBody = new JSONObject(json);
byte[] bytes = pdfService.generatePdf(jsonBody.getString("html"));
byte[] bytes = pdfService.generatePdf(html);
if(jsonBody.optBoolean("base64")){
if (isBase64) {
String base64 = commonUtils.byteToBase64(bytes);
return ResponseEntity.ok().body(
new JSONObject()
.put("base64", base64)
.put("file_name", jsonBody.optString("file_name", "pdf.pdf"))
.put("status", true)
.put("message", "OK")
new JSONObject()
.put("base64", base64)
.put("file_name", fileName)
.put("status", true)
.put("message", "OK")
.toString()
);
}else{
Resource resource = commonUtils.byteToResource(bytes, jsonBody.optString("file_name", "no_name.pdf"));
} else {
Resource resource = commonUtils.byteToResource(bytes, fileName);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + jsonBody.optString("file_name", "no_name.pdf"));
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
return ResponseEntity.ok()
.headers(headers)
.contentLength(bytes.length)
.contentType(MediaType.APPLICATION_PDF)
.body(resource);
.headers(headers)
.contentLength(bytes.length)
.contentType(MediaType.APPLICATION_PDF)
.body(resource);
}
}catch (Exception e){
System.out.println(e);
} catch (Exception e) {
System.err.println(e.getMessage());
return ResponseEntity.internalServerError().body(e.getMessage());
}
}
......
......@@ -3,8 +3,6 @@ package web.multitask.trismegistoservices.services;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import org.json.JSONArray;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.stereotype.Service;
import java.io.File;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment