Node Express从二进制字符串中保存pdf [英] Node express save pdf from binary string
问题描述
从二进制数据保存pdf文件时出现问题.我从Web服务获取二进制文件,而我的快速服务器(作为中间件)应将文件发送给客户端. 问题是客户端和Adobe Acrobat Reader显示空白的pdf,所以我认为我在保存/编码二进制数据时做过一些错误.
exports.downloadReceipt = function(req, res) {
var idPrenotazione = req.params.idPrenotazione;
var options = {
'method': 'GET',
'uri': api_url + '/ricevute/'+idPrenotazione,
'headers': {
'Authorization': req.get('Authorization')
}
}
request(options, function(error, response, body) {
var date = new Date().getTime();
var filename = 'ricevuta_'+idPrenotazione+'_'+date+'.pdf';
var file = folderPath + filename;
fs.writeFile(file, body, function(err) {
if(err)
console.log(err);
else
console.log("The file was saved!");
});
// for the moment I only save the file on server file system
res.end();
});
}
我还尝试使用createWriteStream代替writeFile,无论是否带有编码选项
var options = { encoding: 'binary' };
var wstream = fs.createWriteStream(file_);
wstream.write(body);
wstream.end();
我从Web服务获得的字符串是这样的:
%PDF-1.5
%����
2 0 obj
<</Length1 17948/Length 9587/Filter/FlateDecode>>stream
x��{y`՝���!ɖ-Y�iɖF�e˖,۲lَ��v�$���s8v��B�$�(4nHiI�RP��@�n��������-P�-Wbi����H��������y��|���^��}3�p9� -[3�~�+���< ��l�E���E7�7��Њ�+�l\�1��?P��rt�
�G��!}ؿj���ɿ~�@�+H߰
o�t�&��u��5m�??ن�5�~���e�����й���3����#��~���ԷX��%����������
k\��\z���d���O�x@�A9(-�A>�@`�B0� �`+ؠ���Q��\ �<��R�A��*��*A5�@-��"P
�Fh�f�-�
[.....]
0000042111 00000 n
0000042252 00000 n
0000042393 00000 n
trailer
<</Root 7 0 R/ID [<10edca6daaad5a49919bad108ba77f0a><492e2d9a8ca810421f41667217724e69>]/Info 4 0 R/Size 183>>
%iText-5.5.8
startxref
173008
%%EOF
我做错了什么?我只是有一个在base64中获取图像(从相同的Web服务获得)的函数,并使用writeFile将其保存在服务器文件系统中,并且效果很好.
感谢帮助
我发现了问题!我需要在请求选项和writeFile中指定'encoding': 'binary'
:fs.writeFile(file, body, 'binary', function(err) {
.
现在,我可以从二进制字符串中正确打开并发送图像和pdf.
I have a problem saving a pdf file from binary data. I get the binary from a web service and my express server (as middleware) should send the file to the client. The problem is that client and adobe acrobat reader show a blank pdf, so I'm thinking that I done some error saving/encoding the binary data.
exports.downloadReceipt = function(req, res) {
var idPrenotazione = req.params.idPrenotazione;
var options = {
'method': 'GET',
'uri': api_url + '/ricevute/'+idPrenotazione,
'headers': {
'Authorization': req.get('Authorization')
}
}
request(options, function(error, response, body) {
var date = new Date().getTime();
var filename = 'ricevuta_'+idPrenotazione+'_'+date+'.pdf';
var file = folderPath + filename;
fs.writeFile(file, body, function(err) {
if(err)
console.log(err);
else
console.log("The file was saved!");
});
// for the moment I only save the file on server file system
res.end();
});
}
I also tried to use createWriteStream instead of writeFile, with and without encoding option
var options = { encoding: 'binary' };
var wstream = fs.createWriteStream(file_);
wstream.write(body);
wstream.end();
The string that I get from web service is something like this:
%PDF-1.5
%����
2 0 obj
<</Length1 17948/Length 9587/Filter/FlateDecode>>stream
x��{y`՝���!ɖ-Y�iɖF�e˖,۲lَ��v�$���s8v��B�$�(4nHiI�RP��@�n��������-P�-Wbi����H��������y��|���^��}3�p9� -[3�~�+���< ��l�E���E7�7��Њ�+�l\�1��?P��rt�
�G��!}ؿj���ɿ~�@�+H߰
o�t�&��u��5m�??ن�5�~���e�����й���3����#��~���ԷX��%����������
k\��\z���d���O�x@�A9(-�A>�@`�B0� �`+ؠ���Q��\ �<��R�A��*��*A5�@-��"P
�Fh�f�-�
[.....]
0000042111 00000 n
0000042252 00000 n
0000042393 00000 n
trailer
<</Root 7 0 R/ID [<10edca6daaad5a49919bad108ba77f0a><492e2d9a8ca810421f41667217724e69>]/Info 4 0 R/Size 183>>
%iText-5.5.8
startxref
173008
%%EOF
What I'm doing wrong? I just have a function that get an image (from the same web service) in base64 and I save it on server file system with writeFile and it works well.
Thanks for help
I found the problem! I needed to specify 'encoding': 'binary'
in request options and in writeFile: fs.writeFile(file, body, 'binary', function(err) {
.
Now I can open and send correctly image and pdf from binary string.
这篇关于Node Express从二进制字符串中保存pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!