我试图从nodejs脚本的PDF文件中获取信息。
在执行程序时,我会得到这个错误。
Error: stream must have data
at error (eval at <anonymous> (/Users/.../node_modules/pdf2json/lib/pdf.js:60:6), <anonymous>:193:7)
....以下是代码:
http.get(url_Of_Pdf_File, function(res) {
var body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function() {
// Here body have the pdf content
pdf2table.parse(body, function (err, rows, rowsdebug) { // <-- Conflict
// Code fail executing the previous line
if(err) return console.log(err);
toMyFormat(rows, function(data){
console.log(JSON.stringify(data,null," "));
});
});
});
});我不知道为什么代码不能工作,因为如果我下载了PDF文件,然后不再使用'http.request‘方法,而是使用'fs.readFile’方法获得文件,那么代码在工作之前就可以使用了。
fs.readFile(pdf_file_path, function (err, buffer) {
if (err) return console.log(err);
pdf2table.parse(buffer, function (err, rows, rowsdebug) {
if(err) return console.log(err);
console.timeEnd("Processing time");
toMyFormat(rows, function(data){
output(JSON.stringify(rows, null, " "));
});
});
});我的问题是:
“身体”和“缓冲区”的内容在两种情况下有什么区别?
发布于 2015-08-12 01:42:25
在第一个示例中,chunk是缓冲区,通过添加空体''将其转换为utf8字符串。添加带有字符串的缓冲区时,它将转换为utf8,原始数据将丢失。
试试这个:
var chunks = [];
res.on('data', function (chunk) {
chunks.push(chunk)
});
res.on('end', function() {
// Here body have the pdf content
pdf2table.parse(Buffer.concat(chunks), function (err, rows, rowsdebug) {
//...
});
});https://stackoverflow.com/questions/31952690
复制相似问题