@adobe/pdfservices-node-sdk
Version:
The Adobe PDF Services Node.js SDK provides APIs for creating, combining, exporting and manipulating PDFs.
135 lines • 6.22 kB
JavaScript
;
/*
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: Adobe permits you to use, modify, and distribute this file in
* accordance with the terms of the Adobe license agreement accompanying
* it. If you have received this file from a source other than Adobe,
* then your use, modification, or distribution of it requires the prior
* written permission of Adobe.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReplacePagesJob = void 0;
const uuid_1 = require("uuid");
const PDFServicesJob_1 = require("./PDFServicesJob");
const ValidationUtil_1 = require("../../internal/util/ValidationUtil");
const OperationHeaderInfoEndpointMap_1 = require("../../internal/constants/OperationHeaderInfoEndpointMap");
const DefaultRequestHeaders_1 = require("../../internal/http/DefaultRequestHeaders");
const CombinePDFParams_1 = require("../params/combinepdf/CombinePDFParams");
const CloudAsset_1 = require("../../io/CloudAsset");
const CombinePDFInternalAssetRequest_1 = require("../../internal/dto/request/combinepdf/CombinePDFInternalAssetRequest");
const CombinePDFExternalAssetRequest_1 = require("../../internal/dto/request/combinepdf/CombinePDFExternalAssetRequest");
const PageRanges_1 = require("../params/PageRanges");
const PDFServicesHelper_1 = require("../../internal/PDFServicesHelper");
/**
* A job that allows specific pages in a PDF file to be replaced with pages from multiple PDF files.
* <p>
* For more complex use cases, refer the {@link CombinePDFJob}.
* <p>
*
* @example
* Sample Usage:
* ```js
* baseReadStream = fs.createReadStream("<SOURCE_PATH>");
* readStream1 = fs.createReadStream("<SOURCE_PATH>");
*
* const credentials = new ServicePrincipalCredentials({
* clientId: process.env.PDF_SERVICES_CLIENT_ID,
* clientSecret: process.env.PDF_SERVICES_CLIENT_SECRET
* });
*
* const pdfServices = new PDFServices({credentials});
*
* const [baseAsset, asset1] = await pdfServices.uploadAssets({
* streamAssets: [{
* readStream: baseReadStream,
* mimeType: MimeType.PDF
* }, {
* readStream: readStream1,
* mimeType: MimeType.PDF
* }]
* });
*
* const params = new ReplacePagesParams(baseAsset)
* .addPagesForReplace({
* asset: asset1,
* basePage: 1
* });
*
* const job = new ReplacePagesJob({params});
*
* const pollingURL = await pdfServices.submit({job});
*
* const pdfServicesResponse = await pdfServices.getJobResult({
* pollingURL,
* resultType: InsertPagesResult
* });
*
* const resultAsset = pdfServicesResponse.result.asset;
* const streamAsset = await pdfServices.getContent({asset: resultAsset});
* ```
*/
class ReplacePagesJob extends PDFServicesJob_1.PDFServicesJob {
/**
* Constructs a new `ReplacePagesJob` instance.
*
* @param params The parameters to construct an instance of `ReplacePagesJob`.
* @param params.params {@link ReplacePagesParams} object containing the base PDF file and the pages to be
* replaced. Cannot be undefined.
* @param [params.outputAsset] {@link Asset} object representing the output asset.
* @remarks External assets can be set as output only when input is external asset as well.
*/
constructor(params) {
super();
ValidationUtil_1.ValidationUtil.validateReplacePagesJobParams(params);
this._replacePagesParams = params.params;
this._outputAsset = params.outputAsset;
}
/**
* @hidden
*/
async process(executionContext, notifierConfigList) {
this.validate(executionContext);
const combinePDFRequest = this.generatePDFServicesAPIRequest(notifierConfigList), xRequestId = (0, uuid_1.v4)(), response = await PDFServicesHelper_1.PDFServicesHelper.submitJob(executionContext, combinePDFRequest, xRequestId, OperationHeaderInfoEndpointMap_1.OperationHeaderInfoEndpointMap.REPLACE_PAGES);
return response.headers[DefaultRequestHeaders_1.DefaultRequestHeaders.LOCATION_HEADER_NAME];
}
generatePDFServicesAPIRequest(notifierConfigList) {
const combinePDFParams = this.getFilesToReplace(this._replacePagesParams.baseAsset, this._replacePagesParams.assetsToReplace);
combinePDFParams.validate();
let combinePDFRequest;
if (combinePDFParams?.assetsToCombine[0].asset instanceof CloudAsset_1.CloudAsset) {
combinePDFRequest = new CombinePDFInternalAssetRequest_1.CombinePDFInternalAssetRequest(combinePDFParams, notifierConfigList);
}
else {
combinePDFRequest = new CombinePDFExternalAssetRequest_1.CombinePDFExternalAssetRequest(combinePDFParams, this._outputAsset, notifierConfigList);
}
return combinePDFRequest;
}
getFilesToReplace(baseAsset, assetsToReplace) {
const assetList = [], pageRangeList = [];
let baseFileStartIndex = 1;
for (const [pageIndex, combinePDFJobInput] of assetsToReplace.entries()) {
if (pageIndex !== baseFileStartIndex) {
assetList.push(baseAsset);
const pageRanges = new PageRanges_1.PageRanges();
pageRanges.addRange(baseFileStartIndex, pageIndex - 1);
pageRangeList.push(pageRanges);
}
assetList.push(combinePDFJobInput.asset);
pageRangeList.push(combinePDFJobInput.pageRanges);
baseFileStartIndex = pageIndex + 1;
}
assetList.push(baseAsset);
const basePageRanges = new PageRanges_1.PageRanges();
basePageRanges.addAllFrom(baseFileStartIndex);
pageRangeList.push(basePageRanges);
const combinePDFParams = new CombinePDFParams_1.CombinePDFParams();
for (let i = 0; i < assetList.length; i++) {
combinePDFParams.addAsset(assetList[i], pageRangeList[i]);
}
return combinePDFParams;
}
}
exports.ReplacePagesJob = ReplacePagesJob;
//# sourceMappingURL=ReplacePagesJob.js.map