@adobe/pdfservices-node-sdk
Version:
The Adobe PDF Services Node.js SDK provides APIs for creating, combining, exporting and manipulating PDFs.
137 lines • 6.29 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.InsertPagesJob = 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 can be used to insert pages of multiple PDF files into a base PDF file.
* <p>
* For more complex use cases, refer the {@link CombinePDFJob}.
* <p>
*
* @example
* Sample Usage:
* ```js
* const baseReadStream = fs.createReadStream("<SOURCE_PATH>");
* const readStreamToInsert = 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, assetToInsert] = await pdfServices.uploadAssets({
* streamAssets: [{
* readStream: baseReadStream,
* mimeType: MimeType.PDF
* }, {
* readStream: readStreamToInsert,
* mimeType: MimeType.PDF
* }]
* });
*
* const params = new InsertPagesParams(baseAsset)
* .addPagesToInsertAt({
* inputAsset: assetToInsert,
* basePage: 1
* });
*
* const job = new InsertPagesJob({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 InsertPagesJob extends PDFServicesJob_1.PDFServicesJob {
/**
* Constructs a new `InsertPagesJob` instance.
*
* @param params The parameters for constructing an instance of `InsertPagesJob`.
* @param params.params {@link InsertPagesParams} object containing the input files and the page numbers
* to insert at. 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.validateInsertPagesJobParams(params);
this._insertPagesParams = 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.INSERT_PAGES);
return response.headers[DefaultRequestHeaders_1.DefaultRequestHeaders.LOCATION_HEADER_NAME];
}
generatePDFServicesAPIRequest(notifierConfigList) {
const combinePDFParams = this.getFilesToInsert(this._insertPagesParams.baseAsset, this._insertPagesParams.assetsToInsert);
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;
}
getFilesToInsert(baseAsset, assetsToInsert) {
const assetList = [], pageRangeList = [];
let baseFileStartIndex = 1;
for (const [pageIndex, inputList] of assetsToInsert.entries()) {
if (pageIndex !== 1) {
assetList.push(baseAsset);
const pageRanges = new PageRanges_1.PageRanges();
pageRanges.addRange(baseFileStartIndex, pageIndex - 1);
pageRangeList.push(pageRanges);
baseFileStartIndex = pageIndex;
}
for (const combinePDFJobInput of inputList) {
assetList.push(combinePDFJobInput.asset);
pageRangeList.push(combinePDFJobInput.pageRanges);
}
}
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.InsertPagesJob = InsertPagesJob;
//# sourceMappingURL=InsertPagesJob.js.map