-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
error handling, excel, bug fixes, name fixes (#57)
- Loading branch information
Showing
27 changed files
with
558 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/stirling/software/SPDF/controller/converters/ConvertXlsxController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package stirling.software.SPDF.controller.converters; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
|
||
import org.apache.poi.ss.usermodel.Color; | ||
import org.apache.poi.ss.usermodel.Workbook; | ||
import org.apache.poi.ss.usermodel.WorkbookFactory; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import com.itextpdf.text.BaseColor; | ||
import com.itextpdf.text.Document; | ||
import com.itextpdf.text.DocumentException; | ||
import com.itextpdf.text.Element; | ||
import com.itextpdf.text.pdf.PdfPCell; | ||
import com.itextpdf.text.pdf.PdfPTable; | ||
import com.itextpdf.text.pdf.PdfWriter; | ||
|
||
import stirling.software.SPDF.utils.PdfUtils; | ||
|
||
@Controller | ||
public class ConvertXlsxController { | ||
|
||
|
||
@GetMapping("/xlsx-to-pdf") | ||
public String cinvertToPDF(Model model) { | ||
model.addAttribute("currentPage", "xlsx-to-pdf"); | ||
return "convert/xlsx-to-pdf"; | ||
} | ||
|
||
@PostMapping("/xlsx-to-pdf") | ||
public ResponseEntity<byte[]> convertToPDF(@RequestParam("fileInput") MultipartFile xlsx) throws IOException, DocumentException{ | ||
// Load Excel file | ||
|
||
Workbook workbook = WorkbookFactory.create(xlsx.getInputStream()); | ||
|
||
// Create PDF document | ||
Document document = new Document(); | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
PdfWriter.getInstance(document, outputStream); | ||
document.open(); | ||
|
||
// Convert each sheet in Excel to a separate page in PDF | ||
for (int i = 0; i < workbook.getNumberOfSheets(); i++) { | ||
PdfPTable table = new PdfPTable(workbook.getSheetAt(i).getRow(0).getPhysicalNumberOfCells()); | ||
for (int row = 0; row < workbook.getSheetAt(i).getPhysicalNumberOfRows(); row++) { | ||
for (int cell = 0; cell < workbook.getSheetAt(i).getRow(row).getPhysicalNumberOfCells(); cell++) { | ||
PdfPCell pdfCell = new PdfPCell(); | ||
pdfCell.addElement(new com.itextpdf.text.Paragraph(workbook.getSheetAt(i).getRow(row).getCell(cell).toString())); | ||
|
||
// Copy cell style, borders, and background color | ||
pdfCell.setBorderColor(new BaseColor(workbook.getSheetAt(i).getRow(row).getCell(cell).getCellStyle().getBottomBorderColor())); | ||
pdfCell.setBorderColor(new BaseColor(workbook.getSheetAt(i).getRow(row).getCell(cell).getCellStyle().getTopBorderColor())); | ||
pdfCell.setBorderColor(new BaseColor(workbook.getSheetAt(i).getRow(row).getCell(cell).getCellStyle().getLeftBorderColor())); | ||
pdfCell.setBorderColor(new BaseColor(workbook.getSheetAt(i).getRow(row).getCell(cell).getCellStyle().getRightBorderColor())); | ||
Short bc = workbook.getSheetAt(i).getRow(row).getCell(cell).getCellStyle().getFillBackgroundColor(); | ||
pdfCell.setBackgroundColor(new BaseColor(bc)); | ||
|
||
table.addCell(pdfCell); | ||
} | ||
} | ||
document.add(table); | ||
} | ||
// Close document and output stream | ||
document.close(); | ||
outputStream.flush(); | ||
outputStream.close(); | ||
return PdfUtils.boasToWebResponse(outputStream, xlsx.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_convertedToPDF.pdf"); | ||
// Close document and input stream | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/stirling/software/SPDF/utils/ErrorUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.