2017-10-05 13 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')));