Generating PNGs from InDesign might sound inappropriate to my colleagues, but the exportFile DOM command is more than enough for most of my needs. In scripting terms, you just have to set a few preferences in app.pngExportPreferences, select a component and call exportFile on it with the parameter ExportFormat.PNG_FORMAT.

The result is quite decent. The native exporter supports page items and groups as well as pages and spreads, and it takes care of transparency effects if necessary.

As the name suggests exportFile produces a file object, which is the obvious purpose of ID-to-PNG conversion. But we developers are fond of ready-to-use binary data that we can copy-paste into source codes.

For example, we want to translate this:

A simple Bézier shape (currently selected in InDesign.)

into that:

\xC3\xED\x99\xD1\x11\x83 \f\x863\x82\xA38\x82#t\x04Ga\x047hGp\x04
\xC1\xD4T\xD3\xA4\x91 \x9Ar\xCD($\x8C\xA2\x84\xA9\x10o\xC7\xF5
[%mW2\x1D\x0E 0\x02#0\x02S\x18\x06\x1B\x89\xA9\xA4\xE3H\xECb\xC4
\xB4\x0E\xCC\xE7 \xC3\x03\xF18\xE2\t\x80H\x0E\xBF\x9Ep\x84\xC4

The above hieroglyphs represent the original image in PNG language. More technically, that's the serialized expression of the binary file once compacted into a JavaScript string. (Well, I've added line breaks to make it flow in the box, but you get the picture.)

If you are familiar with ScriptUI icons or stuff like that, you already know how boring it is to export and stringify PNG data, then re-inject these strings in your code.

That's why I've written SelToPng.jsx, an instant tool that serializes the current selection and shows the “stringified code” in an edit box. Here is how this works:

SelToPng in action. Select an object, get its serialized PNG.

1. Select your artwork in InDesign. (Make sure a single object is selected. If needed, create a group of multiple objects.)

2. Run SelToPng.jsx from the Scripts panel. (Important: the script is based on IdExtenso, so the framework must be reachable from the #include directive.)

3. Copy the PNG string from the edit box. You're done!

On top of that SelToPng also displays the serialized PNG in a ScriptUIImage so you can visualize how binary data actually render in a ScriptUI context.

SelToPng is part of IdExtenso's Toolbox:

See also:
IdExtenso: ExtendScript Framework for InDesign Ninjas.
‘Sprite’ Buttons in ScriptUI.
IdExtenso on GitHub.