@adobe/pdfservices-node-sdk
Version:
The Adobe PDF Services Node.js SDK provides APIs for creating, combining, exporting and manipulating PDFs.
117 lines • 5.75 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.ExportPDFToImagesJob = 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 CloudAsset_1 = require("../../io/CloudAsset");
const PDFToImagesInternalAssetRequest_1 = require("../../internal/dto/request/pdftoimages/PDFToImagesInternalAssetRequest");
const PDFToImagesExternalAssetRequest_1 = require("../../internal/dto/request/pdftoimages/PDFToImagesExternalAssetRequest");
const ExternalAsset_1 = require("../../io/ExternalAsset");
const ExportPDFToImagesOutputType_1 = require("../params/exportpdftoimages/ExportPDFToImagesOutputType");
const SDKError_1 = require("../../exception/SDKError");
const PDFServicesHelper_1 = require("../../internal/PDFServicesHelper");
/**
* A job which exports a source PDF file to a supported format specified by
* {@link ExportPDFToImagesTargetFormat}.
* <p>
* The result is a list of images or a list containing a zip of images. For example, a PDF file with 15
* pages will generate 15 image files. The first file's name ends with "_0" and the last file's name ends with "_14".
* </p>
*
* @example
* Sample Usage:
* ```js
* const readStream = 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 inputAsset = await pdfServices.upload({
* readStream,
* mimeType: MimeType.PDF
* });
*
* const params = new ExportPDFToImagesParams({
* targetFormat: ExportPDFToImagesTargetFormat.JPEG,
* outputType: ExportPDFToImagesOutputType.LIST_OF_PAGE_IMAGES
* });
*
* const job = new ExportPDFToImagesJob({inputAsset, params});
*
* const pollingURL = await pdfServices.submit({job});
*
* const pdfServicesResponse = await pdfServices.getJobResult({
* pollingURL,
* resultType: ExportPDFToImagesResult
* });
*
* const resultAssets = pdfServicesResponse.result.assets;
*
* for (const resultAsset of resultAssets) {
* const streamAsset = await pdfServices.getContent({asset: resultAsset});
* }
* ```
*/
class ExportPDFToImagesJob extends PDFServicesJob_1.PDFServicesJob {
/**
* Constructs a new `ExportPDFToImagesJob` instance.
*
* @param params The parameters for constructing an instance of `ExportPDFToImagesJob`.
* @param params.inputAsset {@link Asset} object containing the input file. Cannot be undefined.
* @param params.params {@link ExportPDFToImagesParams} object containing the export parameters.
* 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.validateExportPDFToImagesJobParams(params);
this._inputAsset = params.inputAsset;
this._exportPDFToImagesParams = params.params;
this._outputAsset = params.outputAsset;
}
/**
* @hidden
*/
async process(executionContext, notifierConfigList) {
this.validate(executionContext);
const exportPDFToImagesRequest = this.generatePDFServicesAPIRequest(notifierConfigList), xRequestId = (0, uuid_1.v4)(), response = await PDFServicesHelper_1.PDFServicesHelper.submitJob(executionContext, exportPDFToImagesRequest, xRequestId, OperationHeaderInfoEndpointMap_1.OperationHeaderInfoEndpointMap.EXPORT_PDF_TO_IMAGES);
return response.headers[DefaultRequestHeaders_1.DefaultRequestHeaders.LOCATION_HEADER_NAME];
}
generatePDFServicesAPIRequest(notifierConfigList) {
let exportPDFToImagesRequest;
if (this._inputAsset instanceof CloudAsset_1.CloudAsset) {
exportPDFToImagesRequest = new PDFToImagesInternalAssetRequest_1.PDFToImagesInternalAssetRequest(this._inputAsset.assetId, this._exportPDFToImagesParams, notifierConfigList);
}
else {
exportPDFToImagesRequest = new PDFToImagesExternalAssetRequest_1.PDFToImagesExternalAssetRequest(this._inputAsset, this._exportPDFToImagesParams, this._outputAsset, notifierConfigList);
}
return exportPDFToImagesRequest;
}
validate(executionContext) {
super.validate(executionContext);
if (this._exportPDFToImagesParams.outputType === ExportPDFToImagesOutputType_1.ExportPDFToImagesOutputType.LIST_OF_PAGE_IMAGES &&
this._inputAsset instanceof ExternalAsset_1.ExternalAsset) {
throw new SDKError_1.SDKError("Please select Output Type as zip of page images for external storage.");
}
}
}
exports.ExportPDFToImagesJob = ExportPDFToImagesJob;
//# sourceMappingURL=ExportPDFToImagesJob.js.map