-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpdf.js
53 lines (45 loc) · 1.39 KB
/
pdf.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
const puppeteer = require('puppeteer');
const fs = require('fs');
const os = require('os');
const path = require('path');
(async () => {
// Create a browser instance
const browser = await puppeteer.launch({
executablePath: '/usr/bin/chromium',
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
// Create a new page
const page = await browser.newPage();
// Set HTML content
const htmlContent = `
<html>
<head><title>Test PDF</title></head>
<body>
<h1>PDF Generation Test</h1>
<p>This is a simple PDF to test the generation process.</p>
</body>
</html>
`;
// Set content to the page
await page.setContent(htmlContent, { waitUntil: 'networkidle0' });
// Define a temporary file path for the PDF output
const tempPdfPath = `test-output-${Date.now()}.pdf`;
// Generate PDF from the page content
await page.pdf({
path: tempPdfPath,
format: 'A4'
});
// Close the browser
await browser.close();
// Check if the PDF file was created and log the results
try {
const stats = fs.statSync(tempPdfPath);
if (stats.isFile() && stats.size > 0) {
console.log(`PDF successfully created at ${tempPdfPath}, size: ${stats.size} bytes`);
} else {
console.error('PDF generation failed, file was not created correctly.');
}
} catch (error) {
console.error('Error accessing PDF file:', error.message);
}
})();