Edit in GitHubLog an issue

Quickstart for PDF Accessibility Auto-Tag API (Node.js)

To get started using Adobe PDF Accessibility Auto-Tag API, let's walk through a simple scenario - taking an input PDF document and running PDF Accessibility Auto-Tag API against it. Once the PDF has been tagged, we'll provide the document with tags and optionally, a report file. 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-accessibility-auto-tag-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 recommend 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.

alt

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 a PDF, Adobe Accesibility Auto-Tag API Sample.pdf (downloadable from here)) and tag its contents. The results will be saved in a given directory /output.

6) In your editor, open the directory where you previously copied the credentials. Create a new file, autotag-pdf.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 AutotagPDFParams,
6 AutotagPDFJob,
7 AutotagPDFResult,
8} = require("@adobe/pdfservices-node-sdk");
9const 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 can create our credentials and use them:

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 asset:

Copied to your clipboard
1const inputAsset = await pdfServices.upload({
2 readStream,
3 mimeType: MimeType.PDF
4});

5) Now, let's create the parameters and the job:

Copied to your clipboard
1// Create parameters for the job
2const params = new AutotagPDFParams({
3 generateReport: true,
4 shiftHeadings: true
5});
6
7// Creates a new job instance
8const job = new AutotagPDFJob({inputAsset, params});

This set of code defines what we're doing (an Auto-Tag operation), it defines parameters for the Auto-Tag job. PDF Accessibility Auto-Tag API has a few different options, but in this example, we're simply asking for a basic tagging operation, which returns the tagged PDF document and an XLSX report of the document.

6) 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: AutotagPDFResult
6});
7
8// Get content from the resulting asset(s)
9const resultAsset = pdfServicesResponse.result.taggedPDF;
10const resultAssetReport = pdfServicesResponse.result.report;
11const streamAsset = await pdfServices.getContent({asset: resultAsset});
12const streamAssetReport = await pdfServices.getContent({asset: resultAssetReport});

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

Copied to your clipboard
1// Creates an output stream and copy stream asset's content to it
2const outputFilePath = "./autotag-tagged.pdf";
3const outputFilePathReport = "./autotag-report.xlsx";
4console.log(`Saving asset at ${outputFilePath}`);
5console.log(`Saving asset at ${outputFilePathReport}`);
6
7let writeStream = fs.createWriteStream(outputFilePath);
8streamAsset.readStream.pipe(writeStream);
9writeStream = fs.createWriteStream(outputFilePathReport);
10streamAssetReport.readStream.pipe(writeStream);

Example running at the command line

Here's the complete application (autotag-pdf.js):

Copied to your clipboard
1const {
2 ServicePrincipalCredentials,
3 PDFServices,
4 MimeType,
5 AutotagPDFParams,
6 AutotagPDFJob,
7 AutotagPDFResult,
8} = require("@adobe/pdfservices-node-sdk");
9const fs = require("fs");
10
11(async () => {
12 let readStream;
13 try {
14 // Initial setup, create credentials instance
15 const credentials = new ServicePrincipalCredentials({
16 clientId: process.env.PDF_SERVICES_CLIENT_ID,
17 clientSecret: process.env.PDF_SERVICES_CLIENT_SECRET
18 });
19
20 // Creates a PDF Services instance
21 const pdfServices = new PDFServices({credentials});
22
23 // Creates an asset(s) from source file(s) and upload
24 readStream = fs.createReadStream("./Adobe_Accessibility_Auto_Tag_API_Sample.pdf");
25 const inputAsset = await pdfServices.upload({
26 readStream,
27 mimeType: MimeType.PDF
28 });
29
30 // Create parameters for the job
31 const params = new AutotagPDFParams({
32 generateReport: true,
33 shiftHeadings: true
34 });
35
36 // Creates a new job instance
37 const job = new AutotagPDFJob({inputAsset, params});
38
39 // Submit the job and get the job result
40 const pollingURL = await pdfServices.submit({job});
41 const pdfServicesResponse = await pdfServices.getJobResult({
42 pollingURL,
43 resultType: AutotagPDFResult
44 });
45
46 // Get content from the resulting asset(s)
47 const resultAsset = pdfServicesResponse.result.taggedPDF;
48 const resultAssetReport = pdfServicesResponse.result.report;
49 const streamAsset = await pdfServices.getContent({asset: resultAsset});
50 const streamAssetReport = await pdfServices.getContent({asset: resultAssetReport});
51
52 // Creates an output stream and copy stream asset's content to it
53 const outputFilePath = "./autotag-tagged.pdf";
54 const outputFilePathReport = "./autotag-report.xlsx";
55 console.log(`Saving asset at ${outputFilePath}`);
56 console.log(`Saving asset at ${outputFilePathReport}`);
57
58 let writeStream = fs.createWriteStream(outputFilePath);
59 streamAsset.readStream.pipe(writeStream);
60 writeStream = fs.createWriteStream(outputFilePathReport);
61 streamAssetReport.readStream.pipe(writeStream);
62 } catch (err) {
63 console.log("Exception encountered while executing operation", err);
64 } finally {
65 readStream?.destroy();
66 }
67})();

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.

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