Spaces:
Paused
Paused
Update index.js
Browse files
index.js
CHANGED
|
@@ -10,7 +10,7 @@ import { createRequire } from 'module';
|
|
| 10 |
import { fileURLToPath } from 'url';
|
| 11 |
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
| 12 |
import puppeteer from "puppeteer"
|
| 13 |
-
|
| 14 |
|
| 15 |
const require = createRequire(import.meta.url);
|
| 16 |
const PORT = process.env.PORT || 7860;
|
|
@@ -145,7 +145,7 @@ async function createPDFV2(imagePaths, instanceID, tempDir) {
|
|
| 145 |
doc.pipe(fs.createWriteStream(pdfPath));
|
| 146 |
|
| 147 |
for (const imagePath of imagePaths) {
|
| 148 |
-
const { width, height } = await
|
| 149 |
doc.addPage({ size: [width, height] });
|
| 150 |
doc.image(imagePath, 0, 0, { width: width, height: height });
|
| 151 |
}
|
|
@@ -155,6 +155,16 @@ async function createPDFV2(imagePaths, instanceID, tempDir) {
|
|
| 155 |
return pdfPath;
|
| 156 |
}
|
| 157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
app.get('/downloadv2', async (req, res) => {
|
| 160 |
const { url } = req.query;
|
|
|
|
| 10 |
import { fileURLToPath } from 'url';
|
| 11 |
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
| 12 |
import puppeteer from "puppeteer"
|
| 13 |
+
import sharp from 'sharp';
|
| 14 |
|
| 15 |
const require = createRequire(import.meta.url);
|
| 16 |
const PORT = process.env.PORT || 7860;
|
|
|
|
| 145 |
doc.pipe(fs.createWriteStream(pdfPath));
|
| 146 |
|
| 147 |
for (const imagePath of imagePaths) {
|
| 148 |
+
const { width, height } = await getImageDimensionsV2(imagePath);
|
| 149 |
doc.addPage({ size: [width, height] });
|
| 150 |
doc.image(imagePath, 0, 0, { width: width, height: height });
|
| 151 |
}
|
|
|
|
| 155 |
return pdfPath;
|
| 156 |
}
|
| 157 |
|
| 158 |
+
async function getImageDimensionsV2(imagePath) {
|
| 159 |
+
try {
|
| 160 |
+
const { width, height } = await sharp(imagePath).metadata();
|
| 161 |
+
return { width, height };
|
| 162 |
+
} catch (error) {
|
| 163 |
+
console.error(`Failed to get dimensions for ${imagePath}: ${error.message}`);
|
| 164 |
+
throw error;
|
| 165 |
+
}
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
|
| 169 |
app.get('/downloadv2', async (req, res) => {
|
| 170 |
const { url } = req.query;
|