Skip to main content
Participant
May 20, 2024
Question

INDD To SVG Conversion

  • May 20, 2024
  • 2 replies
  • 1175 views

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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&apos;');
}

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) + '"/>';
}
 
var combinedSVG = '<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">' + textSVG + imageSVG + '</svg>';
 
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();
This topic has been closed for replies.

2 replies

Keith_Gilbert
Inspiring
May 20, 2024

The "Export Selection to SVG" script found here: https://gilbertconsulting.com/scripts  might be helpful.

leo.r
Community Expert
Community Expert
May 20, 2024

While I don't have a solution for your script, you can create a PDF and then use an online converter to SVG like this one:

https://cloudconvert.com/pdf-to-svg