|
|
@@ -86,13 +86,64 @@ public class PrintServiceImpl implements PrintService {
|
|
|
return print(userName, reportName, whereCondition, otherParameters, null, pageIndex);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Integer previewFirstPage(final String userName, final String reportName, final String whereCondition,
|
|
|
+ final String otherParameters, final String pdfFilePath) {
|
|
|
+ // 先生成第一页pdf
|
|
|
+ final Integer pageSize = writePdfData(userName, reportName, whereCondition, otherParameters,
|
|
|
+ pdfFilePath.replace(".pdf", "_1.pdf"), 1);
|
|
|
+
|
|
|
+ // 再开线程生成后面页的pdf和总的pdf(即未分页的pdf)
|
|
|
+ new Thread(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ // 生成之后页的pdf
|
|
|
+ for (int i = 2; i <= pageSize; i++) {
|
|
|
+ writePdfData(userName, reportName, whereCondition, otherParameters,
|
|
|
+ pdfFilePath.replace(".pdf", "_" + i + ".pdf"), i);
|
|
|
+ }
|
|
|
+ // 生成总的pdf
|
|
|
+ writePdfData(userName, reportName, whereCondition, otherParameters, pdfFilePath, null);
|
|
|
+ }
|
|
|
+ }).start();
|
|
|
+
|
|
|
+ return pageSize;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取第pageIndex页的pdf数据并写入pdfFilePath路径下
|
|
|
+ *
|
|
|
+ * @param userName
|
|
|
+ * @param reportName
|
|
|
+ * @param whereCondition
|
|
|
+ * @param otherParameters
|
|
|
+ * @param pdfFilePath
|
|
|
+ * @param pageIndex
|
|
|
+ * @return 总页数
|
|
|
+ */
|
|
|
+ public Integer writePdfData(String userName, String reportName, String whereCondition, String otherParameters,
|
|
|
+ String pdfFilePath, Integer pageIndex) {
|
|
|
+ File file = new File(pdfFilePath);
|
|
|
+ Map<String, Object> result = preview(userName, reportName, whereCondition, otherParameters, pageIndex);
|
|
|
+ byte[] data = null;
|
|
|
+ Integer pageSize = null;
|
|
|
+ if (result != null && result.containsKey("data") && result.containsKey("pageSize")) {
|
|
|
+ data = (byte[]) result.remove("data");
|
|
|
+ pageSize = (Integer) result.remove("pageSize");
|
|
|
+ }
|
|
|
+ if (data == null || pageSize == null) {
|
|
|
+ throw new SystemError("获取预览数据失败");
|
|
|
+ }
|
|
|
+ writeDataToFile(file.getPath(), data);
|
|
|
+ return pageSize;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void writeDataToFile(String filePath, byte[] data) {
|
|
|
File file = new File(filePath);
|
|
|
if (!file.getParentFile().exists()) {
|
|
|
file.getParentFile().mkdirs();
|
|
|
}
|
|
|
-
|
|
|
try {
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
|
fos.write(data);
|
|
|
@@ -100,8 +151,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
logger.info("Write file..." + file.getPath());
|
|
|
fos.close();
|
|
|
} catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new SystemError(e.getMessage());
|
|
|
+ throw new SystemError(e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -123,8 +173,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
}
|
|
|
pdfReader.close();
|
|
|
} catch (IOException | DocumentException e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new SystemError(e.getMessage());
|
|
|
+ throw new SystemError(e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -133,8 +182,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
try {
|
|
|
return new PdfReader(pdfFilePath).getNumberOfPages();
|
|
|
} catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new SystemError(e.getMessage());
|
|
|
+ throw new SystemError(e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -186,24 +234,19 @@ public class PrintServiceImpl implements PrintService {
|
|
|
String exportFileType, Integer pageIndex) {
|
|
|
try {
|
|
|
resourceService.syncResources(userName);
|
|
|
- } catch (URISyntaxException | IOException e1) {
|
|
|
- e1.printStackTrace();
|
|
|
- throw new SystemError(e1.getMessage());
|
|
|
+ } catch (URISyntaxException | IOException e) {
|
|
|
+ throw new SystemError(e);
|
|
|
}
|
|
|
|
|
|
// 报表路径为报表根路径REPORT_DIR + 当前账套用户名userName
|
|
|
- String reportDir = new StringBuilder(jsRestAPIConf.getLocalBaseDir()).append("/").append(userName).append("/")
|
|
|
- .toString();
|
|
|
-
|
|
|
- String jrxmlFilePath = new StringBuilder(reportDir).append("jrxml").append("/").append(reportName)
|
|
|
+ String reportDir = new StringBuilder(jsRestAPIConf.getLocalBaseDir()).append("/").append(userName).toString();
|
|
|
+ String jrxmlFilePath = new StringBuilder(reportDir).append("/").append("jrxml").append("/").append(reportName)
|
|
|
.append(".jrxml").toString();
|
|
|
File jrxmlFile = new File(jrxmlFilePath);
|
|
|
- String message = "";
|
|
|
// 报表模板不存在
|
|
|
if (!jrxmlFile.exists()) {
|
|
|
- message = "未发现模板文件:" + jrxmlFile.getPath();
|
|
|
- logger.error(message);
|
|
|
- throw new SystemError(message);
|
|
|
+ // 替换windows下路径中的双反斜杠为单斜杠
|
|
|
+ throw new SystemError("未发现模板文件:" + jrxmlFile.getPath().replaceAll("\\\\", "/"));
|
|
|
}
|
|
|
|
|
|
String jasperFilePath = jrxmlFile.getPath().replace(".jrxml", ".jasper");
|
|
|
@@ -245,9 +288,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
// 获取数据源
|
|
|
DataSource dataSource = getDataSource(userName);
|
|
|
if (dataSource == null) {
|
|
|
- message = "获取数据源失败";
|
|
|
- logger.error(message);
|
|
|
- throw new SystemError(message);
|
|
|
+ throw new SystemError("获取数据源失败");
|
|
|
}
|
|
|
|
|
|
connection = dataSource.getConnection();
|
|
|
@@ -262,7 +303,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
// 只导出数据
|
|
|
if (exportFileType.equals("xls_with_only_data")) {
|
|
|
JasperDesign jasperDesign = JRXmlLoader.load(jrxmlFile);
|
|
|
- // 移除多余元素
|
|
|
+ // 移除模板中多余元素
|
|
|
removeUnusedElements(jasperDesign);
|
|
|
exportFileType = "xls";
|
|
|
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
|
|
|
@@ -272,25 +313,27 @@ public class PrintServiceImpl implements PrintService {
|
|
|
jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
|
|
|
}
|
|
|
|
|
|
- boolean exportSucceeded = exportReport(jasperPrint, exportFileType, outputStream);
|
|
|
- if (exportSucceeded) {
|
|
|
- byte[] data = outputStream.toByteArray();
|
|
|
- outputStream.close();
|
|
|
- result.put("data", data);
|
|
|
- return result;
|
|
|
- }
|
|
|
+ exportReport(jasperPrint, exportFileType, outputStream);
|
|
|
+ byte[] data = outputStream.toByteArray();
|
|
|
+ outputStream.close();
|
|
|
+ result.put("data", data);
|
|
|
+ return result;
|
|
|
}
|
|
|
// 报表预览,则直接输出pdf,并且需要进行分页
|
|
|
- else if (pageIndex != null) {
|
|
|
- // 页码并非有效数值,重置为第一页
|
|
|
+ else {
|
|
|
jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, connection);
|
|
|
- if (pageIndex < 0 || pageIndex >= jasperPrint.getPages().size()) {
|
|
|
- pageIndex = 0;
|
|
|
- }
|
|
|
JRPdfExporter exporter = new JRPdfExporter();
|
|
|
- SimplePdfReportConfiguration configuration = new SimplePdfReportConfiguration();
|
|
|
- configuration.setPageIndex(pageIndex);
|
|
|
- // exporter.setConfiguration(configuration);
|
|
|
+ if (pageIndex != null) {
|
|
|
+ // 前端显示时页码从1开始,生成报表时页码从0开始
|
|
|
+ pageIndex -= 1;
|
|
|
+ // 页码并非有效数值,重置为第一页
|
|
|
+ if (pageIndex < 0 || pageIndex >= jasperPrint.getPages().size()) {
|
|
|
+ pageIndex = 0;
|
|
|
+ }
|
|
|
+ SimplePdfReportConfiguration configuration = new SimplePdfReportConfiguration();
|
|
|
+ configuration.setPageIndex(pageIndex);
|
|
|
+ exporter.setConfiguration(configuration);
|
|
|
+ }
|
|
|
ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
|
|
|
exporter.setExporterInput(exporterInput);
|
|
|
OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(outputStream);
|
|
|
@@ -302,32 +345,18 @@ public class PrintServiceImpl implements PrintService {
|
|
|
result.put("data", data);
|
|
|
result.put("pageSize", jasperPrint.getPages().size());
|
|
|
return result;
|
|
|
- } else {
|
|
|
- message = "exportFileType 和 pageIndex 不能同时为空!";
|
|
|
- logger.error(message);
|
|
|
- throw new SystemError(message);
|
|
|
}
|
|
|
- } catch (SQLException e) {
|
|
|
- message = "数据库连接错误!";
|
|
|
- logger.error(message);
|
|
|
- throw new SystemError(message);
|
|
|
- } catch (JRException e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new SystemError(e.getMessage());
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new SystemError(e.getMessage());
|
|
|
+ } catch (SQLException | JRException | IOException e) {
|
|
|
+ throw new SystemError(e);
|
|
|
} finally {
|
|
|
if (connection != null) {
|
|
|
try {
|
|
|
connection.close();
|
|
|
} catch (SQLException e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new SystemError(e.getMessage());
|
|
|
+ throw new SystemError(e);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -372,12 +401,8 @@ public class PrintServiceImpl implements PrintService {
|
|
|
return DruidDataSourceFactory.createDataSource(properties);
|
|
|
}
|
|
|
}
|
|
|
- } catch (SQLException e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new SystemError(e.getMessage());
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new SystemError(e.getMessage());
|
|
|
+ throw new SystemError(e);
|
|
|
} finally {
|
|
|
try {
|
|
|
if (resultSet != null) {
|
|
|
@@ -385,7 +410,6 @@ public class PrintServiceImpl implements PrintService {
|
|
|
}
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
- throw new SystemError(e.getMessage());
|
|
|
}
|
|
|
try {
|
|
|
if (preparedStatement != null) {
|
|
|
@@ -393,13 +417,13 @@ public class PrintServiceImpl implements PrintService {
|
|
|
}
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
- throw new SystemError(e.getMessage());
|
|
|
}
|
|
|
try {
|
|
|
- connection.close();
|
|
|
+ if (connection != null) {
|
|
|
+ connection.close();
|
|
|
+ }
|
|
|
} catch (SQLException e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new SystemError(e.getMessage());
|
|
|
+ throw new SystemError(e);
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
@@ -411,24 +435,19 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* @param jasperPrint
|
|
|
* @param exportFileType
|
|
|
* @param outputStream
|
|
|
- * @return 报表是否成功导出
|
|
|
*/
|
|
|
- private boolean exportReport(JasperPrint jasperPrint, String exportFileType, OutputStream outputStream) {
|
|
|
+ private void exportReport(JasperPrint jasperPrint, String exportFileType, OutputStream outputStream) {
|
|
|
try {
|
|
|
if (exportFileType.equals("pdf")) {
|
|
|
exportReportToPdf(jasperPrint, outputStream);
|
|
|
} else if (exportFileType.equals("xls")) {
|
|
|
exportReportToXls(jasperPrint, outputStream);
|
|
|
} else {
|
|
|
- logger.error("不支持导出为 " + exportFileType + "格式!");
|
|
|
- return false;
|
|
|
+ throw new SystemError("不支持导出为 " + exportFileType + "格式!");
|
|
|
}
|
|
|
} catch (JRException e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new SystemError(e.getMessage());
|
|
|
- // return false;
|
|
|
+ throw new SystemError(e);
|
|
|
}
|
|
|
- return true;
|
|
|
}
|
|
|
|
|
|
/**
|