|
@@ -0,0 +1,198 @@
|
|
|
+/**
|
|
|
+ * Created by zhang on 2017/8/14.
|
|
|
+ */
|
|
|
+
|
|
|
+const prf_cons = require('./rpt_pdf_consts');
|
|
|
+var pdf = require('pdfkit');
|
|
|
+var fs = require('fs');
|
|
|
+let reportO = require('./reportOj');
|
|
|
+
|
|
|
+let JV = prf_cons.JV;
|
|
|
+export_pdf_file();
|
|
|
+function export_pdf_file (data) {
|
|
|
+ let offsetX= 10;
|
|
|
+ let offsetY=10;
|
|
|
+ var doc = new pdf({autoFirstPage: false});
|
|
|
+ doc.pipe(fs.createWriteStream('导出.pdf'));
|
|
|
+ let pageObj = reportO.reportObj;
|
|
|
+ // doc.rect(5,5,1190,890).lineWidth(1).strokeColor('black').stroke();//边框
|
|
|
+
|
|
|
+ if (pageObj && pageObj.items.length > 0 ) {
|
|
|
+ for(let i=0;i<pageObj.items.length;i++){
|
|
|
+ doc.addPage({size:[1200,900]});
|
|
|
+ var page = pageObj.items[i],
|
|
|
+ fonts = pageObj[JV.NODE_FONT_COLLECTION],
|
|
|
+ styles = pageObj[JV.NODE_STYLE_COLLECTION],
|
|
|
+ controls = pageObj[JV.NODE_CONTROL_COLLECTION],
|
|
|
+ mergedBand = pageObj[JV.BAND_PROP_MERGE_BAND];
|
|
|
+
|
|
|
+ for (var j = 0; j < page.cells.length; j++) {
|
|
|
+ var cell = page.cells[j];
|
|
|
+ private_drawCell(cell, fonts, styles, controls, mergedBand);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ doc.end();
|
|
|
+
|
|
|
+
|
|
|
+ function private_drawCell(cell, fonts, styles, controls, mergedBand) {
|
|
|
+ doc.save();
|
|
|
+ //doc.translate(0.5,0.5);
|
|
|
+ var style = styles[cell[JV.PROP_STYLE]];
|
|
|
+ if (style) {
|
|
|
+ private_drawLine(cell, doc, style, JV.PROP_TOP, [JV.PROP_LEFT, JV.PROP_TOP],[JV.PROP_RIGHT, JV.PROP_TOP], mergedBand, styles);
|
|
|
+ private_drawLine(cell, doc, style, JV.PROP_RIGHT, [JV.PROP_RIGHT, JV.PROP_TOP],[JV.PROP_RIGHT, JV.PROP_BOTTOM], mergedBand, styles);
|
|
|
+ private_drawLine(cell, doc, style, JV.PROP_BOTTOM, [JV.PROP_RIGHT, JV.PROP_BOTTOM],[JV.PROP_LEFT, JV.PROP_BOTTOM], mergedBand, styles);
|
|
|
+ private_drawLine(cell, doc, style, JV.PROP_LEFT, [JV.PROP_LEFT, JV.PROP_BOTTOM],[JV.PROP_LEFT, JV.PROP_TOP], mergedBand, styles);
|
|
|
+ }
|
|
|
+ private_drawCellText(cell, fonts, controls);
|
|
|
+ doc.restore();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function private_drawLine(cell, doc, style, styleBorderDest, startP, destP, mergedBand, styles) {
|
|
|
+ //doc.beginPath();
|
|
|
+ var destStyle = style;
|
|
|
+ if (mergedBand) {
|
|
|
+ if (mergedBand[styleBorderDest] == cell[JV.PROP_AREA][styleBorderDest]) {
|
|
|
+ destStyle = styles[mergedBand[JV.PROP_STYLE][JV.PROP_ID]];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ doc.moveTo(cell[JV.PROP_AREA][startP[0]] + offsetX, cell[JV.PROP_AREA][startP[1]] + offsetY);
|
|
|
+ if (destStyle[styleBorderDest] && destStyle[styleBorderDest][JV.PROP_LINE_WEIGHT] != 0) {
|
|
|
+ doc.lineWidth(1.0 * destStyle[styleBorderDest][JV.PROP_LINE_WEIGHT]);
|
|
|
+ doc.lineTo(cell[JV.PROP_AREA][destP[0]] + offsetX, cell[JV.PROP_AREA][destP[1]] + offsetY);
|
|
|
+ doc.strokeColor(destStyle[styleBorderDest][JV.PROP_COLOR]);
|
|
|
+ }
|
|
|
+ doc.stroke();
|
|
|
+ }
|
|
|
+ function private_drawCellText(cell, fonts, controls) {
|
|
|
+ if (cell[JV.PROP_VALUE]) {
|
|
|
+ var values = ("" + cell[JV.PROP_VALUE]).split('|');
|
|
|
+ var font = fonts[cell[JV.PROP_FONT]];
|
|
|
+ var control = controls[cell[JV.PROP_CONTROL]];
|
|
|
+ var height = cell[JV.PROP_AREA][JV.PROP_BOTTOM] - cell[JV.PROP_AREA][JV.PROP_TOP];
|
|
|
+ var area = [cell[JV.PROP_AREA][JV.PROP_LEFT] + offsetX, cell[JV.PROP_AREA][JV.PROP_TOP] + offsetY, cell[JV.PROP_AREA][JV.PROP_RIGHT] + offsetX, cell[JV.PROP_AREA][JV.PROP_BOTTOM] + offsetY];
|
|
|
+ for (var i = 0; i < values.length; i++) {
|
|
|
+ area[JV.IDX_TOP] = cell[JV.PROP_AREA][JV.PROP_TOP] + i * (height / values.length) + offsetY;
|
|
|
+ area[JV.IDX_BOTTOM] = cell[JV.PROP_AREA][JV.PROP_TOP] + (i + 1) * (height / values.length) + offsetY;
|
|
|
+ private_drawText(values[i], area, font, control);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function private_drawText(val, area, font, control) {
|
|
|
+ var dftFontHeight = 12;
|
|
|
+ var output = [];
|
|
|
+ if (font) {
|
|
|
+ dftFontHeight = 1 * font[JV.FONT_PROPS[1]];
|
|
|
+ var dftOthers = "";
|
|
|
+ var dftFontBold = font[JV.FONT_PROPS[3]];
|
|
|
+ if (dftFontBold && dftFontBold == 'T') {
|
|
|
+ doc.font('simsunB.ttf');
|
|
|
+ }else {
|
|
|
+ doc.font('Smart.ttf');
|
|
|
+ }
|
|
|
+ /* var dftFontItalic = font[JV.FONT_PROPS[4]]; 没有找到中文斜体的字库,暂不支持
|
|
|
+ if (dftFontItalic && dftFontItalic == 'T') {
|
|
|
+ dftOthers = dftOthers + "italic ";
|
|
|
+ }*/
|
|
|
+ // ctx.font = dftOthers + dftFontHeight + "px " + font[JV.PROP_NAME];
|
|
|
+ doc.fontSize(dftFontHeight);
|
|
|
+ }
|
|
|
+ var options={};
|
|
|
+ if (control) {
|
|
|
+ private_setupAreaH(area, control.Horizon, font.FontAngle, dftFontHeight, output,options);
|
|
|
+ private_setupAreaV(area, control.Vertical, font.FontAngle, dftFontHeight, output);
|
|
|
+ } else {
|
|
|
+ private_setupAreaH(area, "left", font.FontAngle, dftFontHeight, output,options);
|
|
|
+ private_setupAreaV(area, "bottom", font.FontAngle, dftFontHeight, output);
|
|
|
+ }
|
|
|
+ var w = area[JV.IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - area[JV.IDX_LEFT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT];
|
|
|
+ if (font.FontAngle != "0") {
|
|
|
+ w = area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM] - area[JV.IDX_TOP] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
|
|
|
+ }
|
|
|
+ doc.save();
|
|
|
+ //doc.translate(output[0], output[1]);
|
|
|
+ if (w >= doc.widthOfString(val)) {
|
|
|
+ options.width=w;
|
|
|
+ options.height=dftFontHeight;
|
|
|
+ } else {
|
|
|
+ while (true) {
|
|
|
+ dftFontHeight--;
|
|
|
+ doc.fontSize(dftFontHeight);
|
|
|
+ if (w >= doc.widthOfString(val) || dftFontHeight < 6) {
|
|
|
+ options.width=w;
|
|
|
+ options.height=dftFontHeight;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var rotateOptions;
|
|
|
+ if (font.FontAngle != "0") {
|
|
|
+ if (control){
|
|
|
+ rotateOptions=private_setupAreaRotateOption(area,w,control.Vertical,dftFontHeight, output);
|
|
|
+ }else {
|
|
|
+ rotateOptions=private_setupAreaRotateOption(area,w,"bottom",dftFontHeight, output);
|
|
|
+ }
|
|
|
+ doc.rotate(font.FontAngle,rotateOptions);
|
|
|
+ }
|
|
|
+ doc.text(val,output[0], output[1],options);
|
|
|
+ doc.restore();
|
|
|
+ }
|
|
|
+
|
|
|
+ function private_setupAreaH(area, type, fontAngle, dftFontHeight, outputPoint,options) {
|
|
|
+ var lType = type;
|
|
|
+ if (type != "left" && type != "right" && type != "center") lType = "left";
|
|
|
+ options.align=lType;
|
|
|
+ outputPoint[0]=1 * area[JV.IDX_LEFT]+ JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
|
|
|
+ }
|
|
|
+
|
|
|
+ function private_setupAreaV(area, type, fontAngle, dftFontHeight, outputPoint) {
|
|
|
+ var lType = type;
|
|
|
+ if (type != "top" && type != "bottom" && type != "center") lType = "top";
|
|
|
+ switch (lType) {
|
|
|
+ case "top":
|
|
|
+ outputPoint[1] = 1 * area[JV.IDX_TOP] + JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
|
|
|
+ break;
|
|
|
+ case "bottom":
|
|
|
+ outputPoint[1] = 1 * area[JV.IDX_BOTTOM] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM]-dftFontHeight;
|
|
|
+ break;
|
|
|
+ case "center":
|
|
|
+ outputPoint[1] = 1 * area[JV.IDX_TOP] + (1 * area[JV.IDX_BOTTOM]-1 * area[JV.IDX_TOP]- dftFontHeight) / 2;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function private_setupAreaRotateOption(area,w, type="top",dftFontHeight,outputPoint){
|
|
|
+ var x = (area[JV.IDX_RIGHT] - area[JV.IDX_LEFT])/2+area[JV.IDX_LEFT];
|
|
|
+ var y =(area[JV.IDX_BOTTOM] - area[JV.IDX_TOP])/2+ area[JV.IDX_TOP];
|
|
|
+ var rotateOptions = {origin:[x,y]};
|
|
|
+ var h = area[JV.IDX_RIGHT] - area[JV.IDX_LEFT];
|
|
|
+ outputPoint[0]=x-w/2+JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT];
|
|
|
+ var lType = type;
|
|
|
+ switch (lType) {
|
|
|
+ case "top":
|
|
|
+ outputPoint[1] = y- h/2+ JV.OUTPUT_OFFSET[JV.OFFSET_IDX_TOP];
|
|
|
+ break;
|
|
|
+ case "bottom":
|
|
|
+ outputPoint[1] = y+ h/2-JV.OUTPUT_OFFSET[JV.OFFSET_IDX_BOTTOM]-dftFontHeight;
|
|
|
+ break;
|
|
|
+ case "center":
|
|
|
+ outputPoint[1] = y+dftFontHeight/2;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return rotateOptions;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|