如何使用jsreport和jsreport Chrome Pdf渲染1k pdf文件?

时间:2019-04-08 20:38:11

标签: javascript node.js pdf puppeteer jsreport

我试图能够使用Express.js和终结点生成1k报告,我在JSON中传递数组,API遍历它并在forEach循环中使用,然后使用每个对象来抓取门户,获取响应并创建PDF文件...

这种方法是伪方法,但是我很确定存在一些并发问题...因为,如果我在JSON数组中传递2个项目,那么API可以毫无问题地创建2个PDF文件,但是如果我通过300 API会随机创建50 ...或60或120。

这是我的jsreport配置

const jsReportConfig = {
  extensions: {
    "chrome-pdf": {
      launchOptions: {
        timeout: 10000,
        args: ['--no-sandbox', '--disable-setuid-sandbox'],
      },
    },
  },
  tempDirectory: path.resolve(__dirname, './../../temporal/pdfs'),
  templatingEngines: {
    numberOfWorkers: 4,
    timeout: 180000,
    strategy: 'http-server',
  },
};

我这样设置jsreport实例

jsreport.use(jsReportChrome());
jsreport.use(jsReportHandlebars());
jsreport.init()

而且,这就是我呈现报告的方式,checkInvoiceStatus函数用作HTTP调用,返回返回到Handlebars模板中的HTML响应。

const renderReports = (reporter, invoices) => new Promise(async (resolve, reject) => {
    try {
      const templateContent = await readFile(
        path.resolve(__dirname, './../templates/hello-world.hbs'),
        'utf-8',
      );
      invoices.forEach(async (invoice) => {
        try {
          const response = await checkInvoiceStatus(invoice.re, invoice.rr, invoice.id)
          const $ = await cheerio.load(response);
          const reporterResponse = await reporter.render({
            template: {
              content: templateContent,
              engine: 'handlebars',
              recipe: 'chrome-pdf',
              name: 'PDF Validation',
              chrome: {
                displayHeaderFooter: true,
                footerTemplate: '<table width=\'100%\' style="font-size: 12px;"><tr><td width=\'33.33%\'>{#pageNum} de {#numPages}</td><td width=\'33.33%\' align=\'center\'></td><td width=\'33.33%\' align=\'right\'></td></tr></table>',
              },
            },
            data: {
              taxpayerId: 'CAC070508MY2',
              captcha: $('#ctl00_MainContent_ImgCaptcha').attr('src'),
              bodyContent: $('#ctl00_MainContent_PnlResultados').html(),
            },
          });
          reporterResponse.result.pipe(fs.createWriteStream(`./temporal/validatedPdfs/${invoice.id}.pdf`));
        } catch (err) {
          console.error(err);
          reject(new Error(JSON.stringify({
            code: 'PORTAL-PDFx001',
            message: 'The server could not retrieve the PDF from the portal',
          })));
        }
      });
      resolve();
    } catch (err) {
      console.error(err);
      reject(new Error(JSON.stringify({
        code: 'PORTAL-PDFx001',
        message: 'The server could not retrieve the PDF from the portal',
      })));
    }
  });

我不知道为什么,但是此功能在 500ms 中终止,但是文件是在1分钟后创建的...

app.post('/pdf-report', async (req, res, next) => {
  const { invoices } = req.body;
  repository.renderReports(reporter, invoices)
    .then(() => res.status(200).send('Ok'))
    .catch((err) => {
      res.status(500).send(err);
    });
});

更新

在@hurricane提供的代码旁边,我不得不将jsReport配置更改为此

const jsReportConfig = {
  chrome: {
    timeout: 180000,
    strategy: 'chrome-pool',
    numberOfWorkers: 4
  },
  extensions: {
    'chrome-pdf': {
      launchOptions: {
        timeout: 180000,
        args: ['--no-sandbox', '--disable-setuid-sandbox'],
        ignoreDefaultArgs: ['--disable-extensions'],
      },
    },
  },
  tempDirectory: path.resolve(__dirname, './../../temporal/pdfs'),
  templatingEngines: {
    numberOfWorkers: 4,
    timeout: 180000,
    strategy: 'http-server',
  },
};

1 个答案:

答案 0 :(得分:1)

我认为在您的结构中,使用writeFileSync函数而不是使用writeStream可以轻松解决问题。但这并不意味着这是最好的方法。因为您必须等待每个渲染和每个写入文件进程开始另一个进程。因此,我建议使用Promise.all,以便您可以同时运行较长的进程。这意味着您将只等待最长的过程。

快速获胜-进度缓慢

reporterResponse.result.pipe 

使用

更改此行
   fs.createFileSync(`./temporal/validatedPdfs/${invoice.id}.pdf`);

更好的方法-快速的过程

const renderReports = (reporter, invoices) => new Promise(async (resolve, reject) => {
  try {
    const templateContent = await readFile(
      path.resolve(__dirname, './../templates/hello-world.hbs'),
      'utf-8',
    );
    const renderPromises = invoices.map((invoice) => {
      const response = await checkInvoiceStatus(invoice.re, invoice.rr, invoice.id)
      const $ = await cheerio.load(response);
      return await reporter.render({
        template: {
          content: templateContent,
          engine: 'handlebars',
          recipe: 'chrome-pdf',
          name: 'PDF Validation',
          chrome: {
            displayHeaderFooter: true,
            footerTemplate: '<table width=\'100%\' style="font-size: 12px;"><tr><td width=\'33.33%\'>{#pageNum} de {#numPages}</td><td width=\'33.33%\' align=\'center\'></td><td width=\'33.33%\' align=\'right\'></td></tr></table>',
          },
        },
        data: {
          taxpayerId: 'CAC070508MY2',
          captcha: $('#ctl00_MainContent_ImgCaptcha').attr('src'),
          bodyContent: $('#ctl00_MainContent_PnlResultados').html(),
        },
      });
    });
    const renderResults = await Promise.all(renderPromises);
    const filewritePromises = results.map(renderedResult => await fs.writeFile(`./temporal/validatedPdfs/${invoice.id}.pdf`, renderedResult.content));
    const writeResults = await Promise.all(filewritePromises);
    resolve(writeResults);
  } catch (err) {
    console.error(err);
    reject(new Error(JSON.stringify({
      code: 'PORTAL-PDFx001',
      message: 'The server could not retrieve the PDF from the portal',
    })));
  }
});