Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raw table data output #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ fs.readFile('./test.pdf', function (err, buffer) {
});
});

```
### Getting raw table data
X-axis data is being stripped in the default mode, but may be necessary to reconstruct the table extracted from the pdf. This can be retrieved by passing 'true' as the raw argument, which is false by default.
```
pdf2table.parse(buffer, function, raw = false)
```

## Note
Expand Down
12 changes: 6 additions & 6 deletions lib/pdf2table.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@ SOFTWARE.
var path = require('path');
var PDFParser = require("pdf2json/pdfparser");


function parse (pdfBuffer, callback) {
// will return raw cell data including x-coordinates if raw = true
function parse (pdfBuffer, callback, raw = false) {
var pdfParser = new PDFParser();

// adding try/catch/printstack 'cause pdfParser seems to prevent errors from bubbing up (weird implementation).
// It also doesn't seem to implement the callback(err, otherdata) convention used in most Node.js modules, so let's fix that here.
pdfParser.on("pdfParser_dataReady", function (data) {
try{
pdfParserCallback(null, data);
pdfParserCallback(null, data, raw);
}catch(err){
console.log(err.stack);
}
});

pdfParser.on("pdfParser_dataError", function (err) {
try{
pdfParserCallback(err, null);
pdfParserCallback(err, null, raw);
}catch(err){
console.log(err.stack);
}
});


function pdfParserCallback (err, data) {
function pdfParserCallback (err, data, raw) {
if(err) return callback(err);


Expand Down Expand Up @@ -176,7 +176,7 @@ function parse (pdfBuffer, callback) {
var rowEntries = []
var row = myPages[p][r].data;
for (var i = 0; i < row.length; i++) {
rowEntries.push(row[i].text)
rowEntries.push(raw ? row[i] : row[i].text)
}
// now append the extracted and ordered text into the return rows.
rows.push(rowEntries); };
Expand Down