forked from t7tran/carbone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_image_processing.js
More file actions
75 lines (56 loc) · 2.05 KB
/
debug_image_processing.js
File metadata and controls
75 lines (56 loc) · 2.05 KB
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
const carbone = require("./lib/index.js");
const fs = require("fs");
const path = require("path");
// Enable debug for the postprocessor
process.env.DEBUG = "carbone:*";
process.env.DEBUG_DOCX_IMAGES = "true";
async function testImageExtraction() {
console.log("Testing image extraction and processing...");
// Initialize Carbone first
console.log("Initializing Carbone...");
await new Promise((resolve, reject) => {
carbone.set({ factories: 1 }, (err) => {
if (err) return reject(err);
resolve();
});
});
const templatePath = path.join(__dirname, "examples", "movies.docx");
if (!fs.existsSync(templatePath)) {
console.error("Template file not found:", templatePath);
return;
}
console.log("Template file found:", templatePath);
const template = fs.readFileSync(templatePath);
const data = { movies: [{ title: "Test Movie" }] };
const options = {
convertTo: "html",
};
console.log("Starting Carbone render...");
carbone.render(template, data, options, function (err, result) {
if (err) {
console.error("Error:", err);
return;
}
console.log("Render successful!");
const outputPath = path.join(__dirname, "debug_result.html");
fs.writeFileSync(outputPath, result);
console.log("HTML written to:", outputPath);
// Check if images were processed
const htmlContent = result.toString();
const localImages = (
htmlContent.match(/src="[^"]*\.(png|jpg|jpeg|gif|svg)"/g) || []
).filter((src) => !src.includes("data:"));
const dataUriImages = htmlContent.match(/src="data:[^"]*"/g) || [];
console.log("Local image references found:", localImages.length);
console.log("Data URI images found:", dataUriImages.length);
if (localImages.length > 0) {
console.log("Local images (should be 0):");
localImages.forEach((img, i) => console.log(` ${i + 1}: ${img}`));
}
if (dataUriImages.length > 0) {
console.log("Data URI images found:", dataUriImages.length);
}
process.exit(0);
});
}
testImageExtraction().catch(console.error);