JSReport API。从响应中保存 PDF
JSReport API. Save PDF from Response
使用 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 一起使用时生成一个文件保存对话框,其中包含页面上的适当文本以供打印。
bug在哪里?
根据 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')));
使用 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 一起使用时生成一个文件保存对话框,其中包含页面上的适当文本以供打印。
bug在哪里?
根据 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')));