LoginSignup
20
11

More than 3 years have passed since last update.

draw.io をコマンドラインで実行して、画像ファイルをエクスポートする

Last updated at Posted at 2019-12-14

draw.io をコマンドラインで使いたい

ダイアグラムを作成するのに便利な draw.io ですが、画像ファイルのエクスポートをコマンドラインで実行できたら、ドキュメントのビルドなんかに使えて便利だろうなと思い調べてみました。

drawio-desktop のインストール

オンライン番が有名な draw.io ですが、コマンドラインで実行するにはローカル環境にインストールする必要があります。まずは、drawio-desktop のリリースページ からダウンロードしてインストールしてください。

補足
少し前までは、下記URLにデスクトップ版のインストーラがあったのですが、いつの間にかリンクが消えてますね。
https://about.draw.io/integrations/#integrations_offline
Githubでは引き続き公開されているので、Githubのリリースページからダウンロードしましょう。
https://github.com/jgraph/drawio-desktop/releases

エイリアスの登録

次に、コマンドラインで使いやすいようにエイリアスを登録します。
実行ファイルのパスは、異なることがあるので、自分の環境でインストール先を確認してください。

macOS

alias draw.io=/Applications/draw.io.app/Contents/MacOS/draw.io

.bash_profile とかに保存しておくと、毎回入力する必要がなくて便利です。

Windows

doskey draw.io="C:\Program Files\draw.io\draw.io.exe" $*

doskey とか言うコマンド、初めて知りました。

コマンドラインでの実行

コマンドラインで実行する準備が整ったので、早速試してみましょう。

draw.io --help

下記のような、ヘルプページが表示されるはずです。

Usage: draw.io [options] [input file/folder]

Options:
  -V, --version                      output the version number
  -c, --create                       creates a new empty file if no file is passed
  -k, --check                        does not overwrite existing files
  -x, --export                       export the input file/folder based on the given options
  -r, --recursive                    for a folder input, recursively convert all files in sub-folders also
  -o, --output <output file/folder>  specify the output file/folder. If omitted, the input file name is used for output with the specified format as extension
  -f, --format <format>              if output file name extension is specified, this option is ignored (file type is determined from output extension, possible export formats are pdf, png, jpg, svg, vsdx) (default: "pdf")
  -q, --quality <quality>            output image quality for JPEG (default: 90)
  -t, --transparent                  set transparent background for PNG
  -e, --embed-diagram                includes a copy of the diagram (for PNG format only)
  -b, --border <border>              sets the border width around the diagram (default: 0)
  -s, --scale <scale>                scales the diagram size
  --width <width>                    fits the generated image/pdf into the specified width, preserves aspect ratio.
  --height <height>                  fits the generated image/pdf into the specified height, preserves aspect ratio.
  --crop                             crops PDF to diagram size
  -a, --all-pages                    export all pages (for PDF format only)
  -p, --page-index <pageIndex>       selects a specific page, if not specified and the format is an image, the first page is selected
  -g, --page-range <from>..<to>      selects a page range (for PDF format only)
  -h, --help                         output usage information

画像ファイルをエクスポート

画像ファイルをエクスポートするには、次のように実行します。

draw.io -xf svg -o diagram.svg diagram.drawio

フォルダごとまとめてエクスポートすることも可能なようです。

draw.io -xrf png -o output/ input/

ドキュメント生成ツールと組み合わせれば、色々と応用がききそうです。

20
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
11