使用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,当与邮递员一起使用时,第二个块会生成一个文件保存对话框,并在页面上显示相应的文本进行打印。
错误在哪里?
答案 0 :(得分: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')));