-
Notifications
You must be signed in to change notification settings - Fork 1
[SRLT-155] PDF AI 리포트 생성 및 완료 메일 PDF 첨부 발송 #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
2ghrms
wants to merge
19
commits into
develop
Choose a base branch
from
SRLT-155-ai-리포트-email에-리포트-pdf-업로드
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
The head ref may contain hidden characters: "SRLT-155-ai-\uB9AC\uD3EC\uD2B8-email\uC5D0-\uB9AC\uD3EC\uD2B8-pdf-\uC5C5\uB85C\uB4DC"
Open
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d19dd26
[SRLT-155] Feat: AI 리포트 메일용 PDF html
2ghrms 77bc0db
[SRLT-155] Feat: AI 리포트 메일용 HTML -> PDF 변환시 디자인 고려하여 수정
2ghrms 26130c2
[SRLT-155] Feat: HTML to PDF 의존성 추가
2ghrms ccf04d7
[SRLT-155] Feat: PDF Renderer 및 폰트 추가
2ghrms 678603c
[SRLT-155] Feat: PDF Renderer 추가
2ghrms 5890e63
[SRLT-155] Refactor: SMTP Email Client 커스텀 에러 추가 및 RequiredArg 추가
2ghrms 07a4bd7
[SRLT-155] Refactor: 개발용 Emailpit 등의 추가를 고려한 EmailConfig의 포트 추가
2ghrms 57e017d
[SRLT-155] Refactor: event가 application 레이어임에 따라 DTO 명 및 패키지 위치 수정
2ghrms 6531dd4
[SRLT-155] Refactor: HTML to PDF를 위한 PDF Renderer 및 View Mapper 추가
2ghrms 7697ad2
[SRLT-155] Refactor: Member Lookup Port 추가
2ghrms 8d01b91
[SRLT-155] Refactor: PDF 기반 AI 리포트 채점과 이메일 전송 로직을 2개의 이벤트로 분리 / 리스너 계…
2ghrms 2da4fdf
[SRLT-155] Refactor: AI 리포트 관련 테스트 리팩토링
2ghrms a0b852b
[SRLT-155] Fix: 코드래빗 리뷰 반영
2ghrms 67d0e97
[SRLT-155] Fix: 코드래빗 리뷰 반영
2ghrms 992aa88
[SRLT-155] Fix: 코드래빗 리뷰 반영
2ghrms 641e272
Merge branch 'develop' into SRLT-155-ai-리포트-email에-리포트-pdf-업로드
2ghrms 06c66ac
[SRLT-155] Refactor: 폰트파일 캐싱 로직 수정
2ghrms 2b196ac
Merge branch 'SRLT-155-ai-리포트-email에-리포트-pdf-업로드' of https://github.c…
2ghrms d1c77bb
[SRLT-155] Fix: 테스트 수정
2ghrms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
src/main/java/starlight/adapter/shared/infrastructure/pdf/PdfRenderer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| package starlight.adapter.shared.infrastructure.pdf; | ||
|
|
||
| import com.openhtmltopdf.outputdevice.helper.BaseRendererBuilder; | ||
| import com.openhtmltopdf.pdfboxout.PdfRendererBuilder; | ||
| import com.openhtmltopdf.svgsupport.BatikSVGDrawer; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.pdfbox.pdmodel.PDDocument; | ||
| import org.apache.pdfbox.pdmodel.font.PDFont; | ||
| import org.apache.pdfbox.pdmodel.font.PDType0Font; | ||
| import org.springframework.core.io.ClassPathResource; | ||
| import org.springframework.stereotype.Component; | ||
| import org.thymeleaf.context.Context; | ||
| import org.thymeleaf.spring6.SpringTemplateEngine; | ||
| import starlight.adapter.shared.infrastructure.pdf.mapper.AiReportPdfViewMapper; | ||
| import starlight.adapter.shared.infrastructure.pdf.view.AiReportPdfView; | ||
| import starlight.application.aireport.provided.dto.AiReportResult; | ||
| import starlight.application.aireport.required.AiReportPdfRenderPort; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.File; | ||
| import java.io.InputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class PdfRenderer implements AiReportPdfRenderPort { | ||
|
|
||
| private static final String FONT_FAMILY = "PdfReportFont"; | ||
| private static final List<String> CLASSPATH_FONTS = List.of( | ||
| "fonts/NotoSansKR-Regular.ttf" | ||
| ); | ||
| private static final List<Path> SYSTEM_FONTS = List.of( | ||
| Path.of("C:\\Windows\\Fonts\\malgun.ttf"), | ||
| Path.of("/usr/share/fonts/truetype/nanum/NanumGothic.ttf"), | ||
| Path.of("/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc"), | ||
| Path.of("/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc") | ||
| ); | ||
|
|
||
| private final SpringTemplateEngine templateEngine; | ||
| private final AiReportPdfViewMapper aiReportPdfViewMapper; | ||
|
|
||
| @Override | ||
| public byte[] render(AiReportResult report) { | ||
| try { | ||
| AiReportPdfView view = aiReportPdfViewMapper.toView(report); | ||
| Context context = new Context(); | ||
| context.setVariable("totalScore", view.totalScore()); | ||
| context.setVariable("sections", view.sections()); | ||
| context.setVariable("strengths", view.strengths()); | ||
| context.setVariable("weaknesses", view.weaknesses()); | ||
| context.setVariable("radarChartSvg", view.radarChartSvg()); | ||
|
|
||
| String html = templateEngine.process("pdf-ai-report", context); | ||
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| PdfRendererBuilder builder = new PdfRendererBuilder(); | ||
| builder.useFastMode(); | ||
| builder.useSVGDrawer(new BatikSVGDrawer()); | ||
| configureFont(builder); | ||
| builder.withHtmlContent(html, null); | ||
| builder.toStream(out); | ||
| builder.run(); | ||
| return out.toByteArray(); | ||
| } catch (Exception e) { | ||
| log.error("[AI_REPORT_PDF] html to pdf render failed", e); | ||
| throw new IllegalStateException("AI 리포트 PDF 생성에 실패했습니다.", e); | ||
| } | ||
| } | ||
|
|
||
| private void configureFont(PdfRendererBuilder builder) { | ||
| for (String classpathFont : CLASSPATH_FONTS) { | ||
| if (registerClasspathFont(builder, classpathFont)) { | ||
| return; | ||
| } | ||
| } | ||
| for (Path systemFont : SYSTEM_FONTS) { | ||
| if (registerFileFont(builder, systemFont)) { | ||
| log.info("[AI_REPORT_PDF] using system font: {}", systemFont); | ||
| return; | ||
| } | ||
| } | ||
| throw new IllegalStateException( | ||
| "한글 PDF 폰트를 찾을 수 없습니다. classpath(" + CLASSPATH_FONTS + ") 또는 OS 폰트를 확인하세요." | ||
| ); | ||
| } | ||
|
|
||
| private boolean registerClasspathFont(PdfRendererBuilder builder, String classpathLocation) { | ||
| try { | ||
| ClassPathResource resource = new ClassPathResource(classpathLocation); | ||
| if (!resource.exists()) { | ||
| return false; | ||
| } | ||
| File fontFile = copyFontToTemp(resource); | ||
| if (!isPdfBoxLoadableTrueType(fontFile.toPath())) { | ||
| log.warn("[AI_REPORT_PDF] skipping unsupported classpath font: {}", classpathLocation); | ||
| return false; | ||
| } | ||
| builder.useFont(fontFile, FONT_FAMILY, 400, BaseRendererBuilder.FontStyle.NORMAL, true); | ||
| return true; | ||
| } catch (Exception e) { | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| log.warn("[AI_REPORT_PDF] failed to load classpath font: {}", classpathLocation, e); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private boolean registerFileFont(PdfRendererBuilder builder, Path fontPath) { | ||
| if (!Files.isRegularFile(fontPath) || !isPdfBoxLoadableTrueType(fontPath)) { | ||
| return false; | ||
| } | ||
| try { | ||
| builder.useFont(fontPath.toFile(), FONT_FAMILY, 400, BaseRendererBuilder.FontStyle.NORMAL, true); | ||
| return true; | ||
| } catch (Exception e) { | ||
| log.warn("[AI_REPORT_PDF] failed to load system font: {}", fontPath, e); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private boolean isPdfBoxLoadableTrueType(Path fontPath) { | ||
| try { | ||
| if (!Files.isRegularFile(fontPath)) { | ||
| return false; | ||
| } | ||
| byte[] header = Files.readAllBytes(fontPath); | ||
| if (header.length < 4) { | ||
| return false; | ||
| } | ||
| if (header[0] == 'O' && header[1] == 'T' && header[2] == 'T' && header[3] == 'O') { | ||
| return false; | ||
| } | ||
| try (PDDocument document = new PDDocument()) { | ||
| PDFont font = PDType0Font.load(document, fontPath.toFile()); | ||
| return font != null; | ||
| } | ||
| } catch (Exception e) { | ||
| log.debug("[AI_REPORT_PDF] font not loadable: {}", fontPath, e); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private File copyFontToTemp(ClassPathResource resource) throws Exception { | ||
| String suffix = resource.getFilename() != null && resource.getFilename().endsWith(".ttf") | ||
| ? ".ttf" | ||
| : ".font"; | ||
| File temp = Files.createTempFile("pdf-report-font-", suffix).toFile(); | ||
| temp.deleteOnExit(); | ||
| try (InputStream in = resource.getInputStream()) { | ||
| Files.copy(in, temp.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); | ||
| } | ||
| return temp; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.