使用fluentreports添加多个表

时间:2017-06-09 22:33:20

标签: node.js

我有三个阵列" listA,listB,listC"我想从这些列表中创建三个表。到目前为止,我只能创建一个单独的表格,而且我还没有能够弄清楚如何用文档做我想做的事情。 fluentreports documentaiton

以下是我到目前为止的内容,我正在查看.addReport并尝试使用它但我收到错误"未处理的拒绝TypeError:rpt.addReport不是函数"。我可以请一些帮助来显示多个表格。我的代码如下。



const express = require('express');
const db = require('../config/db');
const router = new express.Router();
const PDFDocument =  require('pdfkit') 
const Report = require('fluentreports').Report

var fs = require("fs")


router.get('/getpdf', (req, res) => {
  res.type('application/pdf');
   // Our Simple Data in Object format: 
  
  var results1;

  db.sequelize.query("SELECT device.\"deviceID\", device.\"deviceType\", event.data, event.\"event_time_stamp\" FROM  public.device, public.event WHERE  event.\"device_deviceID\" = device.\"deviceID\"").spread((results, metadata) => {
    

      //console.log(results)
      var listA = [];
      var listB = [];
      var listC = [];
      results.map(cv => {
        switch (cv.deviceType) {
          case 'type1':
            listA.push(cv);
            break;
          case 'type2':
            listB.push(cv);
            break;
          case 'type3':
            listC.push(cv);
            break;
            
          }
      });
    
   
   
    listA.map(cv => {
  [cv.quantity, cv.additional] = cv.data.split(','); 
  cv.deviceType =  'Poison Grizach';
  cv.quantity = cv.quantity + '%'
  delete cv.data;
});
    listB.map(cv => {
  [cv.mouse, cv.rat, cv.other, cv.additional] = cv.data.split(','); 
  cv.deviceType =  'Non-Poison Grizach';
  delete cv.data;
});
    listC.map(cv => {
  [cv.flies, cv.quantity, cv.additional] = cv.data.split(','); 
  cv.deviceType =  'Insect Lamp';
  cv.flies =  cv.flies + '%'
  delete cv.data;
});
   var all = listA.concat(listB).concat(listC)
   

 var headerFunction = function(Report) {
        Report.print("Device Scans", {fontSize: 22, bold: true, underline:true, align: "center"});
        Report.newLine(2);
        Report.band([
          {data: 'Device Type', width: 100},
          {data: 'Quantity', width: 50},
          {data: 'Mouse', width: 50},
           {data: 'Rat', width: 50},
          {data: 'Other', width: 50},
          {data: 'Additional', width: 50},
          {data: '% Myxi', width: 50},
      ]);
    };
// Create a Report   
  var rpt = new Report(res,({fontSize: 10}))     
        .pageHeader( ["Device Scans"] )     // Add a simple (optional) page Header...         
        .margins(40)
        .data( listA )               // Add some Data (This is required) 
        .pageHeader(headerFunction) 
        .detail( [ ['deviceType', 100], ['quantity', 50] , ['mouse', 50], ['rat', 50], ['other', 50], ['additional', 50] , ['flies', 50]]) // Layout the report in a Grid of 200px & 50px 
        .render()

  var rpt2 = new Report(res,({fontSize: 10}))     
        .pageHeader( ["Device Scans"] )     // Add a simple (optional) page Header...         
        .margins(40)
        .data( listB )               // Add some Data (This is required) 
        .pageHeader(headerFunction) 
        .detail( [ ['deviceType', 100] , ['mouse', 50], ['rat', 50], ['other', 50], ['additional', 50] ]) // Layout the report in a Grid of 200px & 50px 
  

    rpt.addReport(rpt2)

  })  




       

  });

module.exports = router;




1 个答案:

答案 0 :(得分:1)

解决方案包括在下面。父报告rpt包含在“兄弟”报告的创建中。使用isSibling:true选项从此实现中的最后一个兄弟调用Render。定义头文件以提供列名

获取请求:

router.get('/getpdf', (req, res) => {
  res.type('application/pdf');
   // Our Simple Data in Object format: 
 

  db.sequelize.query("SELECT device.\"deviceID\", device.\"deviceType\", event.data, event.\"event_time_stamp\" FROM  public.device, public.event WHERE  event.\"device_deviceID\" = device.\"deviceID\"").spread((results, metadata) => {
    

      //console.log(results)
      var listA = [];
      var listB = [];
      var listC = [];
      results.map(cv => {
        switch (cv.deviceType) {
          case 'type1':
            listA.push(cv);
            break;
          case 'type2':
            listB.push(cv);
            break;
          case 'type3':
            listC.push(cv);
            break;
            
          }
      });
    
   
   
    listA.map(cv => {
  [cv.quantity, cv.additional] = cv.data.split(','); 
  cv.deviceType =  'Poison Grizach';
  cv.quantity = cv.quantity + '%'
  delete cv.data;
});
    listB.map(cv => {
  [cv.mouse, cv.rat, cv.other, cv.additional] = cv.data.split(','); 
  cv.deviceType =  'Non-Poison Grizach';
  delete cv.data;
});
    listC.map(cv => {
  [cv.flies, cv.quantity, cv.additional] = cv.data.split(','); 
  cv.deviceType =  'Insect Lamp';
  cv.flies =  cv.flies + '%'
  delete cv.data;
});
   var all = listA.concat(listB).concat(listC)
   

 var headerFunction = function(Report) {
        
       
        Report.newLine(2);
        Report.band([
          {data: 'Device Type', width: 100},
          {data: 'Quantity', width: 50},
          {data: 'Additional', width: 50},
          
      ]);
};
 var headerFunction2 = function(Report) {
        
        Report.newLine(2);
        Report.band([
          {data: 'Device Type', width: 100},
          {data: 'Mouse', width: 50},
          {data: 'Rat', width: 50},
          {data: 'Other', width: 50},
          {data: 'Additional', width: 50},
          

      ]);
};
 var headerFunction3 = function(Report) {
        
        Report.newLine(2);
        Report.band([
          {data: 'Device Type', width: 100},
          {data: 'Burned Bulbs', width: 80},
          {data: '% Flies', width: 50},
          {data: 'Change Board', width: 80},
          

      ]);
};

// Create a Report   
  var rpt = new Report(res,({fontSize: 10}))
        .titleHeader("Device Scans")
        .margins(40)
        .data( {})               // Add some Data (This is required) 
       

  var rpt1 = new Report(rpt,({fontSize: 10}))
        .margins(40)
        .data( listA )               // Add some Data (This is required) 
        .pageHeader(headerFunction) 
        .detail( [ ['deviceType', 100], ['quantity', 50] , ['additional', 50]]) // Layout the report in a Grid of 200px & 50px       

  var rpt2 = new Report(rpt, ({isSibling: true}))     
        .margins(40)
        .data( listB )               // Add some Data (This is required) 
        .pageHeader(headerFunction2) 
        .detail( [ ['deviceType', 100] , ['mouse', 50], ['rat', 50], ['other', 50], ['additional', 50] ]) // Layout the report in a Grid of 200px & 50px 
        

  var rpt3 = new Report(rpt, ({isSibling: true}))     
         .pageHeader( ["Device Scans"] )
        .margins(40)
        .data( listC )               // Add some Data (This is required) 
        .pageHeader(headerFunction3) 
        .detail( [ ['deviceType', 100] , ['quantity', 80] , ['flies', 50] ,['additional', 80] ]) // Layout the report in a Grid of 200px & 50px 
        .render()

   
  })