Skip to content

Commit

Permalink
fix(index): do not trim output if options.maintainLayout set (#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs authored Jan 13, 2025
1 parent e0d7ca6 commit ebc3fd0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,5 @@ test_resources/test_files/*
!test_resources/test_files/pdf_1.3_NHS_Constitution.pdf
!test_resources/test_files/pdf_1.7_NHS_Constitution_Handbook.pdf
!test_resources/test_files/pdf_1.3_NHS_Constitution_attached_detach.pdf
!test_resources/test_files/pdf_1.7_whitespace_example.pdf
!test_resources/test_files/test.txt
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,9 @@ class Poppler {
child.on("close", (code) => {
/* istanbul ignore else */
if (stdOut !== "") {
resolve(stdOut.trim());
resolve(
options.maintainLayout ? stdOut : stdOut.trim()
);
} else if (code === 0) {
resolve(errorMessages[code]);
} else if (stdErr !== "") {
Expand Down
18 changes: 18 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const { Poppler } = require("./index");

const testDirectory = `${__dirname}/../test_resources/test_files/`;
const file = `${testDirectory}pdf_1.3_NHS_Constitution.pdf`;
const whitespaceFile = `${testDirectory}pdf_1.7_whitespace_example.pdf`;

/**
* @description Returns the path to the poppler-util binaries based on the OS.
Expand Down Expand Up @@ -53,6 +54,7 @@ describe("Node-Poppler module", () => {
`${testDirectory}/pdf_1.3_NHS_Constitution.pdf`,
`${testDirectory}/pdf_1.7_NHS_Constitution_Handbook.pdf`,
`${testDirectory}/test.txt`,
whitespaceFile,
],
});

Expand Down Expand Up @@ -1220,6 +1222,22 @@ describe("Node-Poppler module", () => {
expect(res).toMatch("The NHS Constitution");
});

it("Converts PDF file to Text file and retains the original layout, including whitespace", async () => {
const poppler = new Poppler(testBinaryPath);
const options = {
maintainLayout: true,
};

const res = await poppler.pdfToText(
whitespaceFile,
undefined,
options
);

expect(res).toMatch(/^\s*/u);
expect(res).toMatch(/\s*$/u);
});

it("Accepts options and only process 2 pages of PDF file", async () => {
const poppler = new Poppler(testBinaryPath);
const outputFile = `${testDirectory}pdf_1.3_NHS_Constitution.txt`;
Expand Down
Binary file not shown.

0 comments on commit ebc3fd0

Please sign in to comment.