在nodejs中生成的pdf报告中未打印数据

时间:2016-12-29 18:53:18

标签: mysql node.js

我正在尝试使用api fluentreports在nodejs中生成一个非常简单的报告。数据来自mysql db。但是,生成的pdf文件中未打印数据。虽然页眉和页脚正在打印。似乎.data(结果)没有被执行。任何建议都非常感谢。控制台中没有产生错误。这是代码:

 app.post('/adminreport', function(req,res){
  var reportDate = req.body.report_Date;

  console.log("Report Date : " + reportDate);

    connection.connect(function(err){
     if(err){
       console.log('Error connecting to Db');
       return;
     }
     console.log('Connection established');
   });

   connection.query('SELECT id, fName, lName, pickUpDate FROM reservations_db WHERE pickUpDate = ?', [reportDate], function(err, results, fields){
     if (!err) 
     {
      //res.sendStatus(200).json({status:"ok"});

      console.log('The result is generated successfully!');
      console.log(results);
      //console.log(fields);

    var headerFunction = function(Report) {
      Report.print("Report By Date", {fontSize: 22, bold: true, underline:true, align: "center"});
      Report.newLine(2);
    };

    var footerFunction = function(Report) {
        Report.line(Report.currentX(), Report.maxY()-18, Report.maxX(), Report.maxY()-18);
        Report.pageNumber({text: "Page {0} of {1}", footer: true, align: "right"});
        Report.print("Report printed: "+ reportDate, {y: Report.maxY()-14, align: "left"});
    };
    //var res = function(Report) { 
    //    Report.print(results);
    //};


    var rpt = new Report("report_js.pdf")
        .margins(20)                                 // Change the Margins to 20 pixels                                  // Add Data
        .pageHeader(headerFunction)                  // Add a header
        .data(results)                               // Add Data
        .pageFooter(footerFunction)                  // Add a footer
        .render();         
      }

     else 
     {
     console.log('Error while performing persistence!'); 
     } 
    });

这是生成的pdf文件:

enter image description here

这是console.log:

Report Date : 12/09/2016
Connection established
The result is generated successfully!
[ RowDataPacket {
    id: 3,
    fName: 'ffree',
    lName: 'dgtrh',
    pickUpDate: '12/09/2016' } ]
[object Object]
It is working! 

1 个答案:

答案 0 :(得分:0)

好的,我实际上错过了一个调用.detail()的方法。以下是未来用户的代码:

app.post('/adminreport', function(req,res){
  var reportDate = req.body.report_Date;

  console.log("Report Date : " + reportDate);

    connection.connect(function(err){
     if(err){
       console.log('Error connecting to Db');
       return;
     }
     console.log('Connection established');
   });

   connection.query('SELECT id, fName, lName, pickUpDate FROM reservations_db WHERE pickUpDate = ?', 
    [reportDate], function(err, results, fields){
     if (!err) 
     {
      //res.sendStatus(200).json({status:"ok"});

      console.log('The result is generated successfully!');
      console.log(results);
      //console.log(fields);

    var headerFunction = function(Report) {
      Report.print("Report By Date", {fontSize: 22, bold: true, underline:true, align: "center"});
      Report.newLine(2);
      Report.fontBold();
      Report.band([
    {data: 'ID#', width: 50, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
    {data: 'First Name', width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
    {data: 'Last Name', width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
    {data: 'PickUp Date', width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}}
  ], {border:1, width: 0, wrap: 1} );
     Report.fontNormal();
    };

    var reportdetail = function ( Report, data ) {
    Report.band( [
        {data: data.id, width: 50, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
        {data: data.fName, width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
        {data: data.lName, width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}},
        {data: data.pickUpDate, width: 100, align: 3, zborder:{left:1, right: 1, top: 0, bottom: 1}}
    ], {border:1, width: 0, wrap: 1} );
  };

    var footerFunction = function(Report) {
        Report.line(Report.currentX(), Report.maxY()-18, Report.maxX(), Report.maxY()-18);
        Report.pageNumber({text: "Page {0} of {1}", footer: true, align: "right"});
        Report.print("Report printed: "+ reportDate, {y: Report.maxY()-14, align: "left"});
    };
    //var res = function(Report) { 
    //    Report.print(results);
    //};

    var rpt = new Report("report_js.pdf")
        .margins(20)                                 // Change the Margins to 20 pixels                                  // Add Data
        .pageHeader(headerFunction)                  // Add a header
        .data(results)                               // Add Data
        .detail(reportdetail)
        .pageFooter(footerFunction)                  // Add a footer        
        .render();         
      }

     else 
     {
     console.log('Error while performing persistence!'); 
     } 
    });

   connection.end(function(err) {
       console.log('It is working!');
    }); 

  //res.redirect('/admin');
});
相关问题