迭代数据并使用PDFKit生成页面

时间:2022-06-02 21:08:36

I want to generate pages in pdfkit using a for loop. In my application I created a new module which is loaded on a request (all this works fine and I can create a pdf without a loop). With the loop pdfkit creates a pdf, but this one is broken then.

我想使用for循环生成pdfkit中的页面。在我的应用程序中,我创建了一个新的模块,它被加载到一个请求上(所有这一切都正常,我可以创建一个没有循环的pdf)。使用循环pdfkit创建一个pdf,但是这个被破坏了。

The code from the module:

模块中的代码:

module.exports = function () {

var Project = require('./models/projects');
var PDFDocument = require('pdfkit');
var doc = new PDFDocument;

doc.fontSize(25);
doc.text('Portfolio');

Project
    .find()
    .exec(function (err, projects) {
      if (err) {
          console.log(err);
      } else {
        projects.forEach(function (project) {

            var project_customer = project.customer;

            doc.addPage();
            doc.text('project_customer');

        });
      }
    });

doc.addPage();
doc.text('project_customer');

doc.save();

    doc.write('./output.pdf');

}

Is there an solution how I can get a pdf which is not broken?

有没有一个解决方案,我怎么能得到一个没有破坏的PDF?

1 个解决方案

#1


0  

In your code Project.find().exec(...) runs asynchronously. Which means you are creating the PDF document and saving before the call to Project.find().exec(...) completes.

在您的代码中,Project.find()。exec(...)以异步方式运行。这意味着您在调用Project.find()。exec(...)之前创建PDF文档并保存。

I suggest moving the PDF code into the callback included in exec() like so:

我建议将PDF代码移动到exec()中包含的回调中,如下所示:

module.exports = function () {

    var Project = require('./models/projects');
    var PDFDocument = require('pdfkit');

    Project
      .find()
      .exec(function (err, projects) {
        if (err) return console.log(err);

        var doc = new PDFDocument;
        doc.fontSize(25);
        doc.text('Portfolio');

        projects.forEach(function (project) {

          var project_customer = project.customer;

          doc.addPage();
          doc.text('project_customer');
        });

        doc.addPage();
        doc.text('project_customer');

        doc.save();

        doc.write('./output.pdf');

      });
}

#1


0  

In your code Project.find().exec(...) runs asynchronously. Which means you are creating the PDF document and saving before the call to Project.find().exec(...) completes.

在您的代码中,Project.find()。exec(...)以异步方式运行。这意味着您在调用Project.find()。exec(...)之前创建PDF文档并保存。

I suggest moving the PDF code into the callback included in exec() like so:

我建议将PDF代码移动到exec()中包含的回调中,如下所示:

module.exports = function () {

    var Project = require('./models/projects');
    var PDFDocument = require('pdfkit');

    Project
      .find()
      .exec(function (err, projects) {
        if (err) return console.log(err);

        var doc = new PDFDocument;
        doc.fontSize(25);
        doc.text('Portfolio');

        projects.forEach(function (project) {

          var project_customer = project.customer;

          doc.addPage();
          doc.text('project_customer');
        });

        doc.addPage();
        doc.text('project_customer');

        doc.save();

        doc.write('./output.pdf');

      });
}