My Technical Notes

Wednesday, 10 May 2017

Generating a PDF from Graphviz

Create the `.ps` file first (PostScript) from the `.dot` file:


C:\_programs\graphviz\bin\dot.exe -Tps .\graph.dot -o outfile.ps; 

Turn the `.ps` file into a `.pdf` file:


ps2pdf .\outfile.ps; 

Finally use `start` to open it, which will open the `.pdf` using the default handler, in my case, Adobe Acrobat:


start outfile.pdf

To close instances of Acrobat if they are open:


$p = Get-Process Acrobat -ErrorAction SilentlyContinue; 
if ($p) { 
    Stop-Process $p 
}

To bring it all together into a simple function `View-DotPdf`:


Function View-DotPdf([string]$Path) {
    $psPath = ($Path -replace '\.dot$', '.ps');
    $pdfPath = ($Path -replace '\.dot$', '.pdf');

    C:\_programs\graphviz\bin\dot.exe -Tps $Path -o $psPath; 

    $p = Get-Process Acrobat -ErrorAction SilentlyContinue; 
    if ($p) { 
        Stop-Process $p 
    }
    ps2pdf $psPath; 
    start $pdfPath;
}

To view it in `.png` form, do:


Function View-DotPng([string]$Path) {
    $outPath = ($Path -replace '\.dot$', '.png');
    C:\_programs\graphviz\bin\dot.exe -Tpng $Path -o $outPath; 
    start $outPath;
}

No comments: