2017-10-05 18 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として保存し、2番目のブロックはpostmanと一緒に使用するとページ上の適切なテキストを含むファイル保存ダイアログを生成して印刷します。

バグはどこですか?

答えて

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'))); 
関連する問題