创建pdf

时间:2016-10-25 15:56:18

标签: mysql node.js pdfkit

嗨我正在尝试使用MySQL数据库中的数据编写PDF文件,但是当我运行脚本时,它给了我结束PDF文件创建,之后我试图向其插入数据.....我认为这是因为节点中的MySQL是异步的。这有什么解决方案吗? 这是我的代码......

var project_ID = 1;
var PDFDocument = require('pdfkit');
$('#pdf').click(function(e) {
    e.preventDefault();
    var pdf = new PDFDocument;
    pdf.pipe(fs.createWriteStream(__dirname + '/../MyFile.pdf'));
    var option;
    switch ($("input[name=report-type]:checked").val()) {
        case 'all-issues':
            option = {project_id: project_ID};
            break;

        case  'all-issues-customer':
            option = {project_id: project_ID, customer: customer};
            break;
    }
    connection.getConnection(function (err, conn) { //make connection to DB
        if (err) { //error handling
            showNotification('error connecting: ' + err.stack, 'danger', 'glyphicon glyphicon-tasks');
            return;
        }
        conn.query('SELECT issues.id, issues.dbid , issues.date, issues.charm , issues.defect ,issues.key , issues.status, issues.summary, issues.description , actions.date as action_date, actions.description as action_desc FROM issues' +
            ' INNER JOIN actions on issues.id = actions.issue_id WHERE ? ', [option], function (error, data) {
            if (error) {
                showNotification('Error :' + error, 'danger', 'glyphicon glyphicon-tasks');
            } else {
                var lastID = -1;
                data.forEach(function (data) {
                    if (lastID !== data.id) {
                        lastID = data.id;
                        pdf.addPage();
                        pdf.text('Number :' + data.dbid).moveDown();
                        pdf.text('Customers :            ');
                        pdf.text('Baseline :            ');
                        pdf.text('Error/Wish :            ' + data.key).moveDown();
                        pdf.text('Charm :  ' + data.charm + ' / Defect :  ' + data.defect + '                   Status : ' + data.status);
                        pdf.text('Summary :                ' + data.summary);
                        pdf.text('Description :            ' + data.description).moveDown();
                        pdf.text('Actions/ History :').moveDown();
                    }
                    pdf.text('          - date        : ' + convertDate(data.date));
                    pdf.text('            description : ' + data.description).moveDown();
                })
            }
        });
        conn.release();
    });
    pdf.end();
});

1 个答案:

答案 0 :(得分:0)

这是因为Node.js的异步性质。您可以在几个方面快速更改代码以修复它。您有conn.release()pdf.end()之类的电话会在您的回调之外落入

在下面的代码中,我将conn.release()pdf.end()移到了mysql连接回调的最后几行。

var project_ID = 1;
var PDFDocument = require('pdfkit');
$('#pdf').click(function(e) {
    e.preventDefault();
    var pdf = new PDFDocument;
    pdf.pipe(fs.createWriteStream(__dirname + '/../MyFile.pdf'));
    var option;
    switch ($("input[name=report-type]:checked").val()) {
        case 'all-issues':
            option = {project_id: project_ID};
            break;

        case  'all-issues-customer':
            option = {project_id: project_ID, customer: customer};
            break;
    }
    connection.getConnection(function (err, conn) { //make connection to DB
        if (err) { //error handling
            showNotification('error connecting: ' + err.stack, 'danger', 'glyphicon glyphicon-tasks');
            return;
        }
        conn.query('SELECT issues.id, issues.dbid , issues.date, issues.charm , issues.defect ,issues.key , issues.status, issues.summary, issues.description , actions.date as action_date, actions.description as action_desc FROM issues' +
            ' INNER JOIN actions on issues.id = actions.issue_id WHERE ? ', [option], function (error, data) {
            if (error) {
                showNotification('Error :' + error, 'danger', 'glyphicon glyphicon-tasks');
            } else {
                var lastID = -1;
                data.forEach(function (data) {
                    if (lastID !== data.id) {
                        lastID = data.id;
                        pdf.addPage();
                        pdf.text('Number :' + data.dbid).moveDown();
                        pdf.text('Customers :            ');
                        pdf.text('Baseline :            ');
                        pdf.text('Error/Wish :            ' + data.key).moveDown();
                        pdf.text('Charm :  ' + data.charm + ' / Defect :  ' + data.defect + '                   Status : ' + data.status);
                        pdf.text('Summary :                ' + data.summary);
                        pdf.text('Description :            ' + data.description).moveDown();
                        pdf.text('Actions/ History :').moveDown();
                    }
                    pdf.text('          - date        : ' + convertDate(data.date));
                    pdf.text('            description : ' + data.description).moveDown();
                })
            }
            conn.release();
            pdf.end();
        });
    });
});

这应确保在回调逻辑完成之前不会进行这些调用。 注意如果您在该回调中有回调(或更多),则通常需要确保在这些回调中调用这些函数。你经常会发现自己处于Callback Hell的中间位置。

祝你好运!