让我们说我有PDF文件的集合,具有以下路径:

/some/path/pdfs/birds/duck.pdf
/some/path/pdfs/birds/goose.pdf
/some/path/pdfs/insects/fly.pdf
/some/path/pdfs/insects/mosquito.pdf

我想做的是生成用于每个尊重的相对路径结构PDF缩略图,并输出到其他位置,即:

/another/path/thumbnails/birds/duck.png
/another/path/thumbnails/birds/goose.png
/another/path/thumbnails/insects/fly.png
/another/path/thumbnails/insects/mosquito.png

我想这在Ant中来完成。假设我要使用的Ghostscript在命令行中,我已经制定了调用GS:

    <exec executable="${ghostscript.executable.name}">
        <arg value="-q"/>
        <arg value="-r72"/>
        <arg value="-sDEVICE=png16m"/>
        <arg value="-sOutputFile=${thumbnail.image.path}"/>
        <arg value="${input.pdf.path}"/>
    </exec>

因此,我需要做的是制定出${thumbnail.image.path}${input.pdf.path}正确的值在遍历PDF输入目录。

我有机会获得蚂蚁的contrib(刚刚安装了“最新”,这是1.0b3),我使用Ant 1.8.0。我想我可以使用<for>任务,<fileset>s和<mapper>s使一些工作,但我无法把他们放在一起。

我想是这样:

    <for param="file">
        <path>
            <fileset dir="${some.dir.path}/pdfs">
                <include name="**/*.pdf"/>
            </fileset>
        </path>
        <sequential>
            <echo message="@{file}"/>
        </sequential>
    </for>

但不幸的是@{file}属性是一个绝对路径,我无法找到分解成相对的部件的任何简单的方法。

如果我只能做到这一点使用自定义的任务,我想我可以写一个,但我希望我可以插在一起现有的组件。

有帮助吗?

解决方案

在连续的任务,你可以可以可以使用蚂蚁的contrib propertyregex 任务到输入路径输出映射。例如:

<propertyregex override="yes" property="outfile" input="@{file}"
               regexp="/some/path/pdfs/(.*).pdf"
               replace="/another/path/\1.png" />

哪些映射,例如/some/path/pdfs/birds/duck.pdf/another/path/birds/duck.png

其他提示

为了完整起见,这里就是我想出了基于马丁·克莱顿答案目标。它只能在Windows上,但现在这是因为我没有在Mac OS X注意周围得到在非代理的方式安装的Ghostscript然而,这是一个跨平台的解决方案,我不得不“擦洗”的文件分隔符来是一致的只是正斜线。

<target name="make-thumbnails" depends="">
    <taskdef resource="net/sf/antcontrib/antlib.xml">
        <classpath>
            <pathelement location="/path/to/ant-contrib-1.0b3.jar"/>
        </classpath>
    </taskdef>

    <condition property="ghostscript.executable.name" value="/path/to/gswin32c.exe">
        <os family="windows"/>
    </condition>
    <condition property="ghostscript.executable.name" value="">
        <os family="mac"/>
    </condition>

    <for param="file">
        <path>
            <fileset dir="/path/to/pdfs">
                <include name="**/*.pdf"/>
            </fileset>
        </path>
        <sequential>
            <propertyregex override="yes" property="file-scrubbed" input="@{file}"
                                regexp="\\"
                                replace="/" />
            <propertyregex override="yes" property="output-path-directory-fragment" input="${file-scrubbed}"
                                regexp=".*/pdfs/(.*)/.+\.pdf"
                                replace="\1" />
            <propertyregex override="yes" property="output-path-file-fragment" input="${file-scrubbed}"
                                regexp=".*/pdfs.*/(.+)\.pdf"
                                replace="\1.png" />
            <mkdir dir="${thumbnails.base.dir}/${output-path-directory-fragment}"/>
            <exec executable="${ghostscript.executable.name}">
                <arg value="-q"/>
                <arg value="-dLastPage=1"/>
                <arg value="-dNOPAUSE"/>
                <arg value="-dBATCH"/>
                <arg value="-dSAFER"/>
                <arg value="-r72"/>
                <arg value="-sDEVICE=png16m"/>
                <arg value="-sOutputFile=${thumbnails.base.dir}/${output-path-directory-fragment}/${output-path-file-fragment}"/>
                <arg value="${file-scrubbed}"/>
            </exec>
        </sequential>
    </for>
</target>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top