Spaces:
Sleeping
Sleeping
Update puppeteer_pdf.js
Browse files- puppeteer_pdf.js +54 -14
puppeteer_pdf.js
CHANGED
|
@@ -24,6 +24,9 @@ if (!fs.existsSync(htmlFilePath)) {
|
|
| 24 |
async function convertToPDF() {
|
| 25 |
let browser;
|
| 26 |
try {
|
|
|
|
|
|
|
|
|
|
| 27 |
// Launch browser with system Chromium
|
| 28 |
const browserOptions = {
|
| 29 |
headless: 'new',
|
|
@@ -66,24 +69,36 @@ async function convertToPDF() {
|
|
| 66 |
}
|
| 67 |
|
| 68 |
browser = await puppeteer.launch(browserOptions);
|
| 69 |
-
|
| 70 |
const page = await browser.newPage();
|
| 71 |
|
| 72 |
-
// Set viewport for better rendering
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
await page.setViewport({
|
| 74 |
-
width:
|
| 75 |
-
height:
|
| 76 |
deviceScaleFactor: 2
|
| 77 |
});
|
| 78 |
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
const absoluteHtmlPath = path.resolve(htmlFilePath);
|
| 81 |
const htmlContent = fs.readFileSync(absoluteHtmlPath, 'utf8');
|
| 82 |
|
| 83 |
-
// Set base URL for relative paths
|
| 84 |
const baseUrl = 'file://' + path.dirname(absoluteHtmlPath) + '/';
|
| 85 |
|
| 86 |
-
|
| 87 |
await page.setContent(htmlContent, {
|
| 88 |
waitUntil: ['networkidle0', 'domcontentloaded'],
|
| 89 |
timeout: 30000
|
|
@@ -96,7 +111,8 @@ async function convertToPDF() {
|
|
| 96 |
document.head.appendChild(base);
|
| 97 |
}, baseUrl);
|
| 98 |
|
| 99 |
-
// Wait for images and fonts to load
|
|
|
|
| 100 |
await page.evaluate(async () => {
|
| 101 |
const selectors = Array.from(document.querySelectorAll("img"));
|
| 102 |
await Promise.all([
|
|
@@ -112,10 +128,18 @@ async function convertToPDF() {
|
|
| 112 |
]);
|
| 113 |
});
|
| 114 |
|
| 115 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
await page.emulateMediaType('screen');
|
| 117 |
|
| 118 |
-
// Additional wait for rendering
|
|
|
|
| 119 |
await new Promise(resolve => setTimeout(resolve, 2000));
|
| 120 |
|
| 121 |
// Generate PDF path
|
|
@@ -125,13 +149,15 @@ async function convertToPDF() {
|
|
| 125 |
let pdfOptions = {
|
| 126 |
path: pdfPath,
|
| 127 |
printBackground: true,
|
| 128 |
-
preferCSSPageSize:
|
| 129 |
margin: {
|
| 130 |
top: '0mm',
|
| 131 |
right: '0mm',
|
| 132 |
bottom: '0mm',
|
| 133 |
left: '0mm'
|
| 134 |
-
}
|
|
|
|
|
|
|
| 135 |
};
|
| 136 |
|
| 137 |
// Set dimensions based on aspect ratio
|
|
@@ -140,24 +166,38 @@ async function convertToPDF() {
|
|
| 140 |
pdfOptions.landscape = true;
|
| 141 |
pdfOptions.width = '297mm';
|
| 142 |
pdfOptions.height = '210mm';
|
|
|
|
| 143 |
} else if (aspectRatio === '1:1') {
|
| 144 |
pdfOptions.width = '210mm';
|
| 145 |
pdfOptions.height = '210mm';
|
|
|
|
|
|
|
| 146 |
} else if (aspectRatio === '9:16') {
|
| 147 |
pdfOptions.format = 'A4';
|
| 148 |
pdfOptions.landscape = false;
|
| 149 |
pdfOptions.width = '210mm';
|
| 150 |
pdfOptions.height = '297mm';
|
|
|
|
| 151 |
} else {
|
| 152 |
pdfOptions.format = 'A4';
|
| 153 |
pdfOptions.landscape = true;
|
|
|
|
| 154 |
}
|
| 155 |
|
| 156 |
// Generate the PDF
|
|
|
|
| 157 |
await page.pdf(pdfOptions);
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
} catch (error) {
|
| 163 |
console.error(`PDF conversion failed: ${error.message}`);
|
|
|
|
| 24 |
async function convertToPDF() {
|
| 25 |
let browser;
|
| 26 |
try {
|
| 27 |
+
console.log('Starting PDF conversion...');
|
| 28 |
+
console.log(`Aspect Ratio: ${aspectRatio}`);
|
| 29 |
+
|
| 30 |
// Launch browser with system Chromium
|
| 31 |
const browserOptions = {
|
| 32 |
headless: 'new',
|
|
|
|
| 69 |
}
|
| 70 |
|
| 71 |
browser = await puppeteer.launch(browserOptions);
|
|
|
|
| 72 |
const page = await browser.newPage();
|
| 73 |
|
| 74 |
+
// Set viewport for better rendering based on aspect ratio
|
| 75 |
+
let viewportWidth = 1920;
|
| 76 |
+
let viewportHeight = 1080;
|
| 77 |
+
|
| 78 |
+
if (aspectRatio === '9:16') {
|
| 79 |
+
viewportWidth = 1080;
|
| 80 |
+
viewportHeight = 1920;
|
| 81 |
+
} else if (aspectRatio === '1:1') {
|
| 82 |
+
viewportWidth = 1080;
|
| 83 |
+
viewportHeight = 1080;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
await page.setViewport({
|
| 87 |
+
width: viewportWidth,
|
| 88 |
+
height: viewportHeight,
|
| 89 |
deviceScaleFactor: 2
|
| 90 |
});
|
| 91 |
|
| 92 |
+
console.log(`Viewport set to: ${viewportWidth}x${viewportHeight}`);
|
| 93 |
+
|
| 94 |
+
// Load the HTML file
|
| 95 |
const absoluteHtmlPath = path.resolve(htmlFilePath);
|
| 96 |
const htmlContent = fs.readFileSync(absoluteHtmlPath, 'utf8');
|
| 97 |
|
| 98 |
+
// Set base URL for relative paths
|
| 99 |
const baseUrl = 'file://' + path.dirname(absoluteHtmlPath) + '/';
|
| 100 |
|
| 101 |
+
console.log('Loading HTML content...');
|
| 102 |
await page.setContent(htmlContent, {
|
| 103 |
waitUntil: ['networkidle0', 'domcontentloaded'],
|
| 104 |
timeout: 30000
|
|
|
|
| 111 |
document.head.appendChild(base);
|
| 112 |
}, baseUrl);
|
| 113 |
|
| 114 |
+
// Wait for all images and fonts to load
|
| 115 |
+
console.log('Waiting for images and fonts...');
|
| 116 |
await page.evaluate(async () => {
|
| 117 |
const selectors = Array.from(document.querySelectorAll("img"));
|
| 118 |
await Promise.all([
|
|
|
|
| 128 |
]);
|
| 129 |
});
|
| 130 |
|
| 131 |
+
// Count pages for logging
|
| 132 |
+
const pageCount = await page.evaluate(() => {
|
| 133 |
+
const pages = document.querySelectorAll('.page, .slide, section.page, article.page');
|
| 134 |
+
return pages.length || 'unknown';
|
| 135 |
+
});
|
| 136 |
+
console.log(`Detected ${pageCount} page element(s) in HTML`);
|
| 137 |
+
|
| 138 |
+
// Emulate screen media (not print) to preserve CSS
|
| 139 |
await page.emulateMediaType('screen');
|
| 140 |
|
| 141 |
+
// Additional wait for rendering and animations
|
| 142 |
+
console.log('Waiting for final render...');
|
| 143 |
await new Promise(resolve => setTimeout(resolve, 2000));
|
| 144 |
|
| 145 |
// Generate PDF path
|
|
|
|
| 149 |
let pdfOptions = {
|
| 150 |
path: pdfPath,
|
| 151 |
printBackground: true,
|
| 152 |
+
preferCSSPageSize: true, // CRITICAL: Respect CSS @page rules
|
| 153 |
margin: {
|
| 154 |
top: '0mm',
|
| 155 |
right: '0mm',
|
| 156 |
bottom: '0mm',
|
| 157 |
left: '0mm'
|
| 158 |
+
},
|
| 159 |
+
displayHeaderFooter: false,
|
| 160 |
+
scale: 1.0 // No scaling to preserve exact dimensions
|
| 161 |
};
|
| 162 |
|
| 163 |
// Set dimensions based on aspect ratio
|
|
|
|
| 166 |
pdfOptions.landscape = true;
|
| 167 |
pdfOptions.width = '297mm';
|
| 168 |
pdfOptions.height = '210mm';
|
| 169 |
+
console.log('PDF format: A4 Landscape (297mm x 210mm)');
|
| 170 |
} else if (aspectRatio === '1:1') {
|
| 171 |
pdfOptions.width = '210mm';
|
| 172 |
pdfOptions.height = '210mm';
|
| 173 |
+
pdfOptions.landscape = false;
|
| 174 |
+
console.log('PDF format: Square (210mm x 210mm)');
|
| 175 |
} else if (aspectRatio === '9:16') {
|
| 176 |
pdfOptions.format = 'A4';
|
| 177 |
pdfOptions.landscape = false;
|
| 178 |
pdfOptions.width = '210mm';
|
| 179 |
pdfOptions.height = '297mm';
|
| 180 |
+
console.log('PDF format: A4 Portrait (210mm x 297mm)');
|
| 181 |
} else {
|
| 182 |
pdfOptions.format = 'A4';
|
| 183 |
pdfOptions.landscape = true;
|
| 184 |
+
console.log('PDF format: A4 Landscape (default)');
|
| 185 |
}
|
| 186 |
|
| 187 |
// Generate the PDF
|
| 188 |
+
console.log('Generating PDF...');
|
| 189 |
await page.pdf(pdfOptions);
|
| 190 |
|
| 191 |
+
// Verify PDF was created
|
| 192 |
+
if (fs.existsSync(pdfPath)) {
|
| 193 |
+
const stats = fs.statSync(pdfPath);
|
| 194 |
+
console.log(`Successfully converted ${htmlFilePath} to ${pdfPath}`);
|
| 195 |
+
console.log(`PDF size: ${(stats.size / 1024).toFixed(2)} KB`);
|
| 196 |
+
process.exit(0);
|
| 197 |
+
} else {
|
| 198 |
+
console.error('PDF file was not created');
|
| 199 |
+
process.exit(1);
|
| 200 |
+
}
|
| 201 |
|
| 202 |
} catch (error) {
|
| 203 |
console.error(`PDF conversion failed: ${error.message}`);
|