Edit in GitHubLog an issue

Quickstart for Adobe PDF Electronic Seal API (Node.js)

To get started using Adobe PDF Electronic Seal API, let's walk through a simple scenario - Applying an electronic seal on an invoice PDF document. In this guide, we will walk you through the complete process for creating a program that will accomplish this task.

Prerequisites

To complete this guide, you will need:

  • Node.js - Node.js version 18.0 or higher is required.
  • An Adobe ID. If you do not have one, the credential setup will walk you through creating one.
  • A way to edit code. No specific editor is required for this guide.

Step One: Getting credentials

1) To begin, open your browser to https://acrobatservices.adobe.com/dc-integration-creation-app-cdn/main.html?api=pdf-services-api. If you are not already logged in to Adobe.com, you will need to sign in or create a new user. Using a personal email account is recommended and not a federated ID.

Sign in

2) After registering or logging in, you will then be asked to name your new credentials. Use the name, "New Project".

3) Change the "Choose language" setting to "Node.js".

4) Also note the checkbox by, "Create personalized code sample." This will include a large set of samples along with your credentials. These can be helpful for learning more later.

5) Click the checkbox saying you agree to the developer terms and then click "Create credentials."

Project setup

6) After your credentials are created, they are automatically downloaded:

alt

Step Two: Setting up the project

1) In your Downloads folder, find the ZIP file with your credentials: PDFServicesSDK-Node.jsSamples.zip. If you unzip that archive, you will find a folder of samples and the pdfservices-api-credentials.json file.

alt

2) Take the pdfservices-api-credentials.json and place it in a new directory. Remember that these credential files are important and should be stored safely.

3) At the command line, change to the directory you created, and initialize a new Node.js project with npm init -y

alt

4) Install the Adobe PDF Services Node.js SDK by typing npm install --save @adobe/pdfservices-node-sdk at the command line.

5) Install a package to help us work with ZIP files. Type npm install --save adm-zip.

At this point, we've installed the Node.js SDK for Adobe PDF Services API as a dependency for our project and have copied over our credentials files.

Our application will take an Invoice PDF document, sampleInvoice.pdf (downloadable from here), and will use the sealing options with default appearance options to apply electronic seal over the PDF document by invoking Acrobat Services API and generate an electronically sealed PDF.

6) In your editor, open the directory where you previously copied the credentials. Create a new file, electronic-seal.js.

Now you're ready to begin coding.

Step Three: Creating the application

1) We'll begin by including our required dependencies:

Copied to your clipboard
1const {
2 ServicePrincipalCredentials,
3 PDFServices,
4 MimeType,
5 FieldLocation,
6 FieldOptions,
7 CSCAuthContext,
8 CSCCredential,
9 PDFElectronicSealParams,
10 PDFElectronicSealJob,
11 PDFElectronicSealResult,
12 AppearanceOptions,
13 AppearanceItem,
14 SDKError,
15 ServiceUsageError,
16 ServiceApiError, DocumentLevelPermission
17} = require("@adobe/pdfservices-node-sdk");
18const fs = require("fs");

2) Set the environment variables PDF_SERVICES_CLIENT_ID and PDF_SERVICES_CLIENT_SECRET by running the following commands and replacing placeholders YOUR CLIENT ID and YOUR CLIENT SECRET with the credentials present in pdfservices-api-credentials.json file:

  • Windows:

    • set PDF_SERVICES_CLIENT_ID=<YOUR CLIENT ID>
    • set PDF_SERVICES_CLIENT_SECRET=<YOUR CLIENT SECRET>
  • MacOS/Linux:

    • export PDF_SERVICES_CLIENT_ID=<YOUR CLIENT ID>
    • export PDF_SERVICES_CLIENT_SECRET=<YOUR CLIENT SECRET>

3) Next, we setup the SDK to use our credentials.

Copied to your clipboard
1// Initial setup, create credentials instance
2const credentials = new ServicePrincipalCredentials({
3 clientId: process.env.PDF_SERVICES_CLIENT_ID,
4 clientSecret: process.env.PDF_SERVICES_CLIENT_SECRET
5});
6
7// Creates a PDF Services instance
8const pdfServices = new PDFServices({credentials});

4) Now, let's upload the assets:

Copied to your clipboard
1const [sourceFileAsset, sealImageAsset] = await pdfServices.uploadAssets({
2 streamAssets: [{
3 readStream: sourceFileReadStream,
4 mimeType: MimeType.PDF
5 }, {
6 readStream: sealImageReadStream,
7 mimeType: MimeType.PNG
8 }]
9});

5) Now, we will define the document level permission:

Copied to your clipboard
1// Set the document level permission to be applied for output document
2const documentLevelPermission = DocumentLevelPermission.FORM_FILLING;

6) Now, we will define seal field options:

Copied to your clipboard
1// Create AppearanceOptions and add the required signature appearance items
2const sealAppearanceOptions = new AppearanceOptions({
3 items: [
4 AppearanceItem.DATE,
5 AppearanceItem.SEAL_IMAGE,
6 AppearanceItem.NAME,
7 AppearanceItem.LABELS,
8 AppearanceItem.DISTINGUISHED_NAME
9 ]
10});
11
12// Set the Seal Field Name to be created in input PDF document
13const sealFieldName = "Signature1";
14
15// Set the page number in input document for applying seal
16const sealPageNumber = 1;
17
18// Set if seal should be visible or invisible
19const sealVisible = true;
20
21// Create FieldLocation instance and set the coordinates for applying signature
22const fieldLocation = new FieldLocation({
23 left: 150,
24 top: 250,
25 right: 350,
26 bottom: 200
27});
28
29// Create FieldOptions instance with required details
30const sealFieldOptions = new FieldOptions({
31 visible: sealVisible,
32 location: fieldLocation,
33 fieldName: sealFieldName,
34 pageNumber: sealPageNumber,
35});

7) Next, we create a CSC Certificate Credentials instance:

Copied to your clipboard
1// Set the name of TSP Provider being used
2const providerName = "<PROVIDER_NAME>";
3
4// Set the access token to be used to access TSP provider hosted APIs
5const accessToken = "<ACCESS_TOKEN>";
6
7// Set the credential ID
8const credentialId = "<CREDENTIAL_ID>";
9
10// Set the PIN generated while creating credentials
11const pin = "<PIN>";
12
13// Create CSCAuthContext instance using access token and token type
14const authorizationContext = new CSCAuthContext({
15 accessToken,
16 tokenType: "Bearer"
17});
18
19// Create CertificateCredentials instance with required certificate details
20const certificateCredentials = new CSCCredential({
21 providerName,
22 credentialId,
23 pin,
24 authorizationContext,
25});

8) Now, let's create the job with seal parameters using certificate credentials and field options and set the seal image asset:

Copied to your clipboard
1// Create parameters for the job
2const params = new PDFElectronicSealParams({
3 documentLevelPermission
4 certificateCredentials,
5 sealFieldOptions,
6 sealAppearanceOptions
7});
8
9// Creates a new job instance
10const job = new PDFElectronicSealJob({
11 inputAsset: sourceFileAsset,
12 sealImageAsset,
13 params,
14});

This set of code defines what we're doing (an Electronic Seal operation), it defines parameters for the seal job and sets input seal image asset.

9) The next code block submits the job and gets the job result:

Copied to your clipboard
1// Submit the job and get the job result
2const pollingURL = await pdfServices.submit({job});
3const pdfServicesResponse = await pdfServices.getJobResult({
4 pollingURL,
5 resultType: PDFElectronicSealResult
6});
7
8// Get content from the resulting asset(s)
9const resultAsset = pdfServicesResponse.result.asset;
10const streamAsset = await pdfServices.getContent({asset: resultAsset});

10) The next code block saves the result at the specified location:

Copied to your clipboard
1// Creates a write stream and copy stream asset's content to it
2const outputFilePath = "./sealedOutput.pdf";
3console.log(`Saving asset at ${outputFilePath}`);
4
5const writeStream = fs.createWriteStream(outputFilePath);
6streamAsset.readStream.pipe(writeStream);

Here's the complete application (electronic-seal.js):

Copied to your clipboard
1const {
2 ServicePrincipalCredentials,
3 PDFServices,
4 MimeType,
5 FieldLocation,
6 FieldOptions,
7 CSCAuthContext,
8 CSCCredential,
9 PDFElectronicSealParams,
10 PDFElectronicSealJob,
11 PDFElectronicSealResult,
12 AppearanceOptions,
13 AppearanceItem,
14 SDKError,
15 ServiceUsageError,
16 ServiceApiError, DocumentLevelPermission
17} = require("@adobe/pdfservices-node-sdk");
18const fs = require("fs");
19
20(async () => {
21
22 let sourceFileReadStream;
23 let sealImageReadStream;
24 try {
25 // Initial setup, create credentials instance
26 const credentials = new ServicePrincipalCredentials({
27 clientId: process.env.PDF_SERVICES_CLIENT_ID,
28 clientSecret: process.env.PDF_SERVICES_CLIENT_SECRET
29 });
30
31 // Creates a PDF Services instance
32 const pdfServices = new PDFServices({credentials});
33
34 // Creates an asset(s) from source file(s) and upload
35 sourceFileReadStream = fs.createReadStream("./sampleInvoice.pdf")
36 sealImageReadStream = fs.createReadStream("./sampleSealImage.png");
37 const [sourceFileAsset, sealImageAsset] = await pdfServices.uploadAssets({
38 streamAssets: [{
39 readStream: sourceFileReadStream,
40 mimeType: MimeType.PDF
41 }, {
42 readStream: sealImageReadStream,
43 mimeType: MimeType.PNG
44 }]
45 });
46
47 // Set the document level permission to be applied for output document
48 const documentLevelPermission = DocumentLevelPermission.FORM_FILLING;
49
50 // Create AppearanceOptions and add the required signature appearance items
51 const sealAppearanceOptions = new AppearanceOptions({
52 items: [
53 AppearanceItem.DATE,
54 AppearanceItem.SEAL_IMAGE,
55 AppearanceItem.NAME,
56 AppearanceItem.LABELS,
57 AppearanceItem.DISTINGUISHED_NAME
58 ]
59 });
60
61 // Set the Seal Field Name to be created in input PDF document
62 const sealFieldName = "Signature1";
63
64 // Set the page number in input document for applying seal
65 const sealPageNumber = 1;
66
67 // Set if seal should be visible or invisible
68 const sealVisible = true;
69
70 // Create FieldLocation instance and set the coordinates for applying signature
71 const fieldLocation = new FieldLocation({
72 left: 150,
73 top: 250,
74 right: 350,
75 bottom: 200
76 });
77
78 // Create FieldOptions instance with required details
79 const sealFieldOptions = new FieldOptions({
80 visible: sealVisible,
81 location: fieldLocation,
82 fieldName: sealFieldName,
83 pageNumber: sealPageNumber,
84 });
85
86 // Set the name of TSP Provider being used
87 const providerName = "<PROVIDER_NAME>";
88
89 // Set the access token to be used to access TSP provider hosted APIs
90 const accessToken = "<ACCESS_TOKEN>";
91
92 // Set the credential ID
93 const credentialId = "<CREDENTIAL_ID>";
94
95 // Set the PIN generated while creating credentials
96 const pin = "<PIN>";
97
98 // Create CSCAuthContext instance using access token and token type
99 const authorizationContext = new CSCAuthContext({
100 accessToken,
101 tokenType: "Bearer"
102 });
103
104 // Create CertificateCredentials instance with required certificate details
105 const certificateCredentials = new CSCCredential({
106 providerName,
107 credentialId,
108 pin,
109 authorizationContext,
110 });
111
112 // Create parameters for the job
113 const params = new PDFElectronicSealParams({
114 documentLevelPermission
115 certificateCredentials,
116 sealFieldOptions,
117 sealAppearanceOptions
118 });
119
120 // Creates a new job instance
121 const job = new PDFElectronicSealJob({
122 inputAsset: sourceFileAsset,
123 sealImageAsset,
124 params,
125 });
126
127 // Submit the job and get the job result
128 const pollingURL = await pdfServices.submit({job});
129 const pdfServicesResponse = await pdfServices.getJobResult({
130 pollingURL,
131 resultType: PDFElectronicSealResult
132 });
133
134 // Get content from the resulting asset(s)
135 const resultAsset = pdfServicesResponse.result.asset;
136 const streamAsset = await pdfServices.getContent({asset: resultAsset});
137
138 // Creates a write stream and copy stream asset's content to it
139 const outputFilePath = "./sealedOutput.pdf";
140 console.log(`Saving asset at ${outputFilePath}`);
141
142 const writeStream = fs.createWriteStream(outputFilePath);
143 streamAsset.readStream.pipe(writeStream);
144 } catch (err) {
145 if (err instanceof SDKError || err instanceof ServiceUsageError || err instanceof ServiceApiError) {
146 console.log("Exception encountered while executing operation", err);
147 } else {
148 console.log("Exception encountered while executing operation", err);
149 }
150 } finally {
151 sourceFileReadStream?.destroy();
152 sealImageReadStream?.destroy();
153 }
154})();

Next Steps

Now that you've successfully performed your first operation, review the documentation for many other examples and reach out on our forums with any questions. Also remember the samples you downloaded while creating your credentials also have many demos.

Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.