0

使用 JSReport 生成一些打印输出,我在保存从 API 返回的 PDF 文件时遇到了一些问题。

我正在使用此代码保存文件:

var options = { method: 'POST',
        url: 'http://192.168.100.64:5488/api/report',
        headers:
         { 'postman-token': '81e5ced9-d7b1-80bd-5948-a6934e67d4ae',
           'cache-control': 'no-cache',
           'content-type': 'application/json' },
        body:
         { template: { shortid: 'HJsjiZhob' },
           data:
            { Badges: badges },
           options: { 'Content-Disposition': 'Attachment; filename=badges.pdf' } },
        json: true };

      rq(options, function (error, response, body) {
        if (error) throw new Error(error);
        console.dir(body);
        // fs.writeFileSync(path.join(config.outFolder, 'badges.pdf'), body, 'binary');
        // console.log('Wrote PDF File');
        fs.writeFile(path.join(config.outFolder, 'badges.pdf'), body, 'binary', (err) => {
          if(err) log.error(err);
          log.info('Successfully Wrote Badge Sheet.');
        });
      });

但 PDF 为空白,但我可以通过 PostMan 确认该报告适用于以下代码:

var options = { method: 'POST',
  url: 'http://192.168.100.64:5488/api/report',
  headers: 
   { 'postman-token': '81e5ced9-d7b1-80bd-5948-a6934e67d4ae',
     'cache-control': 'no-cache',
     'content-type': 'application/json' },
  body: 
   { template: { shortid: 'HJsjiZhob' },
     data: 
      { Badges: 
         [ { Event: 'Event Name',
             Email: 'someone@somewhere.com',
             Attended: '',
             'First Timer': '',
             'Last Name': '---',
             Name: 'Jim',
             Address: 'AddressLine',
             'City, State Zipcode': 'Charleston, WV 25311',
             City: 'Charleston,',
             State: 'WV',
             zipcode: '25311' } ] },
     options: { 'Content-Disposition': 'Attachment; filename=badges.pdf' } },
  json: true };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
var options = { method: 'POST',
  url: 'http://192.168.100.64:5488/api/report',
  headers: 
   { 'postman-token': '81e5ced9-d7b1-80bd-5948-a6934e67d4ae',
     'cache-control': 'no-cache',
     'content-type': 'application/json' },
  body: 
   { template: { shortid: 'HJsjiZhob' },
     data: 
      { Badges: 
         [ { Event: '2017 West Central Regional Forum',
             Email: 'Jimwithrow@wvbaldy.com',
             Attended: '',
             'First Timer': '',
             'Last Name': 'Withrow',
             Name: 'Jim',
             Address: '1578 Kanawha Blvd., Apt. # E 8C',
             'City, State Zipcode': 'Charleston, WV 25311',
             City: 'Charleston,',
             State: 'WV',
             zipcode: '25311' } ] },
     options: { 'Content-Disposition': 'Attachment; filename=badges.pdf' } },
  json: true };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

第一个代码块将文件保存为具有多页的空白 PDF,当与邮递员一起使用时,第二个代码块会在页面上生成一个文件保存对话框,其中包含适当的文本以供打印。

错误在哪里?

4

1 回答 1

0

根据 JSReport 的管理团队,在 Nodejs 和 Request 中使用它的正确方法如下:

  var options = { method: 'POST',
    url: 'http://192.168.100.64:5488/api/report',
    headers:
     { 'postman-token': '81e5ced9-d7b1-80bd-5948-a6934e67d4ae',
       'cache-control': 'no-cache',
       'content-type': 'application/json' },
    body:
     { template: { shortid: 'HJsjiZhob' },
       data:
        { Badges: badges },
       options: { 'Content-Disposition': 'Attachment; filename=badges.pdf' } },
    json: true };
    rq(options)
      .on('response', (response) => {
        console.log(response.statusCode);
        console.log(response.headers['content-type']);
      })
      .on('error', (err) => {throw new Errror(err)})
      .on('end', () => log.info('Successfully Wrote Badge Sheet'))
      .pipe(fs.createWriteStream(path.join(config.outFolder, 'badges.pdf')));
于 2017-10-13T16:04:34.813 回答