Pdf Utill section이 없는 경우 추가
This commit is contained in:
parent
3b24d39355
commit
9ee29819dd
@ -793,17 +793,18 @@ public class EstimateService {
|
||||
|
||||
estimateResponse.setPwrGnrSim(pwrGnrSimResponse);
|
||||
|
||||
if (true) {
|
||||
if (true) { // PDF 다운로드
|
||||
String[] arrSection = new String[3]; // TODO Section 갯수 넣기
|
||||
int iSection = 0;
|
||||
|
||||
// PDF 다운로드
|
||||
String pdfFileName =
|
||||
"Quation_Detail_" + new SimpleDateFormat("yyyyMMdd").format(new Date());
|
||||
String templateFilePath = "pdf_download_quotation_detail_template.html";
|
||||
|
||||
// 템플릿 html 조회
|
||||
Document doc = PdfUtil.getPdfDoc(request, templateFilePath);
|
||||
|
||||
// 삭제하려는 element
|
||||
Element elm;
|
||||
|
||||
// 발전시뮬레이션 pdf Html 생성
|
||||
|
||||
@ -78,68 +78,57 @@ public class PdfUtil {
|
||||
PdfWriter writer = new PdfWriter(os);
|
||||
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";
|
||||
InputStream fontStream = PdfUtil.class.getClassLoader().getResourceAsStream(fontPath);
|
||||
properties.setFontProvider(setFontProvider(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();
|
||||
// arrSection이 비어 있는 경우 HTML 전체를 변환
|
||||
if (arrSection == null || arrSection.length == 0) {
|
||||
// HTML 전체를 pdfDoc에 직접 변환
|
||||
HtmlConverter.convertToPdf(
|
||||
new ByteArrayInputStream(doc.html().getBytes(StandardCharsets.UTF_8)), os, properties);
|
||||
os.flush();
|
||||
} else {
|
||||
log.error("폰트 파일을 찾을 수 없습니다: " + fontPath);
|
||||
}
|
||||
properties.setFontProvider(fontProvider);
|
||||
// arrSection이 있는 경우 각 섹션을 개별적으로 변환 후 병합
|
||||
String headHtml = doc.select("head").outerHtml();
|
||||
|
||||
// 원본 HTML에서 <head> 내용을 추출
|
||||
String headHtml = doc.select("head").outerHtml(); // <head>의 전체 내용을 가져옵니다.
|
||||
PdfMerger merger = new PdfMerger(pdfDoc);
|
||||
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 생성
|
||||
// arrSection의 각 섹션을 PDF에 추가
|
||||
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 =
|
||||
"<html>" + headHtml + "<body>" + eSection.outerHtml() + "</body></html>";
|
||||
|
||||
String sectionHtml =
|
||||
"<html>" + headHtml + "<body>" + eSection.outerHtml() + "</body></html>";
|
||||
try (ByteArrayOutputStream tempOutputStream = new ByteArrayOutputStream();
|
||||
PdfWriter tempWriter = new PdfWriter(tempOutputStream);
|
||||
PdfDocument tempPdfDoc = new PdfDocument(tempWriter)) {
|
||||
|
||||
// 각 섹션을 임시 PDF 파일에 변환
|
||||
try (ByteArrayOutputStream tempOutputStream = new ByteArrayOutputStream();
|
||||
PdfWriter tempWriter = new PdfWriter(tempOutputStream);
|
||||
PdfDocument tempPdfDoc = new PdfDocument(tempWriter)) {
|
||||
HtmlConverter.convertToPdf(
|
||||
new ByteArrayInputStream(sectionHtml.getBytes(StandardCharsets.UTF_8)),
|
||||
tempPdfDoc,
|
||||
properties);
|
||||
|
||||
HtmlConverter.convertToPdf(
|
||||
new ByteArrayInputStream(sectionHtml.getBytes(StandardCharsets.UTF_8)),
|
||||
tempPdfDoc,
|
||||
properties);
|
||||
PdfDocument tempDoc =
|
||||
new PdfDocument(
|
||||
new PdfReader(new ByteArrayInputStream(tempOutputStream.toByteArray())));
|
||||
merger.merge(tempDoc, 1, tempDoc.getNumberOfPages());
|
||||
tempDoc.close();
|
||||
|
||||
// tempPdfDoc을 메인 pdfDoc에 병합
|
||||
PdfDocument tempDoc =
|
||||
new PdfDocument(
|
||||
new PdfReader(new ByteArrayInputStream(tempOutputStream.toByteArray())));
|
||||
merger.merge(tempDoc, 1, tempDoc.getNumberOfPages());
|
||||
tempDoc.close();
|
||||
|
||||
// 마지막 섹션이 아닐 경우 새 페이지 추가
|
||||
if (!eSection.equals(eSections.last())) {
|
||||
pdfDoc.addNewPage();
|
||||
if (!eSection.equals(eSections.last())) {
|
||||
pdfDoc.addNewPage();
|
||||
}
|
||||
}
|
||||
}
|
||||
} // for
|
||||
} // if
|
||||
} // if
|
||||
}
|
||||
}
|
||||
}
|
||||
pdfDoc.close();
|
||||
os.flush();
|
||||
}
|
||||
pdfDoc.close(); // 전체 PDF 문서 작성이 끝난 후에 한번만 닫기
|
||||
os.flush();
|
||||
} catch (Exception e) {
|
||||
log.error("PDF 생성 중 오류 발생: " + e.getMessage(), e);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user