Draw.io - 如何使用命令行将所有选项卡导出到图像

问题描述

我已经在我的 PC 上安装了 draw.io 应用程序。我想将所有带有图纸的选项卡导出为单独的文件。我发现的唯一选项是:

"c:\Program Files\draw.io\draw.io.exe" --crop -x -f jpg c:\Users\user-name\Documents\_xxx_\my-file.drawio

draw.io 的帮助

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

Options:
(...)
  -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,and xml) (default: "pdf")
                                     (default: 0)
  -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)
(...)

不支持。我可以使用其中之一:

  -p,--page-range <from>..<to>      selects a page range (for PDF format only)

但是如何获取页面范围或页数来选择索引?

解决方法

没有简单的方法可以使用 Draw.io 的 CLI 选项找到开箱即用的页数。

一种解决方案是将图表导出为 XML。

draw.io --export --format xml --uncompressed test-me.drawio

然后计算有多少个 diagram 元素。它应该等于页数(我对此进行了简要测试,但我不能 100% 确定 diagram 元素是否每页只出现一次)。

grep -o "<diagram" "test-me.xml" | wc -l

这是将所有内容放在 bash 脚本中的示例(我在 MacOS 10.15 上尝试过)

#!/bin/bash
file=test-me # File name excluding extension

# Export diagram to plain XML
draw.io --export --format xml --uncompressed "$file.drawio"

# Count how many pages based on <diagram element
count=$(grep -o "<diagram" "$file.xml" | wc -l)

# Export each page as an PNG
# Page index is zero based
for ((i = 0 ; i <= $count-1; i++)); do
  draw.io --export --page-index $i --output "$file-$i.png" "$file.drawio"
done

,

OP 确实参考了 Windows 版本问了这个问题,所以这里有一个受 eddiegroves

启发的 PowerShell 解决方案
$DIR_DRAWIO = "."

$DrawIoFiles = Get-ChildItem $DIR_DRAWIO *.drawio -File

foreach ($file in $DrawIoFiles) {
    
    "File: '$($file.FullName)'"
    
    $xml_file = "$($file.DirectoryName)/$($file.BaseName).xml"

    if ((Test-Path $xml_file)) {
        
        Remove-Item -Path $xml_file -Force
    }
    
    # export to XML
    & "C:/Program Files/draw.io/draw.io.exe" '--export' '--format' 'xml' $file.FullName

    # wait for XML file creation
    while ($true) {
        if (-not (Test-Path $xml_file)) {
            Start-Sleep -Milliseconds 200
        }
        else {
            break
        }
    }
    # load to XML Document (cast text array to object)
    $drawio_xml = [xml](Get-Content $xml_file)

    # for each page export png
    for ($i = 0; $i -lt $drawio_xml.mxfile.pages; $i++) {
        
        $file_out = "$($file.DirectoryName)/$($file.BaseName)$($i + 1).png"

        & "C:/Program Files/draw.io/draw.io.exe" '--export' '--border' '10' '--page-index' $i '--output' $file_out $file.FullName
    }

    # wait for last file PNG image file
    while ($true) {
        if (-not (Test-Path "$($file.DirectoryName)/$($file.BaseName)$($drawio_xml.mxfile.pages).png")) {
            Start-Sleep -Milliseconds 200
        }
        else {
            break
        }
    }
    # remove/delete XML file
    if ((Test-Path $xml_file)) {
        
        Remove-Item -Path $xml_file -Force
    }

    # export 'vsdx' & 'pdf'
    & "C:/Program Files/draw.io/draw.io.exe" '--export' '--format' 'vsdx' $file.FullName
    Start-Sleep -Milliseconds 1000
    & "C:/Program Files/draw.io/draw.io.exe" '--export' '--format' 'pdf' $file.FullName
}