Pdf Utill section이 없는 경우 추가

This commit is contained in:
DESKTOP-6ARNG1Q\dlsgk 2024-11-14 11:41:39 +09:00
parent 3b24d39355
commit 9ee29819dd
2 changed files with 64 additions and 51 deletions

View File

@ -793,17 +793,18 @@ public class EstimateService {
estimateResponse.setPwrGnrSim(pwrGnrSimResponse); estimateResponse.setPwrGnrSim(pwrGnrSimResponse);
if (true) { if (true) { // PDF 다운로드
String[] arrSection = new String[3]; // TODO Section 갯수 넣기 String[] arrSection = new String[3]; // TODO Section 갯수 넣기
int iSection = 0; int iSection = 0;
// PDF 다운로드
String pdfFileName = String pdfFileName =
"Quation_Detail_" + new SimpleDateFormat("yyyyMMdd").format(new Date()); "Quation_Detail_" + new SimpleDateFormat("yyyyMMdd").format(new Date());
String templateFilePath = "pdf_download_quotation_detail_template.html"; String templateFilePath = "pdf_download_quotation_detail_template.html";
// 템플릿 html 조회 // 템플릿 html 조회
Document doc = PdfUtil.getPdfDoc(request, templateFilePath); Document doc = PdfUtil.getPdfDoc(request, templateFilePath);
// 삭제하려는 element
Element elm; Element elm;
// 발전시뮬레이션 pdf Html 생성 // 발전시뮬레이션 pdf Html 생성

View File

@ -78,68 +78,57 @@ public class PdfUtil {
PdfWriter writer = new PdfWriter(os); PdfWriter writer = new PdfWriter(os);
PdfDocument pdfDoc = new PdfDocument(writer)) { PdfDocument pdfDoc = new PdfDocument(writer)) {
// ConverterProperties 생성
ConverterProperties properties = new ConverterProperties();
// 폰트 설정 // 폰트 설정
DefaultFontProvider fontProvider = new DefaultFontProvider(false, false, false); ConverterProperties properties = new ConverterProperties();
String fontPath = "template/pdf/BIZUDPGothic-Regular.ttf"; String fontPath = "template/pdf/BIZUDPGothic-Regular.ttf";
InputStream fontStream = PdfUtil.class.getClassLoader().getResourceAsStream(fontPath); properties.setFontProvider(setFontProvider(fontPath));
if (fontStream != null) { // arrSection이 비어 있는 경우 HTML 전체를 변환
File tempFontFile = Files.createTempFile("BIZUDPGothic-Regular", ".ttf").toFile(); if (arrSection == null || arrSection.length == 0) {
Files.copy( // HTML 전체를 pdfDoc에 직접 변환
fontStream, tempFontFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); HtmlConverter.convertToPdf(
fontProvider.addFont(tempFontFile.getAbsolutePath()); new ByteArrayInputStream(doc.html().getBytes(StandardCharsets.UTF_8)), os, properties);
fontStream.close(); os.flush();
tempFontFile.deleteOnExit();
} else { } else {
log.error("폰트 파일을 찾을 수 없습니다: " + fontPath); // arrSection이 있는 경우 섹션을 개별적으로 변환 병합
} String headHtml = doc.select("head").outerHtml();
properties.setFontProvider(fontProvider);
// 원본 HTML에서 <head> 내용을 추출 PdfMerger merger = new PdfMerger(pdfDoc);
String headHtml = doc.select("head").outerHtml(); // <head> 전체 내용을 가져옵니다. for (String section : arrSection) {
if (section != null && !"".equals(section)) {
Elements eSections = doc.select(section);
if (eSections != null && eSections.size() > 0) {
for (Element eSection : eSections) {
PdfMerger merger = new PdfMerger(pdfDoc); // 최종 문서에 병합할 PdfMerger 생성 String sectionHtml =
// arrSection의 섹션을 PDF에 추가 "<html>" + headHtml + "<body>" + eSection.outerHtml() + "</body></html>";
for (String section : arrSection) {
if (section != null && !"".equals(section)) {
Elements eSections = doc.select(section); // 섹션 선택
if (eSections != null && eSections.size() > 0) {
for (Element eSection : eSections) {
String sectionHtml = try (ByteArrayOutputStream tempOutputStream = new ByteArrayOutputStream();
"<html>" + headHtml + "<body>" + eSection.outerHtml() + "</body></html>"; PdfWriter tempWriter = new PdfWriter(tempOutputStream);
PdfDocument tempPdfDoc = new PdfDocument(tempWriter)) {
// 섹션을 임시 PDF 파일에 변환 HtmlConverter.convertToPdf(
try (ByteArrayOutputStream tempOutputStream = new ByteArrayOutputStream(); new ByteArrayInputStream(sectionHtml.getBytes(StandardCharsets.UTF_8)),
PdfWriter tempWriter = new PdfWriter(tempOutputStream); tempPdfDoc,
PdfDocument tempPdfDoc = new PdfDocument(tempWriter)) { properties);
HtmlConverter.convertToPdf( PdfDocument tempDoc =
new ByteArrayInputStream(sectionHtml.getBytes(StandardCharsets.UTF_8)), new PdfDocument(
tempPdfDoc, new PdfReader(new ByteArrayInputStream(tempOutputStream.toByteArray())));
properties); merger.merge(tempDoc, 1, tempDoc.getNumberOfPages());
tempDoc.close();
// tempPdfDoc을 메인 pdfDoc에 병합 if (!eSection.equals(eSections.last())) {
PdfDocument tempDoc = pdfDoc.addNewPage();
new PdfDocument( }
new PdfReader(new ByteArrayInputStream(tempOutputStream.toByteArray())));
merger.merge(tempDoc, 1, tempDoc.getNumberOfPages());
tempDoc.close();
// 마지막 섹션이 아닐 경우 페이지 추가
if (!eSection.equals(eSections.last())) {
pdfDoc.addNewPage();
} }
} }
} // for }
} // if }
} // if }
pdfDoc.close();
os.flush();
} }
pdfDoc.close(); // 전체 PDF 문서 작성이 끝난 후에 한번만 닫기
os.flush();
} catch (Exception e) { } catch (Exception e) {
log.error("PDF 생성 중 오류 발생: " + e.getMessage(), e); log.error("PDF 생성 중 오류 발생: " + e.getMessage(), e);
if (!response.isCommitted()) { if (!response.isCommitted()) {
@ -148,4 +137,27 @@ public class PdfUtil {
} }
} }
} }
/**
* 폰트 설정 메서드
*
* @param fontPath 폰트 파일 경로
* @return 설정된 FontProvider
*/
private static DefaultFontProvider setFontProvider(String fontPath) throws IOException {
DefaultFontProvider fontProvider = new DefaultFontProvider(false, false, false);
InputStream fontStream = PdfUtil.class.getClassLoader().getResourceAsStream(fontPath);
if (fontStream != null) {
File tempFontFile = Files.createTempFile("BIZUDPGothic-Regular", ".ttf").toFile();
Files.copy(
fontStream, tempFontFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
fontProvider.addFont(tempFontFile.getAbsolutePath());
fontStream.close();
tempFontFile.deleteOnExit();
} else {
log.error("폰트 파일을 찾을 수 없습니다: " + fontPath);
}
return fontProvider;
}
} }