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

Allow custom qpdf binary #39

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Conversation

luicfrr
Copy link

@luicfrr luicfrr commented Nov 16, 2023

With this we can pass a custom qpdf binary path.
If user wants to use source compiled or standalone versions of qpdf binaries.

In my case I needed to use the standalone Linux binary release because qpdf cannot be installed nor used as a layer.
Here's an example of usage:

await encrypt( {
  // path where qpdf binary is located
  qpdfPath: path/to/qpdf/bin,
  ... other props
})

@luicfrr
Copy link
Author

luicfrr commented Dec 6, 2023

While this don't get merged, here's a patch I've made in case of anyone need:

diff --git a/node_modules/node-qpdf2/dist/decrypt.d.ts b/node_modules/node-qpdf2/dist/decrypt.d.ts
index 32909cc..df98587 100644
--- a/node_modules/node-qpdf2/dist/decrypt.d.ts
+++ b/node_modules/node-qpdf2/dist/decrypt.d.ts
@@ -1,5 +1,7 @@
 /// <reference types="node" resolution-mode="require"/>
 export interface DecryptSettings {
+    /** Path to custom qpdf binary */
+    qpdfPath?: string
     /** The path for the encrypted pdf */
     input: string;
     /** The path for the decrypted pdf */
diff --git a/node_modules/node-qpdf2/dist/decrypt.js b/node_modules/node-qpdf2/dist/decrypt.js
index c44ca6a..d2f4994 100644
--- a/node_modules/node-qpdf2/dist/decrypt.js
+++ b/node_modules/node-qpdf2/dist/decrypt.js
@@ -24,5 +24,5 @@ export const decrypt = async (payload) => {
     else {
         callArguments.push("-");
     }
-    return execute(callArguments);
+    return execute(callArguments, payload.qpdfPath);
 };
diff --git a/node_modules/node-qpdf2/dist/encrypt.d.ts b/node_modules/node-qpdf2/dist/encrypt.d.ts
index 10d7a72..3440f50 100644
--- a/node_modules/node-qpdf2/dist/encrypt.d.ts
+++ b/node_modules/node-qpdf2/dist/encrypt.d.ts
@@ -1,5 +1,7 @@
 /// <reference types="node" resolution-mode="require"/>
 export interface EncryptOptions {
+    /** Path to custom qpdf binary */
+    qpdfPath?: string
     /** The location of the unencrypted pdf file */
     input: string;
     /**
@@ -43,7 +45,7 @@ export interface EncryptOptions {
         /** Please see: https://qpdf.readthedocs.io/en/stable/cli.html#option-modify-other */
         modifyOther?: "y" | "n";
         /** Please see: https://qpdf.readthedocs.io/en/stable/cli.html#option-print */
-        print?: "y" | "n" | "full" | "low" | "none";
+        print?: "full" | "low" | "none";
         /** Please see: https://qpdf.readthedocs.io/en/stable/cli.html#option-use-aes */
         useAes?: "y" | "n";
     };
diff --git a/node_modules/node-qpdf2/dist/encrypt.js b/node_modules/node-qpdf2/dist/encrypt.js
index 3009281..f0064ee 100644
--- a/node_modules/node-qpdf2/dist/encrypt.js
+++ b/node_modules/node-qpdf2/dist/encrypt.js
@@ -21,9 +21,9 @@ export const encrypt = async (userPayload) => {
     if (payload.output && !payload.overwrite && fileExists(payload.output))
         throw new Error("Output file already exists");
     const callArguments = [];
-    // If the keyLength is 40, `--allow-weak-crypto` needs to be specified before `--encrypt`.
+    // If the keyLength is not 256, `--allow-weak-crypto` needs to be specified before `--encrypt`.
     // This is required for qpdf 11+.
-    if (payload.keyLength === 40)
+    if (payload.keyLength !== 256)
         callArguments.push("--allow-weak-crypto");
     callArguments.push("--encrypt");
     // Set user-password and owner-password
@@ -78,5 +78,5 @@ export const encrypt = async (userPayload) => {
         callArguments.push("-");
     }
     // Execute command and return stdout for pipe
-    return execute(callArguments);
+    return execute(callArguments, payload.qpdfPath);
 };
diff --git a/node_modules/node-qpdf2/dist/info.d.ts b/node_modules/node-qpdf2/dist/info.d.ts
index 263ded6..a1877b0 100644
--- a/node_modules/node-qpdf2/dist/info.d.ts
+++ b/node_modules/node-qpdf2/dist/info.d.ts
@@ -1,4 +1,6 @@
 export interface InfoSettings {
+    /** Path to custom qpdf binary */
+    qpdfPath?: string
     /** The path for the encrypted pdf */
     input: string;
     /** The password for the encrypted pdf */
diff --git a/node_modules/node-qpdf2/dist/info.js b/node_modules/node-qpdf2/dist/info.js
index 7424797..b65020c 100644
--- a/node_modules/node-qpdf2/dist/info.js
+++ b/node_modules/node-qpdf2/dist/info.js
@@ -17,6 +17,6 @@ export const info = async (payload) => {
     }
     // Input file path
     callArguments.push(payload.input);
-    const result = await execute(callArguments);
+    const result = await execute(callArguments, payload.qpdfPath);
     return result.toLocaleString().trim();
 };
diff --git a/node_modules/node-qpdf2/dist/spawn.d.ts b/node_modules/node-qpdf2/dist/spawn.d.ts
index 2b93ec5..47fa939 100644
--- a/node_modules/node-qpdf2/dist/spawn.d.ts
+++ b/node_modules/node-qpdf2/dist/spawn.d.ts
@@ -1,3 +1,3 @@
 /// <reference types="node" resolution-mode="require"/>
-declare const _default: (callArguments: string[]) => Promise<Buffer>;
-export default _default;
+declare const _default: ( callArguments: string[], qpdfPath?: string ) => Promise<Buffer>
+export default _default
diff --git a/node_modules/node-qpdf2/dist/spawn.js b/node_modules/node-qpdf2/dist/spawn.js
index 21377c8..dc594e2 100644
--- a/node_modules/node-qpdf2/dist/spawn.js
+++ b/node_modules/node-qpdf2/dist/spawn.js
@@ -1,8 +1,10 @@
 // Ignore errors about unsafe-arguments, this is because data is unknown
 /* eslint-disable @typescript-eslint/no-unsafe-argument */
 import { spawn } from "node:child_process";
-export default (callArguments) => new Promise((resolve, reject) => {
-    const process = spawn("qpdf", callArguments);
+export default (callArguments, qpdfPath) => new Promise((resolve, reject) => {
+    const process = spawn(qpdfPath? "./qpdf" : "qpdf", callArguments, {
+      cwd: qpdfPath
+    });
     const stdout = [];
     const stderr = [];
     process.stdout.on("data", (data) => {

@luicfrr
Copy link
Author

luicfrr commented Dec 6, 2023

@Sparticuz Can you please take a look to merge this?
Thanks 😁

@Sparticuz
Copy link
Owner

Sparticuz commented Dec 6, 2023

Thanks for the PR! Sorry it was a little bit before I could look at it.

My main question is this. Can't you just add that directory to PATH?

EDIT: what I mean by that is something like this

process.env["PATH"] = `${process.env["PATH"]}:${path.join(process.cwd(),"qpdf_dir","bin")}`

Copy link
Owner

@Sparticuz Sparticuz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a few notes. Thanks!

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
src/spawn.ts Show resolved Hide resolved
@luicfrr
Copy link
Author

luicfrr commented Dec 6, 2023

Thanks for the PR! Sorry it was a little bit before I could look at it.

My main question is this. Can't you just add that directory to PATH?

EDIT: what I mean by that is something like this

process.env["PATH"] = `${process.env["PATH"]}:${path.join(process.cwd(),"qpdf_dir","bin")}`

I'll see if can manage it to work using your suggestion.
At this moment I'm using the pacakge with PR changes and it's working nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants