forked from westmonroe/pdf-puppeteer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (122 loc) · 4.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { getPaperSize } from '@cityssm/paper-sizes';
import launchPuppeteer from '@cityssm/puppeteer-launch';
import Debug from 'debug';
import exitHook from 'exit-hook';
import { defaultPdfOptions, defaultPdfPuppeteerOptions, defaultPuppeteerOptions, htmlNavigationTimeoutMillis, urlNavigationTimeoutMillis } from './defaultOptions.js';
const debug = Debug('pdf-puppeteer:index');
let cachedBrowser;
export async function convertHTMLToPDF(html, instancePdfOptions = {}, instancePdfPuppeteerOptions = {}) {
if (typeof html !== 'string') {
throw new TypeError('Invalid Argument: HTML expected as type of string and received a value of a different type. Check your request body and request headers.');
}
const pdfPuppeteerOptions = {
...defaultPdfPuppeteerOptions,
...instancePdfPuppeteerOptions
};
let browser;
let doCloseBrowser = false;
let isRunningPdfGeneration = false;
try {
if (pdfPuppeteerOptions.cacheBrowser ?? false) {
if (cachedBrowser === undefined) {
cachedBrowser = await launchPuppeteer(defaultPuppeteerOptions);
}
browser = cachedBrowser;
}
else {
doCloseBrowser = true;
browser = await launchPuppeteer(defaultPuppeteerOptions);
}
const browserVersion = await browser.version();
debug(`Browser: ${browserVersion}`);
const browserIsFirefox = browserVersion.toLowerCase().includes('firefox');
const page = await browser.newPage();
const remoteContent = pdfPuppeteerOptions.remoteContent ?? true;
if (pdfPuppeteerOptions.htmlIsUrl ?? false) {
debug('Loading URL...');
await page.goto(html, {
waitUntil: browserIsFirefox ? 'domcontentloaded' : 'networkidle0',
timeout: urlNavigationTimeoutMillis
});
}
else if (remoteContent) {
debug('Loading HTML with remote content...');
await page.goto(`data:text/html;base64,${Buffer.from(html).toString('base64')}`, {
waitUntil: browserIsFirefox ? 'domcontentloaded' : 'networkidle0',
timeout: urlNavigationTimeoutMillis
});
}
else {
debug('Loading HTML...');
await page.setContent(html, {
timeout: remoteContent
? urlNavigationTimeoutMillis
: htmlNavigationTimeoutMillis
});
}
debug('Content loaded.');
const pdfOptions = { ...defaultPdfOptions, ...instancePdfOptions };
if (pdfOptions.format !== undefined) {
const size = getPaperSize(pdfOptions.format);
if (size !== undefined) {
delete pdfOptions.format;
pdfOptions.width = `${size.width}${size.unit}`;
pdfOptions.height = `${size.height}${size.unit}`;
}
}
debug('Converting to PDF...');
isRunningPdfGeneration = true;
const pdfBuffer = await page.pdf(pdfOptions);
isRunningPdfGeneration = false;
debug('PDF conversion done.');
await page.close();
if (!pdfPuppeteerOptions.cacheBrowser || cachedBrowser !== browser) {
await browser.close();
}
return pdfBuffer;
}
catch (error) {
if (isRunningPdfGeneration &&
defaultPuppeteerOptions.browser === 'chrome') {
if (!doCloseBrowser) {
await closeCachedBrowser();
}
defaultPuppeteerOptions.browser = 'firefox';
debug('Trying again with Firefox.');
return await convertHTMLToPDF(html, instancePdfOptions, instancePdfPuppeteerOptions);
}
else {
throw error;
}
}
finally {
try {
if (doCloseBrowser && browser !== undefined) {
debug('Closing browser...');
await browser.close();
debug('Browser closed.');
}
}
catch {
}
}
}
export default convertHTMLToPDF;
export async function closeCachedBrowser() {
if (cachedBrowser !== undefined) {
try {
await cachedBrowser.close();
}
catch {
}
cachedBrowser = undefined;
}
}
export function hasCachedBrowser() {
return cachedBrowser !== undefined;
}
export { defaultPdfOptions, defaultPdfPuppeteerOptions } from './defaultOptions.js';
exitHook(() => {
debug('Running exit hook.');
void closeCachedBrowser();
});