Converting idml coordinates to pixels


As of CS4, indesign has the ability to export to idml. idml is indesign’s xml format and is very useful if you want to use it in other applications. The only complicated part is the way it calculates the coordinates. If you are interested, you can look at the official idml doc + the cookbook from adobe website. I have created a quick php snippet to extract the relevant information from the idml and do the coordinate translation.

Once you get the idml file, unzip it. Set up the $path var, then run this script to get the coordinates.

<?php
        // Settings
        $path = 'YOUR_PATH/';
        $decimal = '3';

        $designmap = $path . 'designmap.xml';
        $fp = fopen($designmap,'r');
        $contents = fread($fp, filesize($designmap));
        preg_match('/Spreads\/Spread_(.*?).xml/', $contents, $spread);
        $spread = simplexml_load_file($path . $spread[0]);
        // use the base template to get coordinates
        $tf = $spread->xpath('//TextFrame');
        $baseX = 0;
        $baseY = 0;
        for ($i=0; $i < count($tf); $i++) {
                $trans = $tf[$i]['ItemTransform'];
                // get the x-y translation
                $offset = explode(' ', $trans);
                $offsetX = $offset[4];
                $offsetY = $offset[5];
                $story = $path . 'Stories/Story_'.$tf[$i]['ParentStory'].'.xml';
                $widthArray = (array) $tf[$i]->TextFramePreference['TextColumnFixedWidth'];
                $width = number_format($widthArray[0], $decimal);
                // now get the point on the top left hand corner
                $anchor = $tf[$i]->Properties->PathGeometry->GeometryPathType->P
athPointArray->PathPointType[0];
                $anchorArray = (array) $anchor['Anchor'];
                list($topX, $topY) = explode(' ', $anchorArray[0]);
                // if base layer, reset all coordinates to 0,0
                if ($i == 0) {
                        $baseX = $topX + $offsetX;
                        $baseY = $topY + $offsetY;
                        $trueX = 0;
                        $trueY = 0;
                }
                else {
                        $trueX = number_format($topX + $offsetX - $baseX, $decim
al);
                        $trueY = number_format($topY + $offsetY - $baseY, $decim
al);
                }
                echo "x y and width is $trueX, $trueY, $width \n";
        }
?>
Like it.? Share it:
Tags:

4 Responses to Converting idml coordinates to pixels

  1. Build Backlinks

    Great blog! You should start many more. I love all the info thanks I will stay tuned 🙂

  2. Jonathan S.

    This is fantastic, thank you for this!

  3. andrew

    Hi! Thanks for the solution. But another issue I’ve stumbled upon with this stuff is how to apply such a conversion logic to the rotated layers (textframes). I mean those ones that have a clockwise or counterclockwise rotation in the transformation matrix. Do you have any idea regarding this?

  4. lapuinka

    I would like to say thanks, nice way.