remove JLR library using

This commit is contained in:
Семенова Мария 2019-03-28 14:51:17 +04:00
parent cf9a7f895f
commit 0c34571354
2 changed files with 50 additions and 9 deletions

View File

@ -118,7 +118,7 @@ public class PaperController {
}
@PostMapping("/generatePdf")
public ResponseEntity<byte[]> getPdfFile(PaperDto paper) throws IOException {
public ResponseEntity<byte[]> getPdfFile(PaperDto paper) throws IOException, InterruptedException {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename='" +
URLEncoder.encode(paper.getTitle() + ".pdf", UTF_8.toString()) + "'");

View File

@ -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;
}
}
}