diff --git a/src/main/java/ru/ulstu/paper/controller/PaperController.java b/src/main/java/ru/ulstu/paper/controller/PaperController.java index 6bc550a..e0dec98 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperController.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperController.java @@ -118,7 +118,7 @@ public class PaperController { } @PostMapping("/generatePdf") - public ResponseEntity getPdfFile(PaperDto paper) throws IOException { + public ResponseEntity getPdfFile(PaperDto paper) throws IOException, InterruptedException { HttpHeaders headers = new HttpHeaders(); headers.add("Content-Disposition", "attachment; filename='" + URLEncoder.encode(paper.getTitle() + ".pdf", UTF_8.toString()) + "'"); diff --git a/src/main/java/ru/ulstu/paper/service/LatexService.java b/src/main/java/ru/ulstu/paper/service/LatexService.java index b78f497..a44a649 100644 --- a/src/main/java/ru/ulstu/paper/service/LatexService.java +++ b/src/main/java/ru/ulstu/paper/service/LatexService.java @@ -1,33 +1,74 @@ 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.BufferedReader; import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; import java.nio.file.Files; @Service public class LatexService { + private String errorMessage; + private File pdfFile; private FileService fileService; + private final String pdfLatexError = "Errors occurred while executing pdfLaTeX."; + private final String bibtexError = "Errors occurred while executing bibtex."; public LatexService(FileService fileService) { this.fileService = fileService; } - public byte[] generatePdfFromLatexFile(PaperDto paper) throws IOException { + public byte[] generatePdfFromLatexFile(PaperDto paper) throws IOException, InterruptedException { 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()); + if (!generate(paper.getTitle(), tex.getParentFile())) { + throw new IOException(errorMessage); } - return Files.readAllBytes(pdfGen.getPDF().toPath()); + return Files.readAllBytes(pdfFile.toPath()); + } + + private int startProcess(String[] args, File dir, String message) throws IOException, InterruptedException { + ProcessBuilder processBuilder = new ProcessBuilder(args); + processBuilder.redirectErrorStream(true); + processBuilder.directory(dir); + + Process process = processBuilder.start(); + InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream()); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + + try { + while ((bufferedReader.readLine()) != null) ; + } finally { + bufferedReader.close(); + } + + int exitCode = process.waitFor(); + if (exitCode != 0) { + errorMessage = message + " Exit value of the process: " + exitCode; + } + return exitCode; + } + + private boolean generate(String filename, File dir) throws IOException, InterruptedException { + if (startProcess(new String[]{"pdflatex", filename}, dir, pdfLatexError) != 0) return false; + startProcess(new String[]{"bibtex", filename}, dir, bibtexError); + if (startProcess(new String[]{"pdflatex", filename}, dir, pdfLatexError) != 0) return false; + return checkPdf(filename, dir); + } + + private boolean checkPdf(String filename, File dir) { + pdfFile = new File(dir.getAbsolutePath() + File.separator + filename + ".pdf"); + + if (pdfFile.isFile()) return true; + else { + errorMessage = "The pdf file could not be created."; + return false; + } } }