Explorar el Código

预览时不再先产生分页的pdf,再后台新开线程生成总的pdf和excel,而是直接生成总的pdf,excel在下载时才生成(浏览器兼容、状态判断等问题太多,预览速度的提升与预览的稳定性的降低相比得不偿失)

sunyj hace 9 años
padre
commit
c9018a2924

+ 14 - 26
src/main/java/com/uas/report/controller/PrintController.java

@@ -211,33 +211,21 @@ public class PrintController {
 		if (!fileService.isFileValid(
 		if (!fileService.isFileValid(
 				pageIndex == null ? file.getPath() : file.getPath().replace(".pdf", "_" + pageIndex + ".pdf"),
 				pageIndex == null ? file.getPath() : file.getPath().replace(".pdf", "_" + pageIndex + ".pdf"),
 				fileService.getJrxmlFilePath(userName, reportName))) {
 				fileService.getJrxmlFilePath(userName, reportName))) {
-			// 若参数pageIndex不为null,表示是预览并且预览第一页
-			// 检查第一页的pdf文件是否存在,若不存在,先生成第一页pdf,返回给前台展示,
-			// 再开线程生成后面页的pdf和总的pdf(即未分页的pdf),以备后续可能的打印、下载操作使用
-			if (pageIndex != null && pageIndex == 1) {
-				Integer pageSize = printService.previewFirstPage(userName, profile, reportName, whereCondition,
-						otherParameters, file.getPath());
-				result = new HashMap<>();
-				result.put("pageSize", pageSize);
+			// 参数pageIndex为null或1,表示是直接打印或预览第一页(需要先生成总的pdf)
+			result = printService.preview(userName, profile, reportName, whereCondition, otherParameters, pageIndex);
+			byte[] data = null;
+			if (result != null && result.containsKey("data")) {
+				data = (byte[]) result.remove("data");
 			}
 			}
-			// 参数pageIndex为null或不为1,表示是直接打印(需要先生成总的pdf)或预览非第一页
-			else {
-				result = printService.preview(userName, profile, reportName, whereCondition, otherParameters,
-						pageIndex);
-				byte[] data = null;
-				if (result != null && result.containsKey("data")) {
-					data = (byte[]) result.remove("data");
-				}
-				if (ArrayUtils.isEmpty(data)) {
-					throw new ReportException("获取预览数据失败");
-				}
-				if (pageIndex == null) {
-					FileUtils.create(file.getPath(), data);
-					// 同时生成分页的pdf
-					fileService.createPagedPdfFiles(file.getPath());
-				} else {
-					FileUtils.create(file.getPath().replace(".pdf", "_" + pageIndex + ".pdf"), data);
-				}
+			if (ArrayUtils.isEmpty(data)) {
+				throw new ReportException("获取预览数据失败");
+			}
+			if (pageIndex == null || pageIndex == 1) {
+				FileUtils.create(file.getPath(), data);
+				// 同时生成分页的pdf
+				fileService.createPagedPdfFiles(file.getPath());
+			} else {
+				FileUtils.create(file.getPath().replace(".pdf", "_" + pageIndex + ".pdf"), data);
 			}
 			}
 		} else {
 		} else {
 			result = new HashMap<>();
 			result = new HashMap<>();

+ 0 - 23
src/main/java/com/uas/report/service/PrintService.java

@@ -52,27 +52,4 @@ public interface PrintService {
 	public Map<String, Object> preview(String userName, String profile, String reportName, String whereCondition,
 	public Map<String, Object> preview(String userName, String profile, String reportName, String whereCondition,
 			String otherParameters, Integer pageIndex);
 			String otherParameters, Integer pageIndex);
 
 
-	/**
-	 * 先生成第一页pdf,返回给前台展示,以提高预览速度;
-	 * 
-	 * 再开线程生成后面页的pdf、总的pdf(即未分页的pdf)、纯数据excel,以备后续可能的打印、下载操作使用
-	 * 
-	 * @param userName
-	 *            不为null;当前账套用户名
-	 * @param profile
-	 *            可选(UAS系统不必传递该参数),用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
-	 * @param reportName
-	 *            不为null;需要预览的报表的名称,不带任何后缀(如预览采购单,即为"Purchase")
-	 * @param whereCondition
-	 *            可为null;where之后的条件(包括where)
-	 * @param otherParameters
-	 *            若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
-	 *            JSON格式,数据为键值对
-	 * @param pdfFilePath
-	 *            该报表对应的pdf绝对路径
-	 * @return 总页数
-	 */
-	public Integer previewFirstPage(String userName, String profile, String reportName, String whereCondition,
-			String otherParameters, String pdfFilePath);
-
 }
 }

+ 0 - 76
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -11,7 +11,6 @@ import java.util.Map;
 
 
 import javax.sql.DataSource;
 import javax.sql.DataSource;
 
 
-import org.apache.commons.lang.ArrayUtils;
 import org.apache.log4j.Logger;
 import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
@@ -23,7 +22,6 @@ import com.uas.report.core.exception.ReportException;
 import com.uas.report.service.FileService;
 import com.uas.report.service.FileService;
 import com.uas.report.service.PrintService;
 import com.uas.report.service.PrintService;
 import com.uas.report.util.DataSourceUtils;
 import com.uas.report.util.DataSourceUtils;
-import com.uas.report.util.FileUtils;
 import com.uas.report.util.ReportConstants;
 import com.uas.report.util.ReportConstants;
 
 
 import net.sf.jasperreports.engine.JRException;
 import net.sf.jasperreports.engine.JRException;
@@ -96,80 +94,6 @@ public class PrintServiceImpl implements PrintService {
 		return print(userName, reportName, whereCondition, otherParameters, null, pageIndex, dataSource);
 		return print(userName, reportName, whereCondition, otherParameters, null, pageIndex, dataSource);
 	}
 	}
 
 
-	@Override
-	public Integer previewFirstPage(final String userName, final String profile, final String reportName,
-			final String whereCondition, final String otherParameters, final String pdfFilePath) {
-		// 先生成第一页pdf
-		final Integer pageSize = createPdfFile(userName, profile, reportName, whereCondition, otherParameters,
-				pdfFilePath.replace(".pdf", "_1.pdf"), 1);
-
-		// 再开线程生成后面页的pdf、总的pdf(即未分页的pdf)、纯数据excel
-		new Thread(new Runnable() {
-			@Override
-			public void run() {
-				// 生成之后2~3页的pdf,防止数据量较大的情况下,卡在生成总的pdf阶段,此时查看前3页也不会卡顿
-				// for (int i = 2; i <= Math.min(pageSize, 3); i++) {
-				// createPdfFile(userName, profile, reportName, whereCondition,
-				// otherParameters,
-				// pdfFilePath.replace(".pdf", "_" + i + ".pdf"), i);
-				// }
-				// 生成总的pdf
-				createPdfFile(userName, profile, reportName, whereCondition, otherParameters, pdfFilePath, null);
-				fileService.createPagedPdfFiles(pdfFilePath);
-				// 生成纯数据excel
-				File file = new File(pdfFilePath.replace(".pdf", ".xls"));
-				// 文件无效(不存在或过期),创建
-				if (!fileService.isFileValid(file.getPath(), fileService.getJrxmlFilePath(userName, reportName))) {
-					byte[] data = export(userName, profile, reportName, whereCondition, otherParameters,
-							"xls_with_only_data");
-					if (ArrayUtils.isEmpty(data)) {
-						throw new ReportException("报表导出失败:" + reportName);
-					}
-					FileUtils.create(file.getPath(), data);
-				}
-			}
-		}).start();
-
-		return pageSize;
-	}
-
-	/**
-	 * 获取第pageIndex页的pdf数据并写入pdfFilePath路径下
-	 * 
-	 * @param userName
-	 *            不为null;当前账套用户名
-	 * @param profile
-	 *            可选(UAS系统不必传递该参数),用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
-	 * @param reportName
-	 *            不为null;需要预览的报表的名称,不带任何后缀(如预览采购单,即为"Purchase")
-	 * @param whereCondition
-	 *            可为null;where之后的条件(包括where)
-	 * @param otherParameters
-	 *            若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
-	 *            JSON格式,数据为键值对
-	 * @param pdfFilePath
-	 *            该报表对应的pdf绝对路径
-	 * @param pageIndex
-	 *            页码
-	 * @return 总页数
-	 */
-	private Integer createPdfFile(String userName, String profile, String reportName, String whereCondition,
-			String otherParameters, String pdfFilePath, Integer pageIndex) {
-		File file = new File(pdfFilePath);
-		Map<String, Object> result = preview(userName, profile, 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 (ArrayUtils.isEmpty(data) || pageSize == null) {
-			throw new ReportException("获取预览数据失败");
-		}
-		FileUtils.create(file.getPath(), data);
-		return pageSize;
-	}
-
 	/**
 	/**
 	 * 输出报表的数据
 	 * 输出报表的数据
 	 * 
 	 * 

+ 0 - 5
src/main/webapp/WEB-INF/views/preview.html

@@ -242,11 +242,6 @@ See https://github.com/adobe-type-tools/cmap-resources
 								title="下载PDF" tabindex="35">
 								title="下载PDF" tabindex="35">
 								<i class="fa fa-file-pdf-o fa-lg"></i>
 								<i class="fa fa-file-pdf-o fa-lg"></i>
 							</button>
 							</button>
-							<button id="download_excel"
-								class="toolbarButton hiddenMediumView" title="下载Excel"
-								tabindex="36">
-								<i class="fa fa-file-excel-o fa-lg"></i>
-							</button>
 							<button id="download_excel_with_only_data"
 							<button id="download_excel_with_only_data"
 								class="toolbarButton hiddenMediumView" title="下载Excel(仅数据)"
 								class="toolbarButton hiddenMediumView" title="下载Excel(仅数据)"
 								tabindex="37">
 								tabindex="37">

+ 6 - 149
src/main/webapp/resources/js/preview2/app.js

@@ -99,77 +99,18 @@ $("#print").click(function() {
 });
 });
 
 
 // 下载pdf
 // 下载pdf
-$("#downloadPdf").click(
-		function() {
-			if (!pdfDoc) {
-				return;
-			}
-			showLoading();
-			// 检查文件状态,直到其可用
-			var valid = getGeneratedPdfOrXlsInformation('pdf').valid;
-			// 文件除了要保证有效(存在并且未过有效期),还要确保hiddenFrameLoaded不能为真,因为存在下列情况
-			// 1.hiddenFrameLoaded不为真,即此时刚进入页面,文档尚未生成,需要等待其生成;
-			// 2.hiddenFrameLoaded为真,页面加载成功后并未关闭,闲置了一段时间,再次点击打印,
-			// 此时文件可能已经过期,需要立即请求下载(后台此时会重新生成),不能处于等待状态;
-			if (!valid && !hiddenFrameLoaded) {
-				console.log(new Date().format() + " ---- 文件正在生成,请等待");
-				console.log(new Date().format()
-						+ " ---- subscribed wholePdfGeneratedSignal");
-				$.subscribe("wholePdfGeneratedSignal", downloadPdf);
-				waitWholePdfGenerated();
-				// setTimeout(
-				// "valid =
-				// getGeneratedPdfOrXlsInformation('pdf').valid;console.log(valid)",
-				// 1000);
-				// valid = getGeneratedPdfOrXlsInformation("pdf").valid;
-				// sleep(1000);
-			} else {
-				hideLoading();
-				window.location = downloadUrl("pdf");
-			}
-		});
-
-/**
- * 下载文件
- */
-function downloadPdf() {
-	hideLoading();
-	console.log(new Date().format()
-			+ " ---- received and unsubscribe wholePdfGeneratedSignal");
-	$.unsubscribe("wholePdfGeneratedSignal", downloadPdf);
-	console.log(new Date().format() + " ---- start print");
-	window.location = downloadUrl("pdf");
-}
-
-/**
- * 每隔一定时间查询文件状态,直到文件有效
- */
-function waitWholePdfGenerated() {
-	var valid = getGeneratedPdfOrXlsInformation("pdf").valid;
-	if (!valid) {
-		console.log(new Date().format() + " ---- 文件还未生成");
-		console.log(new Date().format() + " ---- wait 3000ms")
-		setTimeout("waitWholePdfGenerated()", 3000);
-	} else {
-		console.log(new Date().format() + " ---- 文件已生成");
-		console.log(new Date().format()
-				+ " ---- published wholePdfGeneratedSignal");
-		$.publish("wholePdfGeneratedSignal", waitWholePdfGenerated);
+$("#downloadPdf").click(function() {
+	if (!pdfDoc) {
+		return;
 	}
 	}
-}
+	window.location = downloadUrl("pdf");
+});
 
 
 // 下载纯数据excel
 // 下载纯数据excel
 $("#downloadExcelWithOnlyData").click(function() {
 $("#downloadExcelWithOnlyData").click(function() {
 	if (!pdfDoc) {
 	if (!pdfDoc) {
 		return;
 		return;
 	}
 	}
-	showLoading();
-	var size;
-	while (!size) {
-		size = getGeneratedPdfOrXlsInformation("xls").size;
-		sleep(1000);
-	}
-	hideLoading();
 	window.location = downloadUrl("xls_with_only_data");
 	window.location = downloadUrl("xls_with_only_data");
 });
 });
 
 
@@ -216,36 +157,8 @@ function printPdf() {
 		alert("文件过大,建议先下载到本地,再进行打印");
 		alert("文件过大,建议先下载到本地,再进行打印");
 		return;
 		return;
 	}
 	}
-	// 判断是否存在
-	// var size;
-	// while (!size) {
-	// size = getGeneratedPdfOrXlsInformation("pdf").valid;
-	// sleep(1000);
-	// }
-	if (!hiddenFrameLoaded) {
-		showLoading();
-		console.log(new Date().format() + " ---- 需打印的文档未生成,正在生成文档");
-		// 订阅信号 "hiddenFrameLoadedSignal",等待整个pdf加载完成
-		console.log(new Date().format()
-				+ " ---- subscribed hiddenFrameLoadedSignal");
-		$.subscribe("hiddenFrameLoadedSignal", print);
-	} else {
-		hideLoading();
-		alert("正在加载文档");
-		console.log(new Date().format() + " ---- start print...");
-		hiddenFrame.contentWindow.print();
-	}
-}
-
-/**
- * 调用浏览器的打印方法并取消订阅信号 "hiddenFrameLoadedSignal"
- */
-function print() {
-	console.log(new Date().format()
-			+ " ---- received and unsubscribe hiddenFrameLoadedSignal");
-	$.unsubscribe("hiddenFrameLoadedSignal", print);
+	console.log(new Date().format() + " ---- start print...");
 	hiddenFrame.contentWindow.print();
 	hiddenFrame.contentWindow.print();
-	alert("文档加载完成");
 }
 }
 
 
 /**
 /**
@@ -347,39 +260,6 @@ function loadWholePdf() {
 		hiddenFrameLoaded = true;
 		hiddenFrameLoaded = true;
 		return;
 		return;
 	}
 	}
-
-	var hiddenFrameOnload = function() {
-		// 加载失败时(pdf文件不存在,404错误),title不为null
-		// 此时等待一段时间再重新加载
-		if (hiddenFrame.contentDocument.title) {
-			console.log(new Date().format() + " ---- file not exist");
-			console.log(new Date().format() + " ---- hiddenFrame load failed");
-			// 等待时间为10*页数 ms(需要在500ms~5000ms之间)
-			var waitTime = pageSize * 10;
-			waitTime = waitTime > 5000 ? 5000 : waitTime;
-			waitTime = waitTime < 500 ? 500 : waitTime;
-			console.log(new Date().format() + " ---- wait " + waitTime + "ms");
-			setTimeout("hiddenFrame.src = wholePdfPath", waitTime);
-			console.log(new Date().format() + " ---- hiddenFrame reloading...");
-
-		} else {
-			console.log(new Date().format()
-					+ " ---- hiddenFrame load successed");
-			hiddenFrameLoaded = true;
-			console.log(new Date().format()
-					+ " ---- publish hiddenFrameLoadedSignal");
-			// 发布信号 "hiddenFrameLoadedSignal"
-			$.publish("hiddenFrameLoadedSignal");
-		}
-	};
-
-	// 绑定onload事件
-	// 低版本Chrome浏览器的onload事件
-	if (hiddenFrame.attachEvent) {
-		hiddenFrame.attachEvent("onload", hiddenFrameOnload)
-	} else {
-		hiddenFrame.onload = hiddenFrameOnload;
-	}
 	// 开始加载
 	// 开始加载
 	console.log(new Date().format() + " ---- hiddenFrame loading...");
 	console.log(new Date().format() + " ---- hiddenFrame loading...");
 	hiddenFrame.src = wholePdfPath;
 	hiddenFrame.src = wholePdfPath;
@@ -486,29 +366,6 @@ function changeTextOfSelectScale() {
 	hiddenOption.hidden = true;
 	hiddenOption.hidden = true;
 }
 }
 
 
-/**
- * 获取生成的pdf或者xls的信息
- * 
- * @param pdfOrXls
- *            pdf或者xls
- */
-function getGeneratedPdfOrXlsInformation(pdfOrXls) {
-	var data;
-	$.ajax({
-		type : "get",
-		async : false,
-		url : "print/getGeneratedPdfOrXlsInformation" + window.location.search
-				+ "&pdfOrXls=" + pdfOrXls,
-		success : function(result) {
-			console
-					.log(new Date().format() + " ---- "
-							+ JSON.stringify(result));
-			data = result;
-		}
-	});
-	return data;
-}
-
 /**
 /**
  * 显示正在载入的动画
  * 显示正在载入的动画
  */
  */