저는 Spring 애플리케이션을 가지고 있으며 PDF 생성에 대한 지원을 구축해야합니다. 나는 플라잉 받침을 Thymeleaf와 함께 사용하여 PDF를 렌더링 할 생각입니다. 그러나 비행 접시를 Thymeleaf와 함께 사용하는 것에 대한 많은 정보를 찾을 수 없습니다. 다른 사람들이 기술을 함께 사용 했습니까?플라잉 받침, Thymeleaf 및 Spring
15
A
답변
17
나는 Thymeleaf 2.0.14와 함께 Flyingsaucer-R8을 문제없이 사용하고 있습니다. Thymeleaf의 최신 버전도 잘 작동합니다.
TemplateEngine
은이 용도로 구성된 클래스 경로 템플릿 확인자와 별도로 있습니다. 이를 사용하여 XHTML
을 String
으로 생성하십시오. Flyingsaucer는 결과로부터 PDF 문서를 생성합니다. 아래의 예를 확인하십시오. NOT 생산 NO 보증와 준비 코드를 사용하여 - 아래
코드 예입니다. 명확하게하기 위해 try-catch 처리 및 리소스 캐싱이 없습니다 (PDF 작성은 비용이 많이 드는 작업 임). 그것을 고려하십시오.
코드
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.springframework.core.io.ClassPathResource;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
public class FlyingSoucerTestService {
public void test() throws DocumentException, IOException {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("META-INF/pdfTemplates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("XHTML");
templateResolver.setCharacterEncoding("UTF-8");
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
Context ctx = new Context();
ctx.setVariable("message", "I don't want to live on this planet anymore");
String htmlContent = templateEngine.process("messageTpl", ctx);
ByteOutputStream os = new ByteOutputStream();
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
ClassPathResource regular = new ClassPathResource("/META-INF/fonts/LiberationSerif-Regular.ttf");
fontResolver.addFont(regular.getURL().toString(), BaseFont.IDENTITY_H, true);
renderer.setDocumentFromString(htmlContent);
renderer.layout();
renderer.createPDF(os);
byte[] pdfAsBytes = os.getBytes();
os.close();
FileOutputStream fos = new FileOutputStream(new File("/tmp/message.pdf"));
fos.write(pdfAsBytes);
fos.close();
}
}
템플릿
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring3-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
div.border {
border: solid;
border-width: 1px 1px 0px 1px;
padding: 5px 20px 5px 20px;
}
</style>
</head>
<body style="font-family: Liberation Serif;">
<div class="border">
<h1 th:text="${message}">message</h1>
</div>
</body>
</html>
관련 문제
- 1. 플라잉 받침 iTextPDF 중국어 글꼴
- 2. 플라잉 받침 PDF가 색상을 표시하지 않습니다.
- 3. 플라잉 받침 - ITextRenderer createPdf 매우 느림
- 4. jsp를 플라잉 받침 접시를 사용하여 PDF로 변환
- 5. 플라잉 받침 - html에서 png로 항상 높이 16 픽셀 렌더링
- 6. Thymeleaf Spring weblow ajax
- 7. GWT & Thymeleaf & Spring MVC
- 8. Spring Boot, Thymeleaf, @Controller
- 9. 다중보기 확인자 구성 (Thymeleaf 및 Json) Spring
- 10. Spring Boot 및 Thymeleaf 리소스 폴더 문제
- 11. Spring, Thymeleaf 및 비동기 데이터 로딩
- 12. Thymeleaf 및 Spring MVC로 큰보기 구성하기
- 13. Spring-Boot + Spring-MVC + Thymeleaf + Apache Tiles
- 14. Spring MVC + Swagger를 사용하는 Thymeleaf
- 15. Thymeleaf Spring 메일 컨텍스트 오류
- 16. Spring MVC 3을 사용하는 Thymeleaf
- 17. Spring + Thymeleaf 바인딩 양식 NullpointerException
- 18. Spring-Boot Thymeleaf 현지화 문제
- 19. Thymeleaf 및 정적 컨텐츠
- 20. 플라잉 문자
- 21. Spring-mvc + Thymeleaf : 복잡한 형식 다루기
- 22. thymeleaf 템플릿에 Spring MVC 모델 속성을 표시합니다.
- 23. 의존성에서 중복 클래스를 받침
- 24. Spring Boot (1.4.1) 및 Thymeleaf (3) MessageSource 단일 따옴표
- 25. Spring MVC 및 Thymeleaf 애플리케이션을위한 UTF8MB4 문자 인코딩
- 26. Spring 3 및 Thymeleaf 템플릿 엔진 구성 문제
- 27. 하나의 Spring MVC 애플리케이션에서 Thymeleaf 및 JSP resolvers
- 28. 보기 파일을 thymeleaf 및 spring mvc 파일 시스템에 저장
- 29. Thymeleaf 3 및 Spring 4의 여러 위치에서 동일한 양식
- 30. [Spring MVC - Thymeleaf] - 양식 유효성 검사 및 오류 메시지
michal.kreuzman @이 솔루션은 나를 조금 도와 있기 때문에 당신이 우리와 함께 여전히 바랍니다. 모두 제일 좋다. 위에서 인용 한 것 : 'ctx.setVariable ("message", "나는이 행성에 더 이상 살고 싶지 않다"); ' – KingJahfy
이 접근법은 번역을 위해 messages.properties를 사용하는 국제화를 다루지 않는다. 게다가, 그것은 효과가있다. – SiriusBits