1
我正在嘗試使用api fluentreports在nodejs中生成一個非常簡單的報告。數據來自mysql數據庫。但是,數據不會被打印在生成的pdf文件中。雖然頁眉和頁腳正在打印。看來.data(結果)沒有被執行。任何建議,非常感謝。在控制檯中沒有產生錯誤。下面是代碼:在nodejs中生成的pdf報告中沒有打印數據
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文件:
這裏是的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!
什麼'console.logs'顯示?是否有錯誤輸出?知道哪些代碼行正在執行以及哪些代碼未被執行將會很有幫助。 – therobinkim
@therobinkim,似乎.data(結果)沒有被執行。在控制檯中沒有產生錯誤。我在問題中添加了console.log。 – JoeZ
請參閱下面的解答的答案。 – JoeZ