Extension to convert SVG to PNG with com.elovirta.ooxml

98 views
Skip to first unread message

Pieterjan Vandenweghe

unread,
Sep 16, 2021, 9:24:12 AM9/16/21
to DITA-OT Users
Hi,

I try to create a custom plug-in to convert SVG images to PNG with ImageMagick command line tool while producing a DOCX file with the com.elovirta.ooxml plug-in.
 
I would like to bypass the build-in Inkscape conversion (SVG to EMF), since it sometimes get stuck along the way (at least with the files I tested) and the commands don't work anymore with my version of InkScape. 

My custom changes work perfectly fine when I just overwrite the code of the  com.elovirta.ooxml plug-in:
  1. I add the link to the ImageMagick.exe instead of inkscape.exe (as parameter) 
  2. I replace the Inkscape commands with ImageMagick commands.
But overwriting the base plug-in is not the way to go of course, so I need to create a custom plug-in but that's where I am stuck. I am new to this kind of plug-ins.

This is what I find in the com.elovirta.ooxml build.xml:

  <target name="docx.svg2emf" if="inkscape.exec" unless="docx.inkscape.skip">
    <mkdir dir="${dita.temp.dir}/docx/word/media"/>
    <for param="svg.file">
      <path>
        <fileset dir="${user.input.dir}">
          <include name="**/*.svg"/>
        </fileset>
      </path>
      <sequential>
        <local name="svg.name"/>
        <local name="emf.file"/>
        <basename property="svg.name" file="@{svg.file}" suffix=".svg"/>
        <property name="emf.file" location="${dita.temp.dir}/docx/word/media/${svg.name}.emf"/>
        <echo taskname="inkscape">Converting @{svg.file} to ${emf.file}</echo>
        <exec executable="${inkscape.exec}" taskname="inkscape">
          <arg value="-z"/>
          <arg value="--export-emf=${emf.file}"/>
          <arg file="@{svg.file}"/>
        </exec>
      </sequential>
    </for>
  </target>

The only thing that needs some change are the commands to get it working and link the ${inkscape.exec} to ImageMagick.exe:
          <arg value="convert"/>
          <arg value="@{svg.file}"/>
          <arg value="${emf.file}"/>

So, I created my build.xml file and made some minor changes:

<project>
<target name="docx.svg2png" if="magick.exec" unless="docx.inkscape.skip">
    <mkdir dir="${dita.temp.dir}/docx/word/media"/>
    <for param="svg.file">
      <path>
        <fileset dir="${user.input.dir}">
          <include name="**/*.svg"/>
        </fileset>
      </path>
      <sequential>
        <local name="svg.name"/>
        <local name="emf.file"/>
        <basename property="svg.name" file="@{svg.file}" suffix=".svg"/>
        <property name="emf.file" location="${dita.temp.dir}/docx/word/media/${svg.name}.png"/>
        <echo taskname="inkscape">Converting @{svg.file} to ${emf.file}</echo>
        <exec executable="${magick.exec}" taskname="inkscape">
          <arg value="convert"/>
          <arg value="@{svg.file}"/>
          <arg value="${emf.file}"/>
        </exec>
      </sequential>
    </for>
  </target>
  </project>

I also created a plug-in.xml, but here I am probably doing something wrong by adding an incorrect extension??? 
Could someone help me which kind of extension I should choose to make this work?

<plugin id="com.flow.magick.word">
<require plugin="com.elovirta.ooxml"/>
<feature extension="ant.import" file="build.xml"/>
<transtype name="docx" desc="DOCX">
<param name="magick.exec" desc="Magick executable" type="file"></param>
</transtype>
</plugin>

I attached both files for references.
Thanks a lot in advance.

Kind regards,
Pieterjan
com.flow.magick.word.zip

Chris Papademetrious

unread,
Sep 16, 2021, 10:44:21 AM9/16/21
to DITA-OT Users
Hi Pieterjan,

With current versions of Inkscape (I am using 1.1), I believe the arguments should be:

        <exec executable="${inkscape.exec}" taskname="inkscape">
          <arg value="--export-type=wmf"/>

          <arg file="@{svg.file}"/>
        </exec>

Can you try locally modifying the original file to see if this works?

 - Chris

Pieterjan Vandenweghe

unread,
Sep 20, 2021, 8:08:31 AM9/20/21
to DITA-OT Users
Thank you Chris.
I will update the settings.

Toshihiko Makita

unread,
Sep 21, 2021, 7:49:27 AM9/21/21
to DITA-OT Users
For your reference, Office 2019 Word can contain SVG images directory into .docx file. The setting for SVG is bit different from others.
If you are interested in this function, see below code line 848 or later:
-- 
/*--------------------------------------------------
 Toshihiko Makita
 Development Group. Antenna House, Inc. Ina Branch
 Web site:
 http://www.antenna.co.jp/
 http://www.antennahouse.com/
 --------------------------------------------------*/   

2021年9月20日月曜日 21:08:31 UTC+9 pieterjan....@gmail.com:
Reply all
Reply to author
Forward
0 new messages