remove JLR library using
This commit is contained in:
parent
cf9a7f895f
commit
0c34571354
@ -118,7 +118,7 @@ public class PaperController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/generatePdf")
|
@PostMapping("/generatePdf")
|
||||||
public ResponseEntity<byte[]> getPdfFile(PaperDto paper) throws IOException {
|
public ResponseEntity<byte[]> getPdfFile(PaperDto paper) throws IOException, InterruptedException {
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
headers.add("Content-Disposition", "attachment; filename='" +
|
headers.add("Content-Disposition", "attachment; filename='" +
|
||||||
URLEncoder.encode(paper.getTitle() + ".pdf", UTF_8.toString()) + "'");
|
URLEncoder.encode(paper.getTitle() + ".pdf", UTF_8.toString()) + "'");
|
||||||
|
@ -1,33 +1,74 @@
|
|||||||
package ru.ulstu.paper.service;
|
package ru.ulstu.paper.service;
|
||||||
|
|
||||||
import de.nixosoft.jlr.JLRGenerator;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import ru.ulstu.file.service.FileService;
|
import ru.ulstu.file.service.FileService;
|
||||||
import ru.ulstu.paper.model.PaperDto;
|
import ru.ulstu.paper.model.PaperDto;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class LatexService {
|
public class LatexService {
|
||||||
|
private String errorMessage;
|
||||||
|
private File pdfFile;
|
||||||
private FileService fileService;
|
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) {
|
public LatexService(FileService fileService) {
|
||||||
this.fileService = fileService;
|
this.fileService = fileService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] generatePdfFromLatexFile(PaperDto paper) throws IOException {
|
public byte[] generatePdfFromLatexFile(PaperDto paper) throws IOException, InterruptedException {
|
||||||
fileService.createLatexAttachs(paper);
|
fileService.createLatexAttachs(paper);
|
||||||
File tex = fileService.createLatexFile(paper);
|
File tex = fileService.createLatexFile(paper);
|
||||||
|
|
||||||
File output = new File(tex.getParentFile().getAbsolutePath() + File.separator);
|
if (!generate(paper.getTitle(), tex.getParentFile())) {
|
||||||
JLRGenerator pdfGen = new JLRGenerator();
|
throw new IOException(errorMessage);
|
||||||
|
|
||||||
if (!pdfGen.generate(tex, output, tex.getParentFile())) {
|
|
||||||
throw new IOException(pdfGen.getErrorMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user