error handling, excel, bug fixes, name fixes (#57)
This commit is contained in:
32
src/main/java/stirling/software/SPDF/utils/ErrorUtils.java
Normal file
32
src/main/java/stirling/software/SPDF/utils/ErrorUtils.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package stirling.software.SPDF.utils;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
public class ErrorUtils {
|
||||
|
||||
public static Model exceptionToModel(Model model, Exception ex) {
|
||||
StringWriter sw = new StringWriter();
|
||||
ex.printStackTrace(new PrintWriter(sw));
|
||||
String stackTrace = sw.toString();
|
||||
|
||||
model.addAttribute("errorMessage", ex.getMessage());
|
||||
model.addAttribute("stackTrace", stackTrace);
|
||||
return model;
|
||||
}
|
||||
|
||||
public static ModelAndView exceptionToModelView(Model model, Exception ex) {
|
||||
StringWriter sw = new StringWriter();
|
||||
ex.printStackTrace(new PrintWriter(sw));
|
||||
String stackTrace = sw.toString();
|
||||
|
||||
ModelAndView modelAndView = new ModelAndView();
|
||||
modelAndView.addObject("errorMessage", ex.getMessage());
|
||||
modelAndView.addObject("stackTrace", stackTrace);
|
||||
return modelAndView;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,6 +29,9 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import com.itextpdf.text.Document;
|
||||
import com.itextpdf.text.DocumentException;
|
||||
import com.itextpdf.text.pdf.PdfWriter;
|
||||
import com.spire.pdf.PdfDocument;
|
||||
|
||||
public class PdfUtils {
|
||||
@@ -157,6 +160,18 @@ public class PdfUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static ResponseEntity<byte[]> iTextDocToWebResponse(Document document, String docName) throws IOException, DocumentException {
|
||||
// Close the document
|
||||
document.close();
|
||||
|
||||
// Open Byte Array and save document to it
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
PdfWriter.getInstance(document, baos).close();
|
||||
|
||||
|
||||
return PdfUtils.boasToWebResponse(baos, docName);
|
||||
}
|
||||
|
||||
public static ResponseEntity<byte[]> pdfDocToWebResponse(PdfDocument document, String docName) throws IOException {
|
||||
|
||||
// Open Byte Array and save document to it
|
||||
|
||||
Reference in New Issue
Block a user