rpt_print.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. let adjustY = 0.5;
  62. for (let cell of page.cells) {
  63. svgPageArr.push(buildCellSvg(cell, fonts, styles, controls, page[JV.PROP_PAGE_MERGE_BORDER], pagesData[JV.BAND_PROP_MERGE_BAND], offsetX, offsetY, adjustY, canvas));
  64. }
  65. svgPageArr.push("</svg>");
  66. rst.push(svgPageArr);
  67. }
  68. return rst;
  69. }
  70. };
  71. function getActualBorderStyle(cell, styles, mergeBorderStyle, pageBorderArea, borderStr) {
  72. let rst = styles[cell[JV.PROP_STYLE]][borderStr];
  73. if (mergeBorderStyle) {
  74. if (parseFloat(cell[JV.PROP_AREA][borderStr]) === parseFloat(pageBorderArea[borderStr])) {
  75. if (borderStr === JV.PROP_LEFT || borderStr === JV.PROP_RIGHT) {
  76. if (parseFloat(cell[JV.PROP_AREA][JV.PROP_TOP]) >= parseFloat(pageBorderArea[JV.PROP_TOP]) &&
  77. parseFloat(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) <= parseFloat(pageBorderArea[JV.PROP_BOTTOM])) {
  78. rst = mergeBorderStyle[borderStr];
  79. }
  80. } else if (borderStr === JV.PROP_TOP || borderStr === JV.PROP_BOTTOM) {
  81. if (parseFloat(cell[JV.PROP_AREA][JV.PROP_LEFT]) >= parseFloat(pageBorderArea[JV.PROP_LEFT]) &&
  82. parseFloat(cell[JV.PROP_AREA][JV.PROP_RIGHT]) <= parseFloat(pageBorderArea[JV.PROP_RIGHT])) {
  83. rst = mergeBorderStyle[borderStr];
  84. }
  85. }
  86. }
  87. }
  88. return rst;
  89. }
  90. function buildCellSvg(cell, fonts, styles, controls, pageMergeBorder, rptMergeBorder, offsetX, offsetY, adjustY, canvas) {
  91. let rst = [];
  92. let style = styles[cell[JV.PROP_STYLE]];
  93. let mergeBandStyle = null;
  94. if (rptMergeBorder) {
  95. mergeBandStyle = styles[rptMergeBorder[JV.PROP_STYLE][JV.PROP_ID]];
  96. }
  97. let font = cell[JV.PROP_FONT];
  98. if (typeof font === 'string') {
  99. font = fonts[cell[JV.PROP_FONT]];
  100. }
  101. let left = parseInt(cell[JV.PROP_AREA][JV.PROP_LEFT]) + offsetX + 0.5,
  102. right = parseInt(cell[JV.PROP_AREA][JV.PROP_RIGHT]) + offsetX + 0.5,
  103. top = parseInt(cell[JV.PROP_AREA][JV.PROP_TOP]) + offsetY + adjustY,
  104. bottom = parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) + offsetY + adjustY
  105. ;
  106. if (style) {
  107. let leftBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_LEFT);
  108. // if (style[JV.PROP_LEFT] && parseFloat(style[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) > 0) {
  109. if (leftBS && parseFloat(leftBS[JV.PROP_LINE_WEIGHT]) > 0) {
  110. rst.push("<line x1='" + left + "' y1='" + top +
  111. "' x2='" + left + "' y2='" + bottom +
  112. "' style='stroke:rgb(0,0,0);stroke-width:" + leftBS[JV.PROP_LINE_WEIGHT] +"'/>")
  113. }
  114. let rightBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_RIGHT);
  115. // if (style[JV.PROP_RIGHT] && parseFloat(style[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) > 0) {
  116. if (rightBS && parseFloat(rightBS[JV.PROP_LINE_WEIGHT]) > 0) {
  117. rst.push("<line x1='" + right + "' y1='" + top +
  118. "' x2='" + right + "' y2='" + bottom +
  119. "' style='stroke:rgb(0,0,0);stroke-width:" + rightBS[JV.PROP_LINE_WEIGHT] +"'/>")
  120. }
  121. let topBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_TOP);
  122. // if (style[JV.PROP_TOP] && parseFloat(style[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) > 0) {
  123. if (topBS && parseFloat(topBS[JV.PROP_LINE_WEIGHT]) > 0) {
  124. rst.push("<line x1='" + left + "' y1='" + top +
  125. "' x2='" + right + "' y2='" + top +
  126. "' style='stroke:rgb(0,0,0);stroke-width:" + topBS[JV.PROP_LINE_WEIGHT] +"'/>")
  127. }
  128. let bottomBS = getActualBorderStyle(cell, styles, mergeBandStyle, (pageMergeBorder)?pageMergeBorder:rptMergeBorder[JV.PROP_AREA], JV.PROP_BOTTOM);
  129. // if (style[JV.PROP_BOTTOM] && parseFloat(style[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) > 0) {
  130. if (bottomBS && parseFloat(bottomBS[JV.PROP_LINE_WEIGHT]) > 0) {
  131. rst.push("<line x1='" + left + "' y1='" + bottom +
  132. "' x2='" + right + "' y2='" + bottom +
  133. "' style='stroke:rgb(0,0,0);stroke-width:" + bottomBS[JV.PROP_LINE_WEIGHT] +"'/>")
  134. }
  135. }
  136. buildText(rst, cell, font, controls[cell[JV.PROP_CONTROL]], offsetX, offsetY, adjustY, canvas);
  137. return rst.join("");
  138. }
  139. function buildText(destRst, cell, font, control, offsetX, offsetY, adjustY, canvas) {
  140. let orgFontHeight = parseInt(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_HEIGHT]]);
  141. let fontWeight = (font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] === 'T')?"bold":"normal";
  142. let fontStyle = (font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] === 'T')?"italic":"normal";
  143. let fontUnderline = (font[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]] === 'T')?"underline":"normal";
  144. let left = parseInt(cell[JV.PROP_AREA][JV.PROP_LEFT]) + offsetX + 0.5,
  145. right = parseInt(cell[JV.PROP_AREA][JV.PROP_RIGHT]) + offsetX + 0.5,
  146. top = parseInt(cell[JV.PROP_AREA][JV.PROP_TOP]) + offsetY + adjustY,
  147. bottom = parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) + offsetY + adjustY,
  148. x = left, y = top,
  149. text_anchor = "start"
  150. ;
  151. let value = cell[JV.PROP_VALUE];
  152. if (!(value)) {
  153. value = "";
  154. }
  155. let values = null;
  156. if (typeof value === "string") {
  157. values = value.split("|");
  158. } else {
  159. values = [value];
  160. }
  161. let stepHeight = (parseInt(cell[JV.PROP_AREA][JV.PROP_BOTTOM]) - parseInt(cell[JV.PROP_AREA][JV.PROP_TOP])) / values.length;
  162. if (control) {
  163. if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]] === "left") {
  164. text_anchor = "start";
  165. x = left + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
  166. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]] === "right") {
  167. text_anchor = "end";
  168. x = right - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
  169. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]] === "center") {
  170. text_anchor = "middle";
  171. x = Math.round((left + right) / 2);
  172. }
  173. }
  174. for (let vidx = 0; vidx < values.length; vidx++) {
  175. //check whether need to adjust the font size
  176. let ctx = canvas.getContext("2d");
  177. let dftFontHeight = orgFontHeight;
  178. 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];
  179. while ((right - left) <= ctx.measureText(values[vidx]).width) {
  180. if (dftFontHeight > 6) {
  181. dftFontHeight--;
  182. 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];
  183. } else {
  184. break;
  185. }
  186. }
  187. dftFontHeight = (dftFontHeight * 3 / 4); //SVG的字体与canvas的字体大小的切换, 不用考虑取整
  188. if (control) {
  189. if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "top") {
  190. y = Math.round((top + vidx * stepHeight) + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP]);
  191. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "bottom") {
  192. y = Math.round((top + (vidx + 1) * stepHeight) - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM]);
  193. } else if (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]] === "center") {
  194. y = Math.round(((top + vidx * stepHeight) + (top + (vidx + 1) * stepHeight) + dftFontHeight) / 2 );
  195. }
  196. }
  197. if (font[JV.PROP_NAME] === "宋体") {
  198. y--;
  199. }
  200. destRst.push("<text style='fill:black;font-family:" + font[JV.PROP_NAME] +
  201. ";font-weight:" + fontWeight +
  202. ";font-style:" + fontStyle +
  203. ";text-decoration:" + fontUnderline +
  204. // ";text-decoration:normal" +
  205. ";font-size:" + dftFontHeight + "pt' x='" +
  206. x +"' y='" + y + "' text-anchor='" + text_anchor + "' xml:space='preserve'>" + values[vidx] + "</text>");
  207. }
  208. }
  209. function getPixelSize(pagesData) {
  210. let rst = [793,1122];
  211. let SCREEN_DPI = [96,96];
  212. if (pagesData[JV.NODE_PAGE_INFO] && pagesData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE]) {
  213. rst[0] = Math.round(SCREEN_DPI[0] * pagesData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][0]);
  214. rst[1] = Math.round(SCREEN_DPI[1] * pagesData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1]);
  215. }
  216. return rst;
  217. }