diff --git a/src/main/java/ru/ulstu/file/service/FileService.java b/src/main/java/ru/ulstu/file/service/FileService.java index 234ea2a..9e1ece2 100644 --- a/src/main/java/ru/ulstu/file/service/FileService.java +++ b/src/main/java/ru/ulstu/file/service/FileService.java @@ -6,7 +6,10 @@ import org.springframework.web.multipart.MultipartFile; import ru.ulstu.file.model.FileData; import ru.ulstu.file.model.FileDataDto; import ru.ulstu.file.repostory.FileRepository; +import ru.ulstu.paper.model.PaperDto; +import java.io.BufferedWriter; +import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -15,6 +18,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; import static java.nio.charset.StandardCharsets.UTF_8; @@ -119,4 +123,24 @@ public class FileService { public FileDataDto createFromMultipartFile(MultipartFile multipartFile) throws IOException { return new FileDataDto(multipartFile.getOriginalFilename(), uploadToTmpDir(multipartFile)); } + + public void createLatexAttachs(PaperDto paper) throws IOException { + for (FileDataDto fileDataDto : paper.getFiles().stream().filter(f -> f.isLatexAttach() && !f.isDeleted()).collect(Collectors.toList())) { + if (fileDataDto.getId() == null) { + File oldFile = getTmpFilePath(fileDataDto.getTmpFileName()).toFile(); + File renamed = getTmpFilePath(fileDataDto.getName()).toFile(); + oldFile.renameTo(renamed); + } else { + Files.write(getTmpFilePath(fileDataDto.getName()), fileRepository.findOne(fileDataDto.getId()).getData()); + } + } + } + + public File createLatexFile(PaperDto paper) throws IOException { + BufferedWriter writer = Files.newBufferedWriter(getTmpFilePath(paper.getTitle() + ".tex")); + writer.write(paper.getLatexText()); + writer.close(); + + return getTmpFilePath(paper.getTitle() + ".tex").toFile(); + } } diff --git a/src/main/java/ru/ulstu/paper/controller/PaperController.java b/src/main/java/ru/ulstu/paper/controller/PaperController.java index 31df0e3..6bc550a 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperController.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperController.java @@ -1,5 +1,8 @@ package ru.ulstu.paper.controller; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.Errors; @@ -13,16 +16,19 @@ import ru.ulstu.deadline.model.Deadline; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.model.PaperFilterDto; +import ru.ulstu.paper.service.LatexService; import ru.ulstu.paper.service.PaperService; import ru.ulstu.user.model.User; import javax.validation.Valid; import java.io.IOException; +import java.net.URLEncoder; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.stream.Collectors; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.springframework.util.StringUtils.isEmpty; @@ -30,9 +36,11 @@ import static org.springframework.util.StringUtils.isEmpty; @RequestMapping(value = "/papers") public class PaperController { private final PaperService paperService; + private final LatexService latexService; - public PaperController(PaperService paperService) { + public PaperController(PaperService paperService, LatexService latexService) { this.paperService = paperService; + this.latexService = latexService; } @GetMapping("/papers") @@ -109,6 +117,14 @@ public class PaperController { return years; } + @PostMapping("/generatePdf") + public ResponseEntity getPdfFile(PaperDto paper) throws IOException { + HttpHeaders headers = new HttpHeaders(); + headers.add("Content-Disposition", "attachment; filename='" + + URLEncoder.encode(paper.getTitle() + ".pdf", UTF_8.toString()) + "'"); + return new ResponseEntity<>(latexService.generatePdfFromLatexFile(paper), headers, HttpStatus.OK); + } + private void filterEmptyDeadlines(PaperDto paperDto) { paperDto.setDeadlines(paperDto.getDeadlines().stream() .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) diff --git a/src/main/java/ru/ulstu/paper/service/LatexService.java b/src/main/java/ru/ulstu/paper/service/LatexService.java new file mode 100644 index 0000000..b78f497 --- /dev/null +++ b/src/main/java/ru/ulstu/paper/service/LatexService.java @@ -0,0 +1,33 @@ +package ru.ulstu.paper.service; + +import de.nixosoft.jlr.JLRGenerator; +import org.springframework.stereotype.Service; +import ru.ulstu.file.service.FileService; +import ru.ulstu.paper.model.PaperDto; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +@Service +public class LatexService { + private FileService fileService; + + public LatexService(FileService fileService) { + this.fileService = fileService; + } + + public byte[] generatePdfFromLatexFile(PaperDto paper) throws IOException { + fileService.createLatexAttachs(paper); + File tex = fileService.createLatexFile(paper); + + File output = new File(tex.getParentFile().getAbsolutePath() + File.separator); + JLRGenerator pdfGen = new JLRGenerator(); + + if (!pdfGen.generate(tex, output, tex.getParentFile())) { + throw new IOException(pdfGen.getErrorMessage()); + } + + return Files.readAllBytes(pdfGen.getPDF().toPath()); + } +} diff --git a/src/main/resources/public/js/core.js b/src/main/resources/public/js/core.js index 216f411..78256e0 100644 --- a/src/main/resources/public/js/core.js +++ b/src/main/resources/public/js/core.js @@ -3,6 +3,7 @@ var urlFileUpload = "/api/1.0/files/uploadTmpFile"; var urlFileDownload = "/api/1.0/files/download/"; +var urlPdfGenerating = "/papers/generatePdf"; var urlFileDownloadTmp = "/api/1.0/files/download-tmp/"; /* exported MessageTypesEnum */ diff --git a/src/main/resources/templates/papers/paper.html b/src/main/resources/templates/papers/paper.html index f2c5332..2d89936 100644 --- a/src/main/resources/templates/papers/paper.html +++ b/src/main/resources/templates/papers/paper.html @@ -20,7 +20,8 @@
-
@@ -143,6 +144,15 @@
+
+ +
@@ -325,6 +335,38 @@ } } } + + function generatePDF() { + $('#pdfLoadingIcon').show(); + $('#pdfBtn').prop('disabled', true); + ; + + var formData = new FormData(document.forms.paperform); + var xhr = new XMLHttpRequest(); + xhr.open("POST", urlPdfGenerating); + xhr.send(formData); + xhr.responseType = 'blob'; + + xhr.onload = function () { + if (this.status == 200) { + console.debug(this.response); + var blob = new Blob([this.response], {type: 'application/pdf'}); + let a = document.createElement("a"); + a.style = "display: none"; + document.body.appendChild(a); + let url = window.URL.createObjectURL(blob); + a.href = url; + a.download = $('#title').val() + '.pdf'; + a.click(); + window.URL.revokeObjectURL(url); + } else { + } + $('#pdfLoadingIcon').hide(); + $('#pdfBtn').prop('disabled', false); + ; + } + } +