Skip to content

Commit 11dd291

Browse files
js: Add TDM samples
Assisted-by: Codex
1 parent 2cb0ac3 commit 11dd291

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var axios = require("axios");
2+
var fs = require("fs");
3+
4+
// This sample uploads a PDF and applies TDM rights metadata.
5+
6+
// By default, we use the US-based API service. This is the primary endpoint for global use.
7+
var apiUrl = "https://api.pdfrest.com";
8+
9+
/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
10+
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
11+
*/
12+
//var apiUrl = "https://eu-api.pdfrest.com";
13+
14+
var uploadData = fs.createReadStream("/path/to/file");
15+
16+
var uploadConfig = {
17+
method: "post",
18+
maxBodyLength: Infinity,
19+
url: apiUrl + "/upload",
20+
headers: {
21+
"Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // Replace with your API key
22+
"Content-Filename": "filename.pdf",
23+
"Content-Type": "application/octet-stream",
24+
},
25+
data: uploadData, // set the data to be sent with the request
26+
};
27+
28+
// send request and handle response or error
29+
axios(uploadConfig)
30+
.then(function (upload_response) {
31+
console.log(JSON.stringify(upload_response.data));
32+
var uploadedId = upload_response.data.files[0].id;
33+
34+
var tdmReservedPdfConfig = {
35+
method: "post",
36+
maxBodyLength: Infinity,
37+
url: apiUrl + "/tdm-reserved-pdf",
38+
headers: {
39+
"Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // Replace with your API key
40+
"Content-Type": "application/json",
41+
},
42+
data: { id: uploadedId, policy: "https://example.com/tdm-policy" }, // set the data to be sent with the request
43+
};
44+
45+
// send request and handle response or error
46+
axios(tdmReservedPdfConfig)
47+
.then(function (tdmReservedPdfResponse) {
48+
console.log(JSON.stringify(tdmReservedPdfResponse.data));
49+
})
50+
.catch(function (error) {
51+
console.log(error);
52+
});
53+
})
54+
.catch(function (error) {
55+
console.log(error);
56+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// This request demonstrates how to apply TDM rights metadata to a PDF.
2+
var axios = require('axios');
3+
var FormData = require('form-data');
4+
var fs = require('fs');
5+
6+
// By default, we use the US-based API service. This is the primary endpoint for global use.
7+
var apiUrl = "https://api.pdfrest.com";
8+
9+
/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
10+
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
11+
*/
12+
//var apiUrl = "https://eu-api.pdfrest.com";
13+
14+
// Create a new form data object and append the PDF file and TDM policy to it.
15+
var tdmReservedPdfData = new FormData();
16+
tdmReservedPdfData.append('file', fs.createReadStream('/path/to/file'));
17+
tdmReservedPdfData.append('policy', 'https://example.com/tdm-policy');
18+
tdmReservedPdfData.append('output', 'pdfrest_tdm_reserved_pdf');
19+
20+
// define configuration options for axios request
21+
var tdmReservedPdfConfig = {
22+
method: 'post',
23+
maxBodyLength: Infinity, // set maximum length of the request body
24+
url: apiUrl + '/tdm-reserved-pdf',
25+
headers: {
26+
'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', // Replace with your API key
27+
...tdmReservedPdfData.getHeaders() // set headers for the request
28+
},
29+
data : tdmReservedPdfData // set the data to be sent with the request
30+
};
31+
32+
// send request and handle response or error
33+
axios(tdmReservedPdfConfig)
34+
.then(function (response) {
35+
console.log(JSON.stringify(response.data));
36+
})
37+
.catch(function (error) {
38+
console.log(error);
39+
});
40+
41+
// If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample.

0 commit comments

Comments
 (0)