|
| 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 | + }); |
0 commit comments