/
ilyaBu
/
WebSpringJava
Обзор
Документация
Войти
/
ilyaBu
/
WebSpringJava
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/ru/gb/spring/diplomawebjavaspringgb/service/impl/PdfGenerateServiceImpl.java
108 строк
4 KB
IliaBu
Update code
13 июл 2024, 17:47
13 июл 2024, 17:47
bfc632e
Код
Авторство
О чём код?
package ru.gb.spring.diplomawebjavaspringgb.service.impl; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.ui.Model; import ru.gb.spring.diplomawebjavaspringgb.service.PdfGenerateService; import com.lowagie.text.pdf.BaseFont; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import org.xhtmlrenderer.pdf.ITextRenderer; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.io.File; /** * Сервис создания pdf-файлов. */ @Service public class PdfGenerateServiceImpl implements PdfGenerateService { @Autowired private TemplateEngine templateEngine; public static final String path = "./pdf/"; @Override public void generatePdfFile(String templateName, Map<String, Object> data, String pdfFileName) { Context context = new Context(); context.setVariables(data); String htmlContent = templateEngine.process(templateName, context); try { if (!Files.exists(Path.of(path))) { Files.createDirectory(Paths.get(path)); } File myFile = new File(path + pdfFileName); FileOutputStream fileOutputStream = new FileOutputStream(myFile); ITextRenderer renderer = new ITextRenderer(); renderer.setDocumentFromString(htmlContent); renderer.getFontResolver().addFont("static/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); renderer.layout(); renderer.createPDF(fileOutputStream); fileOutputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } @Override public void loadListDownloads(Model model) { List<String> list = new ArrayList<String>(); File files = new File(path); String[] fileList = files.list(); for (String name : fileList) { list.add(name); } model.addAttribute("list", list); } @Override public ResponseEntity<Resource> downloadFile(String name) { try { File file = new File(path + name); Path path = Paths.get(file.getAbsolutePath()); ByteArrayResource resource = new ByteArrayResource (Files.readAllBytes(path)); return ResponseEntity.ok().headers(this.headers(name)) .contentLength(file.length()) .contentType(MediaType.parseMediaType ("application/octet-stream")).body(resource); } catch (IOException e) { throw new RuntimeException(e); } } @Override public String delete(String name) { try { Files.deleteIfExists(Paths.get(path + name)); return "redirect:/list-parts"; } catch (IOException e) { throw new RuntimeException(e); } } private HttpHeaders headers(String name) { HttpHeaders header = new HttpHeaders(); header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + name); header.add("Cache-Control", "no-cache, no-store," + " must-revalidate"); header.add("Pragma", "no-cache"); header.add("Expires", "0"); return header; } }