rpt_print.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * Created by Tony on 2018/4/24.
  3. */
  4. let rptPrintHelper = {
  5. preview: function () {
  6. if (zTreeOprObj.checkedRptTplNodes && zTreeOprObj.checkedRptTplNodes.length > 0) {
  7. let rptIds = [];
  8. let params = {};
  9. params.custCfg = zTreeOprObj.reportPageCfg;
  10. params.prj_id = projectInfoObj.projectInfo.ID;
  11. for (let tplNode of zTreeOprObj.checkedRptTplNodes) {
  12. rptIds.push(tplNode.refId);
  13. }
  14. params.rpt_ids = rptIds.join(",");
  15. CommonAjax.postEx("report_api/getMultiReports", params, 10000, true,
  16. function(result){
  17. //sessionStorage.currentPageData = JSON.stringify(zTreeOprObj.currentRptPageRst);
  18. sessionStorage.multiRptsData = JSON.stringify(result);
  19. sessionStorage.pageSize = rptControlObj.getCurrentPageSize();
  20. sessionStorage.orientation = rptControlObj.getCurrentOrientation();
  21. sessionStorage.scaleFactor = 1;
  22. window.open('/rpt_print');
  23. },
  24. function(failRst){
  25. sessionStorage.currentPageData = null;
  26. console.log(failRst);
  27. },
  28. function(exceptionRst){
  29. sessionStorage.currentPageData = null;
  30. console.log(exceptionRst);
  31. }
  32. );
  33. } else {
  34. //不可能的branch
  35. }
  36. // if (zTreeOprObj.currentRptPageRst) {
  37. // sessionStorage.currentPageData = JSON.stringify(zTreeOprObj.currentRptPageRst);
  38. // sessionStorage.pageSize = rptControlObj.getCurrentPageSize();
  39. // sessionStorage.orientation = rptControlObj.getCurrentOrientation();
  40. // sessionStorage.scaleFactor = 1;
  41. // window.open('/rpt_print');
  42. // } else {
  43. // sessionStorage.currentPageData = null;
  44. // }
  45. },
  46. previewSvgData: function() {
  47. //
  48. },
  49. buildSvgArr: function (pagesData, offsetX, offsetY) {
  50. let styles = pagesData[JV.NODE_STYLE_COLLECTION],
  51. fonts = pagesData[JV.NODE_FONT_COLLECTION],
  52. controls = pagesData[JV.NODE_CONTROL_COLLECTION]
  53. ;
  54. let rst = [];
  55. let canvas = document.getElementById("chkCanvas");
  56. for (let idx = 0; idx < pagesData.items.length; idx++) {
  57. let page = pagesData.items[idx];
  58. let svgPageArr = [], pixelSize = getPixelSize(pagesData);
  59. svgPageArr.push("<svg width='" + pixelSize[0] + "' height='" + pixelSize[1] + "'>");
  60. let adjustY = 0.5 * ((idx + 1) % 2);
  61. for (let cell of page.cells) {
  62. svgPageArr.push(buildCellSvg(cell, fonts, styles, controls, page[JV.PROP_PAGE_MERGE_BORDER], pagesData[JV.BAND_PROP_MERGE_BAND], offsetX, offsetY, adjustY, canvas));
  63. }
  64. svgPageArr.push("</svg>");
  65. rst.push(svgPageArr);
  66. }
  67. return rst;
  68. }
  69. };
  70. function getActualBorderStyle(cell, styles, mergeBorderStyle, pageBorderArea, borderStr) {
  71. let rst = styles[cell[JV.PROP_STYLE]][borderStr];
  72. if (mergeBorderStyle) {
  73. if (parseFloat(cell[JV.PROP_AREA][borderStr]) === parseFloat(pageBorderArea[borderStr])) {
  74. if (borderStr === JV.PROP_LEFT || borderStr === JV.PROP_RIGHT) {
  75. if (parseFloat(cell[JV.PROP_AREA][JV.PROP_TOP]) >= parseFloat(pageBorderArea[JV.PROP_TOP]) &&
  76. parseFloat(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) <= parseFloat(pageBorderArea[JV.PROP_BOTTOM])) {
  77. rst = mergeBorderStyle[borderStr];
  78. }
  79. } else if (borderStr === JV.PROP_TOP || borderStr === JV.PROP_BOTTOM) {
  80. if (parseFloat(cell[JV.PROP_AREA][JV.PROP_LEFT]) >= parseFloat(pageBorderArea[JV.PROP_LEFT]) &&
  81. parseFloat(cell[JV.PROP_AREA][JV.PROP_RIGHT]) <= parseFloat(pageBorderArea[JV.PROP_RIGHT])) {
  82. rst = mergeBorderStyle[borderStr];
  83. }
  84. }
  85. }
  86. }
  87. return rst;
  88. }
  89. function buildCellSvg(cell, fonts, styles, controls, pageMergeBorder, rptMergeBorder, offsetX, offsetY, adjustY, canvas) {
  90. let rst = [];
  91. let style = styles[cell[JV.PROP_STYLE]];
  92. let mergeBandStyle = null;
  93. if (rptMergeBorder) {
  94. mergeBandStyle = styles[rptMergeBorder[JV.PROP_STYLE][JV.PROP_ID]];
  95. }
  96. let font = cell[JV.PROP_FONT];
  97. if (typeof font === 'string') {
  98. font = fonts[cell[JV.PROP_FONT]];
  99. }
  100. let left = parseInt(cell[JV.PROP_AREA][JV.PROP_LEFT]) + offsetX + 0.5,
  101. right = parseInt(cell[JV.PROP_AREA][JV.PROP_RIGHT]) + offsetX + 0.5,
  102. top = parseInt(cell[JV.PROP_AREA][JV.PROP_TOP]) + offsetY + adjustY,
  103. bottom = parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) + offsetY + adjustY
  104. ;
  105. if (style) {
  106. let leftBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_LEFT);
  107. // if (style[JV.PROP_LEFT] && parseFloat(style[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) > 0) {
  108. if (leftBS && parseFloat(leftBS[JV.PROP_LINE_WEIGHT]) > 0) {
  109. rst.push("<line x1='" + left + "' y1='" + top +
  110. "' x2='" + left + "' y2='" + bottom +
  111. "' style='stroke:rgb(0,0,0);stroke-width:" + leftBS[JV.PROP_LINE_WEIGHT] +"'/>")
  112. }
  113. let rightBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_RIGHT);
  114. // if (style[JV.PROP_RIGHT] && parseFloat(style[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) > 0) {
  115. if (rightBS && parseFloat(rightBS[JV.PROP_LINE_WEIGHT]) > 0) {
  116. rst.push("<line x1='" + right + "' y1='" + top +
  117. "' x2='" + right + "' y2='" + bottom +
  118. "' style='stroke:rgb(0,0,0);stroke-width:" + rightBS[JV.PROP_LINE_WEIGHT] +"'/>")
  119. }
  120. let topBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_TOP);
  121. // if (style[JV.PROP_TOP] && parseFloat(style[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) > 0) {
  122. if (topBS && parseFloat(topBS[JV.PROP_LINE_WEIGHT]) > 0) {
  123. rst.push("<line x1='" + left + "' y1='" + top +
  124. "' x2='" + right + "' y2='" + top +
  125. "' style='stroke:rgb(0,0,0);stroke-width:" + topBS[JV.PROP_LINE_WEIGHT] +"'/>")
  126. }
  127. let bottomBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_BOTTOM);
  128. // if (style[JV.PROP_BOTTOM] && parseFloat(style[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) > 0) {
  129. if (bottomBS && parseFloat(bottomBS[JV.PROP_LINE_WEIGHT]) > 0) {
  130. rst.push("<line x1='" + left + "' y1='" + bottom +
  131. "' x2='" + right + "' y2='" + bottom +
  132. "' style='stroke:rgb(0,0,0);stroke-width:" + bottomBS[JV.PROP_LINE_WEIGHT] +"'/>")
  133. }
  134. }
  135. buildText(rst, cell, font, controls[cell[JV.PROP_CONTROL]], offsetX, offsetY, adjustY, canvas);
  136. return rst.join("");
  137. }
  138. function buildText(destRst, cell, font, control, offsetX, offsetY, adjustY, canvas) {
  139. let orgFontHeight = parseInt(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_HEIGHT]]);
  140. let fontWeight = (font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] === 'T')?"bold":"normal";
  141. let fontStyle = (font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] === 'T')?"italic":"normal";
  142. let left = parseInt(cell[JV.PROP_AREA][JV.PROP_LEFT]) + offsetX + 0.5,
  143. right = parseInt(cell[JV.PROP_AREA][JV.PROP_RIGHT]) + offsetX + 0.5,
  144. top = parseInt(cell[JV.PROP_AREA][JV.PROP_TOP]) + offsetY + adjustY,
  145. bottom = parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) + offsetY + adjustY,
  146. x = left, y = top,
  147. text_anchor = "start"
  148. ;
  149. let value = cell[JV.PROP_VALUE];
  150. if (!(value)) {
  151. value = "";
  152. }
  153. let values = null;
  154. if (typeof value === "string") {
  155. values = value.split("|");
  156. } else {
  157. values = [value];
  158. }
  159. let stepHeight = (parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) - parseInt(cell[JV.PROP_AREA][JV.PROP_TOP])) / values.length;
  160. if (control) {
  161. if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]] === "left") {
  162. text_anchor = "start";
  163. x = left + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
  164. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]] === "right") {
  165. text_anchor = "end";
  166. x = right - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
  167. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]] === "center") {
  168. text_anchor = "middle";
  169. x = Math.round((left + right) / 2);
  170. }
  171. }
  172. for (let vidx = 0; vidx < values.length; vidx++) {
  173. //check whether need to adjust the font size
  174. let ctx = canvas.getContext("2d");
  175. let dftFontHeight = orgFontHeight;
  176. ctx.font = ((font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] === 'T')?"bold ":"") + ((font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] === 'T')?"italic":"") + dftFontHeight + "px " + font[JV.PROP_NAME];
  177. while ((right - left) <= ctx.measureText(values[vidx]).width) {
  178. if (dftFontHeight > 6) {
  179. dftFontHeight--;
  180. ctx.font = ((font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] === 'T')?"bold ":"") + ((font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] === 'T')?"italic":"") + dftFontHeight + "px " + font[JV.PROP_NAME];
  181. } else {
  182. break;
  183. }
  184. }
  185. dftFontHeight = (dftFontHeight * 3 / 4); //SVG的字体与canvas的字体大小的切换, 不用考虑取整
  186. if (control) {
  187. if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "top") {
  188. y = Math.round((top + vidx * stepHeight) + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP]);
  189. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "bottom") {
  190. y = Math.round((top + (vidx + 1) * stepHeight) - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM]);
  191. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "center") {
  192. y = Math.round(((top + vidx * stepHeight) + (top + (vidx + 1) * stepHeight) + dftFontHeight) / 2 );
  193. }
  194. }
  195. if (font[JV.PROP_NAME] === "宋体") {
  196. y--;
  197. }
  198. destRst.push("<text style='fill:black;font-family:" + font[JV.PROP_NAME] +
  199. ";font-weight:" + fontWeight +
  200. ";font-style:" + fontStyle +
  201. ";font-size:" + dftFontHeight + "pt' x='" +
  202. x +"' y='" + y + "' text-anchor='" + text_anchor + "' xml:space='preserve'>" + values[vidx] + "</text>");
  203. }
  204. }
  205. function getPixelSize(pagesData) {
  206. let rst = [793,1122];
  207. let SCREEN_DPI = [96,96];
  208. if (pagesData[JV.NODE_PAGE_INFO] && pagesData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE]) {
  209. rst[0] = Math.round(SCREEN_DPI[0] * pagesData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][0]);
  210. rst[1] = Math.round(SCREEN_DPI[1] * pagesData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1]);
  211. }
  212. return rst;
  213. }