質問

次のパスを持つ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

これをアリでやりたいです。コマンドラインで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入力ディレクトリを通過している間。

Ant-Contribにアクセスできます(1.0B3である「最新」をインストールしました)。ANT1.8.0を使用しています。私はそれを使用して何かを機能させることができると思います <for> タスク、 <fileset><mapper>S、しかし、私はそれをすべてまとめるのに苦労しています。

私は次のようなものを試しました:

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

しかし、残念ながら @{file} プロパティは絶対的なパスであり、相対コンポーネントに分解する簡単な方法を見つけることができません。

カスタムタスクのみを使用してこれを行うことができれば、書くことができると思いますが、既存のコンポーネントをまとめることができることを望んでいます。

役に立ちましたか?

解決

シーケンシャルタスクでは、 Ant-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.

他のヒント

完全性のために、Martin Claytonの答えに基づいてターゲットのために私が思いついたものです。今のところ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