Skip to content

Instantly share code, notes, and snippets.

@cfjedimaster
Created August 2, 2024 15:52
Show Gist options
  • Save cfjedimaster/a233782a6ab67a4667d7afc4dad7f84f to your computer and use it in GitHub Desktop.
Save cfjedimaster/a233782a6ab67a4667d7afc4dad7f84f to your computer and use it in GitHub Desktop.
import {
ServicePrincipalCredentials,
PDFServices,
MimeType,
CreatePDFJob,
CreatePDFResult,
ProtectPDFParams,
EncryptionAlgorithm,
ProtectPDFJob,
ProtectPDFResult
} from '@adobe/pdfservices-node-sdk';
import fs from 'fs';
let CLIENT_ID = process.env.CLIENT_ID;
let CLIENT_SECRET = process.env.CLIENT_SECRET;
// Setup credentials
let credentials = new ServicePrincipalCredentials({ clientId: CLIENT_ID, clientSecret: CLIENT_SECRET });
let pdfServices = new PDFServices({ credentials });
const inputAsset = await pdfServices.upload({
readStream:fs.createReadStream('./input.docx'),
mimeType: MimeType.DOCX
});
console.log(`Source doc uploaded, asset ID is ${inputAsset.assetId}`);
let job = new CreatePDFJob({inputAsset});
let pollingURL = await pdfServices.submit({job});
console.log(`Create PDF job begun, polling url is ${pollingURL}`);
let response = await pdfServices.getJobResult({ pollingURL, resultType: CreatePDFResult });
console.log(`PDF created, now adding password protection.`);
const params = new ProtectPDFParams({
userPassword: "password",
encryptionAlgorithm: EncryptionAlgorithm.AES_256
});
// Create a new job instance
job = new ProtectPDFJob({inputAsset:response.result.asset, params});
pollingURL = await pdfServices.submit({job});
response = await pdfServices.getJobResult({
pollingURL,
resultType: ProtectPDFResult
});
// Get content from the resulting asset(s)
let streamAsset = await pdfServices.getContent({ asset: response.result.asset});
streamAsset.readStream.pipe(fs.createWriteStream('./output_protected.pdf'));
console.log('All done and result PDF saved.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment