Created
August 27, 2021 19:38
-
-
Save cfjedimaster/2e45f34fa371e64502b41e513071b808 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pdfSDK = require('@adobe/pdfservices-node-sdk'); | |
const fs = require('fs'); | |
(async ()=> { | |
const input = './hamlet2.docx'; | |
const output = './multi.pdf'; | |
const creds = './pdftools-api-credentials.json'; | |
if(fs.existsSync(output)) fs.unlinkSync(output); | |
let result = await createPDF(input, creds); | |
console.log('got result from making a pdf'); | |
result = await protectPDF(result, '12345', creds); | |
console.log('got result from password protecting pdf'); | |
await result.saveAsFile(output); | |
})(); | |
async function createPDF(source, creds) { | |
return new Promise((resolve, reject) => { | |
const credentials = pdfSDK.Credentials | |
.serviceAccountCredentialsBuilder() | |
.fromFile(creds) | |
.build(); | |
const executionContext = pdfSDK.ExecutionContext.create(credentials), | |
createPdfOperation = pdfSDK.CreatePDF.Operation.createNew(); | |
// Set operation input from a source file | |
const input = pdfSDK.FileRef.createFromLocalFile(source); | |
createPdfOperation.setInput(input); | |
// Execute the operation and Save the result to the specified location. | |
createPdfOperation.execute(executionContext) | |
.then(result => resolve(result)) | |
.catch(err => { | |
if(err instanceof PDFToolsSdk.Error.ServiceApiError | |
|| err instanceof PDFToolsSdk.Error.ServiceUsageError) { | |
reject(err); | |
} else { | |
reject(err); | |
} | |
}); | |
}); | |
} | |
async function protectPDF(source, password, creds) { | |
return new Promise((resolve, reject) => { | |
const credentials = pdfSDK.Credentials | |
.serviceAccountCredentialsBuilder() | |
.fromFile(creds) | |
.build(); | |
const executionContext = pdfSDK.ExecutionContext.create(credentials); | |
const protectPDF = pdfSDK.ProtectPDF, | |
options = new protectPDF.options.PasswordProtectOptions.Builder() | |
.setUserPassword(password) | |
.setEncryptionAlgorithm(pdfSDK.ProtectPDF.options.EncryptionAlgorithm.AES_256) | |
.build(); | |
const protectPDFOperation = protectPDF.Operation.createNew(options); | |
// Set operation input from a source file. | |
protectPDFOperation.setInput(source); | |
// Execute the operation and Save the result to the specified location. | |
protectPDFOperation.execute(executionContext) | |
.then(result => resolve(result)) | |
.catch(err => reject(err)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment