Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw.io - how to export all tabs to images using command line

Tags:

draw.io

I have installed on my PC draw.io app. I want to export all tabs with drawings to seperate files. The only options I have found is:

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

Help for 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)
(...)

is not supporting. I can use one of this:

  -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)

but how to get page-range or number of pages to select index?

like image 280
Leszek Avatar asked Dec 22 '20 07:12

Leszek


People also ask

How do I convert Drawio to excel?

Install Excel add in "draw.io" from microsoft store. https://appsource.microsoft.com/en-us/product/office/wa200000113?tab=overview. After installation, open any Excel file and click on Insert tab. Click on Drawio icon and select a .


2 Answers

There is no easy way to find the number of pages out of the box with Draw.io's CLI options.

One solution would be export the diagram as XML.

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

And then count how many diagram elements there are. It should equal the number of pages (I briefly tested this but I'm not 100% sure if diagram element only appears once per page).

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

Here is an example of putting it all together in a bash script (I tried this on 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

like image 112
eddiegroves Avatar answered Oct 18 '22 18:10

eddiegroves


OP did ask the question with reference to the Windows version, so here's a PowerShell solution inspired by eddiegroves

$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
}
like image 32
Pete Thom Avatar answered Oct 18 '22 17:10

Pete Thom