rpt_print.js 11 KB

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