Skip to content

Instantly share code, notes, and snippets.

@nemo0
Created December 18, 2024 18:44
Show Gist options
  • Save nemo0/7591ac589927276dd408c648643fe9e4 to your computer and use it in GitHub Desktop.
Save nemo0/7591ac589927276dd408c648643fe9e4 to your computer and use it in GitHub Desktop.
PDF redaction on the server using Apryse Server SDK
const { PDFNet } = require("@pdftron/pdfnet-node");
const dotenv = require("dotenv");
// Load environment variables
dotenv.config();
// Ensure API key is present
if (!process.env.APRYSE_API_KEY) {
console.error(
"Error: APRYSE_API_KEY is not defined in the environment variables."
);
process.exit(1);
}
async function applyRedaction(inputFile, outputFile) {
const doc = await PDFNet.PDFDoc.createFromFilePath(inputFile);
if (await doc.initSecurityHandler()) {
const redactionArray = [];
redactionArray.push(
await PDFNet.Redactor.redactionCreate(
2,
await PDFNet.Rect.init(100, 100, 550, 600),
false,
"Top Secret"
)
);
redactionArray.push(
await PDFNet.Redactor.redactionCreate(
3,
await PDFNet.Rect.init(0, 240, 600, 600),
false,
""
)
);
const appearance = {
redaction_overlay: true,
border: false,
show_redacted_content_regions: true,
};
PDFNet.Redactor.redact(doc, redactionArray, appearance);
// Save the redacted PDF
await doc.save(outputFile, PDFNet.SDFDoc.SaveOptions.e_linearized);
console.log(`Redacted PDF saved to ${outputFile}`);
}
}
// Run the script
(async () => {
const inputFile = "research.pdf";
const outputFile = "redacted_research.pdf";
try {
await PDFNet.runWithCleanup(
() => applyRedaction(inputFile, outputFile),
process.env.APRYSE_API_KEY
);
} catch (error) {
console.error("PDFNet initialization error:", error.message);
} finally {
await PDFNet.shutdown();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment