Need to generate SVG from Indd file. I have prepared below script but in svg file getting only text, linked images are not showing on svg file. But the linked images are showing in linked section when I am opening the file in Adobe Illustrator.
function extractTextContent(page) {
var textContent = "";
var textFrames = page.textFrames;
for (var i = 0; i < textFrames.length; i++) {
var textFrame = textFrames[i];
textContent += textFrame.contents + "\n";
}
return textContent;
}
function extractImageContent(page) {
var imageContent = [];
var allGraphics = page.allGraphics;
for (var i = 0; i < allGraphics.length; i++) {
var graphic = allGraphics[i];
var bounds = graphic.geometricBounds;
var filePath = "";
if (graphic instanceof Image) {
if (graphic.itemLink) {
filePath = graphic.itemLink.filePath;
}
imageContent.push({
bounds: bounds,
filePath: filePath
});
$.writeln("Image found: " + bounds);
}
}
return imageContent;
}
function encodeForXML(str) {
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
}
function convertToSVG(textContent, imageContent) {
var textLines = textContent.split(/\r\n|\r|\n/);
var textSVG = '<text x="10" y="20">';
for (var i = 0; i < textLines.length; i++) {
textSVG += '<tspan x="10" dy="20">' + encodeForXML(textLines[i]) + '</tspan>';
}
textSVG += '</text>';
var imageContentLog = [];
for (var j = 0; j < imageContent.length; j++) {
imageContentLog.push("Bounds: [" + imageContent[j].bounds.join(", ") + "], File Path: " + imageContent[j].filePath);
}
$.writeln("Image Content: " + imageContentLog.join("\n"));
var imageSVG = "";
for (var k = 0; k < imageContent.length; k++) {
var image = imageContent[k];
var x = image.bounds[1];
var y = -image.bounds[0];
var width = image.bounds[3] - image.bounds[1];
var height = image.bounds[2] - image.bounds[0];
imageSVG += '<image x="' + x + '" y="' + y + '" width="' + width + '" height="' + height + '" xlink:href="' + encodeForXML(image.filePath) + '"/>';
}
return combinedSVG;
}
function logImageContent(imageContent) {
$.writeln("Image Content:");
for (var i = 0; i < imageContent.length; i++) {
var image = imageContent[i];
$.writeln("Image " + (i + 1) + ": bounds=" + image.bounds + ", filePath=" + image.filePath);
}
}
function openDocument(filePath) {
var doc = app.open(new File(filePath));
return doc;
}
function main() {
try {
var inddFilePath = "...";
var doc = openDocument(inddFilePath);
var exportedContent = "";
for (var i = 0; i < doc.pages.length; i++) {
var page = doc.pages[i];
var textContent = extractTextContent(page);
var imageContent = extractImageContent(page);
var pageSVG = convertToSVG(textContent, imageContent);
exportedContent += pageSVG;
}
var exportFile = new File("...");
exportFile.open("w");
exportFile.write(exportedContent);
exportFile.close();
alert("INDD file converted to SVG successfully!");
doc.close(SaveOptions.NO);
} catch (e) {
alert("Error occurred: " + e);
}
}
main();