@adobe/pdfservices-node-sdk
Version:
The Adobe PDF Services Node.js SDK provides APIs for creating, combining, exporting and manipulating PDFs.
123 lines • 5.57 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.HTMLToPDFJob = 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 ObjectUtil_1 = require("../../internal/util/ObjectUtil");
const CloudAsset_1 = require("../../io/CloudAsset");
const HTMLToPDFInternalAssetRequest_1 = require("../../internal/dto/request/htmltopdf/HTMLToPDFInternalAssetRequest");
const HTMLToPDFExternalAssetRequest_1 = require("../../internal/dto/request/htmltopdf/HTMLToPDFExternalAssetRequest");
const PDFServicesHelper_1 = require("../../internal/PDFServicesHelper");
/**
* A job that converts a HTML file to a PDF file. Some source formats may have associated conversion parameters
* which can be set using the {@link HTMLToPDFParams} parameter.
*
*
* <p>
* An HTML input can be provided either as a local zip archive or as a static HTML file with inline CSS.
* Alternatively, an HTML can also be specified via URL.
* <br>
* While creating the corresponding Asset instance, the media type must be:
* <ul>
* <li>"application/zip", if the input is a local zip archive.</li>
* <li>"text/html", if the input is a static HTML file with inline CSS</li>
* </ul>
* <br>
* In case the input is a local zip archive, it must have the following structure:
* <ul>
* <li>The main HTML file must be named "index.html".</li>
* <li>"index.html" must exist at the top level of zip archive, not in a folder.</li>
* </ul>
* <p>
* Sample layout:<pre>
* html_files.zip
* |__index.html
* |__referenced_file_1.css
* |__referenced_file_2.jpeg
* |__subfolder_1
* |_____referenced_file_3.jpeg
* </pre>
*
* <p>
*
* @example
* Sample Usage:
* ```js
* const credentials = new ServicePrincipalCredentials({
* clientId: process.env.PDF_SERVICES_CLIENT_ID,
* clientSecret: process.env.PDF_SERVICES_CLIENT_SECRET
* });
*
* const pdfServices = new PDFServices({credentials});
*
* const job = new HTMLToPDFJob({inputURL: "<HTML URL>"});
*
* const pollingURL = await pdfServices.submit({job});
*
* const pdfServicesResponse = await pdfServices.getJobResult({
* pollingURL,
* resultType: HTMLToPDFResult
* });
*
* const resultAsset = pdfServicesResponse.result.asset;
* const streamAsset = await pdfServices.getContent({asset: resultAsset});
* ```
*/
class HTMLToPDFJob extends PDFServicesJob_1.PDFServicesJob {
/**
* Constructs a new `HTMLToPDFJob` instance.
*
* @param params The parameters for constructing an instance of `HTMLToPDFJob`.
* @param [params.inputAsset] {@link Asset} object containing the input file.
* @param [params.inputURL] The URL of the input HTML file.
* @param [params.outputAsset] {@link Asset} object representing the output asset.
* @param [params.params] {@link HTMLToPDFParams} object containing the HTML to PDF conversion parameters.
* @remarks External assets can be set as output only when input is external asset as well.
*/
constructor(params) {
super();
ValidationUtil_1.ValidationUtil.validateHTMLToPDFJobParams(params);
this._inputAsset = params.inputAsset;
this._inputURL = params.inputURL;
this._outputAsset = params.outputAsset;
this._htmlToPDFParams = params.params;
}
/**
* @hidden
*/
async process(executionContext, notifierConfigList) {
this.validate(executionContext);
const htmlToPDFRequest = this.generatePDFServicesAPIRequest(notifierConfigList), xRequestId = (0, uuid_1.v4)(), response = await PDFServicesHelper_1.PDFServicesHelper.submitJob(executionContext, htmlToPDFRequest, xRequestId, OperationHeaderInfoEndpointMap_1.OperationHeaderInfoEndpointMap.HTML_TO_PDF);
return response.headers[DefaultRequestHeaders_1.DefaultRequestHeaders.LOCATION_HEADER_NAME];
}
generatePDFServicesAPIRequest(notifierConfigList) {
let htmlToPDFRequest;
if (!ObjectUtil_1.ObjectUtil.isUndefinedOrNull(this._inputAsset)) {
if (this._inputAsset instanceof CloudAsset_1.CloudAsset) {
htmlToPDFRequest = new HTMLToPDFInternalAssetRequest_1.HTMLToPDFInternalAssetRequest(this._inputAsset.assetId, undefined, this._htmlToPDFParams, notifierConfigList);
}
else {
htmlToPDFRequest = new HTMLToPDFExternalAssetRequest_1.HTMLToPDFExternalAssetRequest(this._inputAsset, this._outputAsset, this._htmlToPDFParams, notifierConfigList);
}
}
else {
htmlToPDFRequest = new HTMLToPDFInternalAssetRequest_1.HTMLToPDFInternalAssetRequest(undefined, this._inputURL, this._htmlToPDFParams, notifierConfigList);
}
return htmlToPDFRequest;
}
}
exports.HTMLToPDFJob = HTMLToPDFJob;
//# sourceMappingURL=HTMLToPDFJob.js.map