Skip to content

Commit

Permalink
fix(index): set correct "file size" if pdfInfo() passed a buffer (#427
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Fdawgs committed Jun 3, 2022
1 parent ef89aa6 commit 8d30764
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
14 changes: 14 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,16 @@ class Poppler {
versionInfo
);

/**
* Poppler does not set the "File size" metadata value if passed
* a Buffer via stdin, so need to retrieve it from the Buffer
*/
let fileSize;

return new Promise((resolve, reject) => {
if (Buffer.isBuffer(file)) {
args.push("-");
fileSize = file.length;
} else {
args.push(file);
}
Expand Down Expand Up @@ -469,6 +476,13 @@ class Poppler {

child.on("close", async () => {
if (stdOut !== "") {
if (fileSize) {
stdOut = stdOut.replace(
/(File\s+size:\s+)0(\s+)bytes/,
`$1${fileSize}$2bytes`
);
}

if (options.printAsJson === true) {
/**
* Thanks to @sainf for this solution
Expand Down
41 changes: 27 additions & 14 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,21 @@ describe("Node-Poppler Module", () => {
});

describe("pdfInfo Function", () => {
const pdfInfoObject = {
tagged: "yes",
userProperties: "no",
suspects: "no",
form: "AcroForm",
javaScript: "no",
pages: "16",
encrypted: "no",
pageSize: "595.276 x 841.89 pts (A4)",
pageRot: "0",
fileSize: "583094 bytes",
optimized: "no",
pdfVersion: "1.3",
};

test("Should list info of PDF file", async () => {
const poppler = new Poppler(testBinaryPath);

Expand All @@ -379,20 +394,7 @@ describe("Node-Poppler Module", () => {
printAsJson: true,
});

expect(res).toMatchObject({
tagged: "yes",
userProperties: "no",
suspects: "no",
form: "AcroForm",
javaScript: "no",
pages: "16",
encrypted: "no",
pageSize: "595.276 x 841.89 pts (A4)",
pageRot: "0",
fileSize: "583094 bytes",
optimized: "no",
pdfVersion: "1.3",
});
expect(res).toMatchObject(pdfInfoObject);
});

test("Should list info of PDF file as Buffer", async () => {
Expand All @@ -404,6 +406,17 @@ describe("Node-Poppler Module", () => {
expect(res).toEqual(expect.stringContaining("Pages:"));
});

test("Should list info of PDF file as Buffer as a JSON object", async () => {
const poppler = new Poppler(testBinaryPath);
const attachmentFile = await fs.promises.readFile(file);

const res = await poppler.pdfInfo(attachmentFile, {
printAsJson: true,
});

expect(res).toMatchObject(pdfInfoObject);
});

test("Should return an Error object if file passed not PDF format", async () => {
const poppler = new Poppler(testBinaryPath);
const testTxtFile = `${testDirectory}test.txt`;
Expand Down

0 comments on commit 8d30764

Please sign in to comment.