pdf generating

merge-requests/60/head
Семенова Мария 5 years ago
parent 10fe7e8a4e
commit cf9a7f895f

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

@ -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<byte[]> 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()))

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

@ -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 */

@ -20,7 +20,8 @@
<hr/>
<div class="row">
<div class="col-lg-12">
<form id="paper-form" method="post" th:action="@{'/papers/paper?id='+ *{id == null ? '' : id} + ''}"
<form name="paperform" id="paper-form" method="post"
th:action="@{'/papers/paper?id='+ *{id == null ? '' : id} + ''}"
th:object="${paperDto}">
<div class="row">
@ -143,6 +144,15 @@
</div>
</div>
<div class="form-group">
<button id="pdfBtn" class="btn btn-primary text-uppercase"
onclick="generatePDF()"
type="button">
<i id="pdfLoadingIcon" class='fa fa-circle-o-notch fa-spin'
style="display: none;"></i>
pdf
</button>
</div>
</div>
</div>
</div>
@ -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);
;
}
}
</script>
</div>

Loading…
Cancel
Save