【问题标题】:JSReport API. Save PDF from ResponseJS 报告 API。从响应中保存 PDF
【发布时间】:2017-10-05 17:37:18
【问题描述】:

使用 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,第二个代码块与 postman 一起使用时会生成一个文件保存对话框,其中包含用于打印的页面上的适当文本。

错误在哪里?

【问题讨论】:

    标签: node.js pdf jsreport requestjs


    【解决方案1】:

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

      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')));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      • 1970-01-01
      相关资源
      最近更新 更多