2017-10-05 82 views
0

使用JSReport生成一些打印輸出,並且在保存從API返回的PDF文件時遇到了一些麻煩。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是空白,但我可以郵遞員,該報告可與下面的代碼確認:

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: '[email protected]', 
      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: '[email protected]', 
      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,當與郵遞員一起使用時,第二個塊生成文件保存對話框,並在頁面上顯示適當的文本以進行打印。

錯誤在哪裏?

回答

0

根據JSReport的管理團隊,正確的方式在使用的NodeJS這一點,要求如下:

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