| | |
| | | import com.estate.expert.admin.stat.report.data.impl.SystemFuzzyReportData; |
| | | import com.estate.expert.admin.stat.report.data.impl.SystemScoreReportData; |
| | | import com.estate.expert.common.exception.BizException; |
| | | import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter; |
| | | import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.OutputStream; |
| | | |
| | | /** |
| | | * 报告数据服务实现类 |
| | |
| | | return builder.buildReport(reportData, template.getConfig()); |
| | | } |
| | | |
| | | @Override |
| | | public void fuzzyReportPdf(Integer checkTaskId, Integer templateId, OutputStream outputStream) { |
| | | try (var doc = fuzzyReport(checkTaskId, templateId)) { |
| | | convertToPdf(doc, outputStream); |
| | | } catch (Exception e) { |
| | | log.error("生成模糊评价PDF报告失败", e); |
| | | throw new BizException("生成模糊评价PDF报告失败"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void scoreReportPdf(Integer checkTaskId, Integer templateId, OutputStream outputStream) { |
| | | try (var doc = scoreReport(checkTaskId, templateId)) { |
| | | convertToPdf(doc, outputStream); |
| | | } catch (Exception e) { |
| | | log.error("生成打分评价PDF报告失败", e); |
| | | throw new BizException("生成打分评价PDF报告失败"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将Word文档转换为PDF |
| | | * @param template Word模板 |
| | | * @param outputStream 输出流 |
| | | */ |
| | | private void convertToPdf(XWPFTemplate template, OutputStream outputStream) throws Exception { |
| | | // 先将Word模板写入字节数组 |
| | | ByteArrayOutputStream docOutputStream = new ByteArrayOutputStream(); |
| | | template.write(docOutputStream); |
| | | |
| | | // 从字节数组读取Word文档 |
| | | ByteArrayInputStream docInputStream = new ByteArrayInputStream(docOutputStream.toByteArray()); |
| | | org.apache.poi.xwpf.usermodel.XWPFDocument document = new org.apache.poi.xwpf.usermodel.XWPFDocument(docInputStream); |
| | | |
| | | // 转换为PDF |
| | | PdfOptions options = PdfOptions.create(); |
| | | PdfConverter.getInstance().convert(document, outputStream, options); |
| | | |
| | | // 关闭资源 |
| | | document.close(); |
| | | docInputStream.close(); |
| | | docOutputStream.close(); |
| | | } |
| | | |
| | | private ReportBuilderBase getReportBuilder(Integer templateId) { |
| | | var reportTemplate = reportTemplateService.get(templateId); |
| | | BizResultCode.BIZ_REPORT_TEMPLATE_NOT_EXIST.check(reportTemplate == null); |