Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync develop with main #832

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
326 changes: 188 additions & 138 deletions src/pages/overview/document-generation-api/gettingstarted.md

Large diffs are not rendered by default.

222 changes: 130 additions & 92 deletions src/pages/overview/document-generation-api/quickstarts/nodejs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ To get started using Adobe Document Generation API, let's walk through a simple

To complete this guide, you will need:

* [Node.js](https://nodejs.org) - Node.js version 14.0 or higher is required.
* [Node.js](https://nodejs.org) - 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.

Expand Down Expand Up @@ -116,28 +116,22 @@ Notice how the tokens in the Word document match up with values in our JSON. Whi
3) We'll begin by including our required dependencies:

```js
const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');
const fs = require('fs');
const {
ServicePrincipalCredentials,
PDFServices,
MimeType,
DocumentMergeParams,
OutputFormat,
DocumentMergeJob,
DocumentMergeResult,
SDKError,
ServiceUsageError,
ServiceApiError
} = require("@adobe/pdfservices-node-sdk");
const fs = require("fs");
```

The first line includes the Adobe PDF Services Node.js SDK. The second third include Node's `filesystem` package.

2) Now let's define our input and output:

```js
const OUTPUT = './generatedReceipt.pdf';

// If our output already exists, remove it so we can run the application again.
if(fs.existsSync(OUTPUT)) fs.unlinkSync(OUTPUT);

const INPUT = './receiptTemplate.docx';

const JSON_INPUT = require('./receipt.json');
```

These lines are hard coded but in a real application would typically be dynamic.

3) 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:
4) 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>`
Expand All @@ -146,109 +140,153 @@ These lines are hard coded but in a real application would typically be dynamic.
- `export PDF_SERVICES_CLIENT_ID=<YOUR CLIENT ID>`
- `export PDF_SERVICES_CLIENT_SECRET=<YOUR CLIENT SECRET>`

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

```js
const credentials = PDFServicesSdk.Credentials
.servicePrincipalCredentialsBuilder()
.withClientId(process.env.PDF_SERVICES_CLIENT_ID)
.withClientSecret(process.env.PDF_SERVICES_CLIENT_SECRET)
.build();

// Create an ExecutionContext using credentials
const executionContext = PDFServicesSdk.ExecutionContext.create(credentials);
```
// Initial setup, create credentials instance
const credentials = new ServicePrincipalCredentials({
clientId: process.env.PDF_SERVICES_CLIENT_ID,
clientSecret: process.env.PDF_SERVICES_CLIENT_SECRET
});

This code both points to the credentials downloaded previously as well as sets up an execution context object that will be used later.
// Creates a PDF Services instance
const pdfServices = new PDFServices({credentials});
```

5) Now, let's create the operation:
6) Now, let's upload the asset and create JSON data for merge:

```js
const documentMerge = PDFServicesSdk.DocumentMerge,
documentMergeOptions = documentMerge.options,
options = new documentMergeOptions.DocumentMergeOptions(JSON_INPUT, documentMergeOptions.OutputFormat.PDF);
// Creates an asset(s) from source file(s) and upload
readStream = fs.createReadStream("./receiptTemplate.docx");
const inputAsset = await pdfServices.upload({
readStream,
mimeType: MimeType.DOCX
});

// Setup input data for the document merge process
const jsonDataForMerge = JSON.parse(fs.readFileSync('./receipt.json', 'utf-8'));
```

// Create a new operation instance using the options instance.
const documentMergeOperation = documentMerge.Operation.createNew(options);
7) Now, let's create the parameters and the job:

// Set operation input document template from a source file.
const input = PDFServicesSdk.FileRef.createFromLocalFile(INPUT);
documentMergeOperation.setInput(input);
```js
// Create parameters for the job
const params = new DocumentMergeParams({
jsonDataForMerge,
outputFormat: OutputFormat.PDF
});

// Creates a new job instance
const job = new DocumentMergeJob({inputAsset, params});
```

This set of code defines what we're doing (a document merge operation, the SDK's way of describing Document Generation), points to our local JSON file and specifies the output is a PDF. It also points to the Word file used as a template.

6) The next code block executes the operation:
8) The next code block submits the job and gets the job result:

```js
// Execute the operation and Save the result to the specified location.
documentMergeOperation.execute(executionContext)
.then(result => result.saveAsFile(OUTPUT))
.catch(err => {
if(err instanceof PDFServicesSdk.Error.ServiceApiError
|| err instanceof PDFServicesSdk.Error.ServiceUsageError) {
console.log('Exception encountered while executing operation', err);
} else {
console.log('Exception encountered while executing operation', err);
}
// Submit the job and get the job result
const pollingURL = await pdfServices.submit({job});
const pdfServicesResponse = await pdfServices.getJobResult({
pollingURL,
resultType: DocumentMergeResult
});
```

This code runs the document generation process and then stores the result PDF document to the file system.

![Example running at the command line](./shot9.png)
// Get content from the resulting asset(s)
const resultAsset = pdfServicesResponse.result.asset;
const streamAsset = await pdfServices.getContent({asset: resultAsset});
```

Here's the complete application (`documentmerge.js`):
9) The next code block saves the result at the specified location:

```js
const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');
const fs = require('fs');

const OUTPUT = './generatedReceipt.pdf';
// Creates a write stream and copy stream asset's content to it
const outputFilePath = "./generatePDFOutput.pdf";
console.log(`Saving asset at ${outputFilePath}`);

// If our output already exists, remove it so we can run the application again.
if(fs.existsSync(OUTPUT)) fs.unlinkSync(OUTPUT);
const writeStream = fs.createWriteStream(outputFilePath);
streamAsset.readStream.pipe(writeStream);
```

const INPUT = './receiptTemplate.docx';
This code runs the Document Generation process and then stores the resulting PDF document to the file system.

const JSON_INPUT = require('./receipt.json');
![Example running at the command line](./shot9.png)

Here's the complete application (`documentmerge.js`):

// Set up our credentials object.
const credentials = PDFServicesSdk.Credentials
.servicePrincipalCredentialsBuilder()
.withClientId(process.env.PDF_SERVICES_CLIENT_ID)
.withClientSecret(process.env.PDF_SERVICES_CLIENT_SECRET)
.build();
```js
const {
ServicePrincipalCredentials,
PDFServices,
MimeType,
DocumentMergeParams,
OutputFormat,
DocumentMergeJob,
DocumentMergeResult,
SDKError,
ServiceUsageError,
ServiceApiError
} = require("@adobe/pdfservices-node-sdk");
const fs = require("fs");

(async () => {
let readStream;
try {
// Initial setup, create credentials instance
const credentials = new ServicePrincipalCredentials({
clientId: process.env.PDF_SERVICES_CLIENT_ID,
clientSecret: process.env.PDF_SERVICES_CLIENT_SECRET
});

// Create an ExecutionContext using credentials
const executionContext = PDFServicesSdk.ExecutionContext.create(credentials);
// Creates a PDF Services instance
const pdfServices = new PDFServices({credentials});

// This creates an instance of the Export operation we're using, as well as specifying output type (DOCX)
const documentMerge = PDFServicesSdk.DocumentMerge,
documentMergeOptions = documentMerge.options,
options = new documentMergeOptions.DocumentMergeOptions(JSON_INPUT, documentMergeOptions.OutputFormat.PDF);
// Creates an asset(s) from source file(s) and upload
readStream = fs.createReadStream("./receiptTemplate.docx");
const inputAsset = await pdfServices.upload({
readStream,
mimeType: MimeType.DOCX
});

// Create a new operation instance using the options instance.
const documentMergeOperation = documentMerge.Operation.createNew(options);
// Setup input data for the document merge process
const jsonDataForMerge = JSON.parse(fs.readFileSync('./receipt.json', 'utf-8'));

// Set operation input document template from a source file.
const input = PDFServicesSdk.FileRef.createFromLocalFile(INPUT);
documentMergeOperation.setInput(input);
// Create parameters for the job
const params = new DocumentMergeParams({
jsonDataForMerge,
outputFormat: OutputFormat.PDF
});

// Creates a new job instance
const job = new DocumentMergeJob({inputAsset, params});

// Execute the operation and Save the result to the specified location.
documentMergeOperation.execute(executionContext)
.then(result => result.saveAsFile(OUTPUT))
.catch(err => {
if(err instanceof PDFServicesSdk.Error.ServiceApiError
|| err instanceof PDFServicesSdk.Error.ServiceUsageError) {
console.log('Exception encountered while executing operation', err);
} else {
console.log('Exception encountered while executing operation', err);
}
// Submit the job and get the job result
const pollingURL = await pdfServices.submit({job});
const pdfServicesResponse = await pdfServices.getJobResult({
pollingURL,
resultType: DocumentMergeResult
});

// Get content from the resulting asset(s)
const resultAsset = pdfServicesResponse.result.asset;
const streamAsset = await pdfServices.getContent({asset: resultAsset});

// Creates a write stream and copy stream asset's content to it
const outputFilePath = "./generatePDFOutput.pdf";
console.log(`Saving asset at ${outputFilePath}`);

const writeStream = fs.createWriteStream(outputFilePath);
streamAsset.readStream.pipe(writeStream);
} catch (err) {
if (err instanceof SDKError || err instanceof ServiceUsageError || err instanceof ServiceApiError) {
console.log("Exception encountered while executing operation", err);
} else {
console.log("Exception encountered while executing operation", err);
}
} finally {
readStream?.destroy();
}
})();
```

## Next Steps
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/pages/overview/images/node_free_tier.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Please allow-list the following hostnames before using Adobe PDF Services SDK:<u

The sample below generates the output document in the **PDF** format. Similarly, you can specify **DOCX** as the **OutputFormat** to generate Word documents.

<CodeBlock slots="heading, code" repeat="4" languages="Java, .NET, Node JS, Rest API" />
<CodeBlock slots="heading, code" repeat="4" languages="Java, .NET, Node JS, REST API" />

##### Java

Expand Down Expand Up @@ -268,7 +268,7 @@ The sample below generates the output document in the **PDF** format. Similarly,
}
```

##### Rest API
##### REST API

```javascript
curl --location --request POST 'https://cpf-ue1.adobe.io/ops/:create?respondWith=%7B%22reltype%22%3A%20%22http%3A%2F%2Fns.adobe.com%2Frel%2Fprimary%22%7D' \
Expand Down Expand Up @@ -331,7 +331,7 @@ with the input JSON data.

The sample below shows the use of **Fragments** in the word template and generates the output document in the **PDF** format.

<CodeBlock slots="heading, code" repeat="4" languages="Java, .NET, Node JS, Rest API" />
<CodeBlock slots="heading, code" repeat="4" languages="Java, .NET, Node JS, REST API" />

##### Java

Expand Down Expand Up @@ -587,7 +587,7 @@ The sample below shows the use of **Fragments** in the word template and generat

```

##### Rest API
##### REST API

```javascript
curl --location --request POST 'https://cpf-ue1.adobe.io/ops/:create?respondWith=%7B%22reltype%22%3A%20%22http%3A%2F%2Fns.adobe.com%2Frel%2Fprimary%22%7D' \
Expand Down
Loading
Loading