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AC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;QACjD,CAAC;IACH,CAAC;CAAA;AAED;;;;;GAKG;AACH,sBAA4B,OAAsB;;QAChD,MAAM,MAAM,GAAG,MAAM,wBAAM,CAAC;YAC1B,IAAI,EAAE,OAAO,CAAC,IAAI;YAClB,UAAU,EAAE,OAAO,CAAC,UAAU;YAC9B,WAAW,EAAE;gBACX,eAAe;gBACf,YAAY;aACb;SACF,CAAC,CAAC;QACH,OAAO,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QAC3B,MAAM,CAAC,MAAM,CAAC;IAChB,CAAC;CAAA","file":"index.js","sourcesContent":["'use strict';\n\nimport { launch, LaunchedChrome } from 'chrome-launcher';\nimport * as CDP from 'chrome-remote-interface';\nimport * as fs from 'fs';\nimport { Readable, Stream } from 'stream';\n\nimport { ChromePrintOptions } from './ChromePrintOptions';\nimport * as CompletionTrigger from './CompletionTrigger';\nimport { CreateResult } from './CreateResult';\n\nexport { CompletionTrigger, CreateResult };\n\n/**\n * PDF generation options.\n *\n * @export\n * @interface CreateOptions\n */\nexport interface CreateOptions {\n /**\n * The host to connect to Chrome at.\n * If set, it attempts to connect to Chrome.\n * If this and port are not set, it spawns\n * Chrome for the duration of the PDF generation.\n *\n * @type {string}\n * @memberof CreateOptions\n */\n host?: string;\n\n /**\n * The port to connect to Chrome with.\n * If set, it attempts to connect to Chrome.\n * If this and host are not set, it spawns\n * Chrome for the duration of the PDF generation.\n *\n * @type {number}\n * @memberof CreateOptions\n */\n port?: number;\n\n /**\n * The explicit path of the intended Chrome binary.\n *\n * @type {string}\n * @memberof CreateOptions\n */\n chromePath?: string;\n\n /**\n * The options to pass to Chrome's Page.printToPDF.\n * Note: these require Chrome >= 60.\n *\n * @type {ChromePrintOptions}\n * @memberof CreateOptions\n */\n printOptions?: ChromePrintOptions;\n\n /**\n * An optional CompletionTrigger to wait for before\n * printing the rendered page to a PDF.\n *\n * @type {CompletionTrigger.CompletionTrigger}\n * @memberof CreateOptions\n */\n completionTrigger?: CompletionTrigger.CompletionTrigger;\n\n /**\n * The time in milliseconds to wait until timing out.\n */\n timeout?: number;\n\n /**\n * A private flag to signify the operation has been canceled.\n */\n _canceled?: boolean;\n}\n\n/**\n * Generates a PDF from the given HTML string, launching Chrome as necessary.\n *\n * @export\n * @param {string} html the HTML string.\n * @param {Options} [options] the generation options.\n * @returns {Promise} the generated PDF data.\n */\nexport async function create(html: string, options?: CreateOptions): Promise {\n const myOptions = Object.assign({}, options);\n let chrome: LaunchedChrome;\n\n myOptions._canceled = false;\n if (myOptions.timeout >= 0) {\n setTimeout(() => {\n myOptions._canceled = true;\n }, myOptions.timeout);\n }\n\n await throwIfCanceled(myOptions);\n if (!myOptions.host && !myOptions.port) {\n await throwIfCanceled(myOptions);\n chrome = await launchChrome(myOptions);\n }\n\n try {\n return await generate(html, myOptions);\n } finally {\n if (chrome) {\n await chrome.kill();\n }\n }\n}\n\n/**\n * Connects to Chrome and generates a PDF from HTML or a URL.\n *\n * @param {string} html the HTML string or URL.\n * @param {CreateOptions} options the generation options.\n * @returns {Promise} the generated PDF data.\n */\nasync function generate(html: string, options: CreateOptions): Promise {\n await throwIfCanceled(options);\n const client = await CDP(options);\n try {\n const {Page} = client;\n await Page.enable(); // Enable Page events\n const url = /^(https?|file|data):/i.test(html) ? html : `data:text/html,${html}`;\n await throwIfCanceled(options);\n await Page.navigate({url});\n await throwIfCanceled(options);\n await Page.loadEventFired();\n if (options.completionTrigger) {\n await throwIfCanceled(options);\n const waitResult = await options.completionTrigger.wait(client);\n if (waitResult && waitResult.exceptionDetails) {\n await throwIfCanceled(options);\n throw new Error(waitResult.result.value);\n }\n }\n await throwIfCanceled(options);\n // https://chromedevtools.github.io/debugger-protocol-viewer/tot/Page/#method-printToPDF\n const pdf = await Page.printToPDF(options.printOptions);\n await throwIfCanceled(options);\n return new CreateResult(pdf.data);\n } finally {\n client.close();\n }\n}\n\n/**\n * Throws an exception if the operation has been canceled.\n *\n * @param {CreateOptions} options the options which track cancellation.\n * @returns {Promise} reject if cancelled, resolve if not.\n */\nasync function throwIfCanceled(options: CreateOptions): Promise {\n if (options._canceled) {\n throw new Error('HtmlPdf.create() timed out.');\n }\n}\n\n/**\n * Launches Chrome with the specified options.\n *\n * @param {CreateOptions} options the options for Chrome.\n * @returns {Promise} The launched Chrome instance.\n */\nasync function launchChrome(options: CreateOptions): Promise {\n const chrome = await launch({\n port: options.port,\n chromePath: options.chromePath,\n chromeFlags: [\n '--disable-gpu',\n '--headless',\n ],\n });\n options.port = chrome.port;\n return chrome;\n}\n"],"sourceRoot":"../.."}