Spaces:
Sleeping
Sleeping
Update puppeteer_pdf.js
Browse files- puppeteer_pdf.js +51 -19
puppeteer_pdf.js
CHANGED
|
@@ -25,8 +25,23 @@ 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',
|
|
@@ -91,43 +106,60 @@ async function convertToPDF() {
|
|
| 91 |
|
| 92 |
console.log(`Viewport set to: ${viewportWidth}x${viewportHeight}`);
|
| 93 |
|
| 94 |
-
//
|
| 95 |
-
const
|
| 96 |
-
|
| 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.
|
| 103 |
waitUntil: ['networkidle0', 'domcontentloaded'],
|
| 104 |
timeout: 30000
|
| 105 |
});
|
| 106 |
-
|
| 107 |
-
// Inject base tag for relative paths
|
| 108 |
-
await page.evaluate((baseUrl) => {
|
| 109 |
-
const base = document.createElement('base');
|
| 110 |
-
base.href = baseUrl;
|
| 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([
|
| 119 |
document.fonts.ready,
|
| 120 |
...selectors.map((img) => {
|
|
|
|
| 121 |
if (img.complete) return Promise.resolve();
|
| 122 |
return new Promise((resolve) => {
|
| 123 |
-
img.addEventListener("load",
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
});
|
| 127 |
}),
|
| 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');
|
|
@@ -149,7 +181,7 @@ async function convertToPDF() {
|
|
| 149 |
let pdfOptions = {
|
| 150 |
path: pdfPath,
|
| 151 |
printBackground: true,
|
| 152 |
-
preferCSSPageSize: true,
|
| 153 |
margin: {
|
| 154 |
top: '0mm',
|
| 155 |
right: '0mm',
|
|
@@ -157,7 +189,7 @@ async function convertToPDF() {
|
|
| 157 |
left: '0mm'
|
| 158 |
},
|
| 159 |
displayHeaderFooter: false,
|
| 160 |
-
scale: 1.0
|
| 161 |
};
|
| 162 |
|
| 163 |
// Set dimensions based on aspect ratio
|
|
|
|
| 25 |
let browser;
|
| 26 |
try {
|
| 27 |
console.log('Starting PDF conversion...');
|
| 28 |
+
console.log(`HTML File: ${htmlFilePath}`);
|
| 29 |
console.log(`Aspect Ratio: ${aspectRatio}`);
|
| 30 |
|
| 31 |
+
// Get absolute paths
|
| 32 |
+
const absoluteHtmlPath = path.resolve(htmlFilePath);
|
| 33 |
+
const htmlDir = path.dirname(absoluteHtmlPath);
|
| 34 |
+
console.log(`HTML Directory: ${htmlDir}`);
|
| 35 |
+
|
| 36 |
+
// Read HTML content
|
| 37 |
+
let htmlContent = fs.readFileSync(absoluteHtmlPath, 'utf8');
|
| 38 |
+
|
| 39 |
+
// List image files in the directory
|
| 40 |
+
const imageFiles = fs.readdirSync(htmlDir).filter(file =>
|
| 41 |
+
/\.(jpg|jpeg|png|gif|svg|webp|bmp)$/i.test(file)
|
| 42 |
+
);
|
| 43 |
+
console.log(`Found ${imageFiles.length} image file(s) in directory:`, imageFiles);
|
| 44 |
+
|
| 45 |
// Launch browser with system Chromium
|
| 46 |
const browserOptions = {
|
| 47 |
headless: 'new',
|
|
|
|
| 106 |
|
| 107 |
console.log(`Viewport set to: ${viewportWidth}x${viewportHeight}`);
|
| 108 |
|
| 109 |
+
// Construct file:// URL for the HTML directory
|
| 110 |
+
const baseUrl = 'file://' + htmlDir + '/';
|
| 111 |
+
console.log(`Base URL: ${baseUrl}`);
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
// Load HTML content with file:// protocol
|
| 114 |
console.log('Loading HTML content...');
|
| 115 |
+
await page.goto(baseUrl + path.basename(absoluteHtmlPath), {
|
| 116 |
waitUntil: ['networkidle0', 'domcontentloaded'],
|
| 117 |
timeout: 30000
|
| 118 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
// Wait for all images and fonts to load
|
| 121 |
console.log('Waiting for images and fonts...');
|
| 122 |
await page.evaluate(async () => {
|
| 123 |
const selectors = Array.from(document.querySelectorAll("img"));
|
| 124 |
+
console.log('Found', selectors.length, 'img tags');
|
| 125 |
+
|
| 126 |
await Promise.all([
|
| 127 |
document.fonts.ready,
|
| 128 |
...selectors.map((img) => {
|
| 129 |
+
console.log('Image src:', img.src, 'Complete:', img.complete);
|
| 130 |
if (img.complete) return Promise.resolve();
|
| 131 |
return new Promise((resolve) => {
|
| 132 |
+
img.addEventListener("load", () => {
|
| 133 |
+
console.log('Loaded:', img.src);
|
| 134 |
+
resolve();
|
| 135 |
+
});
|
| 136 |
+
img.addEventListener("error", () => {
|
| 137 |
+
console.log('Error loading:', img.src);
|
| 138 |
+
resolve();
|
| 139 |
+
});
|
| 140 |
+
setTimeout(() => {
|
| 141 |
+
console.log('Timeout:', img.src);
|
| 142 |
+
resolve();
|
| 143 |
+
}, 5000);
|
| 144 |
});
|
| 145 |
}),
|
| 146 |
]);
|
| 147 |
});
|
| 148 |
|
| 149 |
+
// Check which images loaded successfully
|
| 150 |
+
const imageStatus = await page.evaluate(() => {
|
| 151 |
+
const images = Array.from(document.querySelectorAll('img'));
|
| 152 |
+
return images.map(img => ({
|
| 153 |
+
src: img.src,
|
| 154 |
+
complete: img.complete,
|
| 155 |
+
naturalWidth: img.naturalWidth,
|
| 156 |
+
naturalHeight: img.naturalHeight,
|
| 157 |
+
width: img.width,
|
| 158 |
+
height: img.height
|
| 159 |
+
}));
|
| 160 |
+
});
|
| 161 |
+
console.log('Image loading status:', JSON.stringify(imageStatus, null, 2));
|
| 162 |
+
|
| 163 |
// Count pages for logging
|
| 164 |
const pageCount = await page.evaluate(() => {
|
| 165 |
const pages = document.querySelectorAll('.page, .slide, section.page, article.page');
|
|
|
|
| 181 |
let pdfOptions = {
|
| 182 |
path: pdfPath,
|
| 183 |
printBackground: true,
|
| 184 |
+
preferCSSPageSize: true,
|
| 185 |
margin: {
|
| 186 |
top: '0mm',
|
| 187 |
right: '0mm',
|
|
|
|
| 189 |
left: '0mm'
|
| 190 |
},
|
| 191 |
displayHeaderFooter: false,
|
| 192 |
+
scale: 1.0
|
| 193 |
};
|
| 194 |
|
| 195 |
// Set dimensions based on aspect ratio
|