DEV Community

artydev
artydev

Posted on

Convert any file between different format using Pandoc and CliWrap in .Net Core. Convert HTML to ODT in C# for free.

Pandoc is a wonderfull tool written in Haskell.
It is the swiss-army knife of files converters.

CliWrap is a library for interacting with command line executables in a functional manner, notice how concise the code is.

The idea is to create a web service...

using CliWrap;
using CliWrap.Buffered;
using System;
using System.Threading.Tasks;

namespace CliWrapDemo
{
  class Program
  {
    const string cmd = "cmd.exe";
    const string cmdArgs = "/c dir ";

    const string pandocCMD = "pandoc";
    const string pandocARGS = " -s  c:\\temp\\index.html -o  c:\\temp\\output.odt";

    const string tempPath = "c:\\temp\\*.odt";

    static async Task Main()
    {      
      var list = await ListDirectory(tempPath);
      Console.WriteLine(list.StandardOutput);
    }

    static async Task<BufferedCommandResult> ListDirectory(string path)
    {
      await HtmlToODT();
      var args = cmdArgs + path;
      var cmdLine = Cli.Wrap(cmd).WithArguments(args);
      var result = await cmdLine.ExecuteBufferedAsync();
      return result;
    }

    static async Task<BufferedCommandResult> HtmlToODT () {
      var cmd = Cli.Wrap(pandocCMD).WithArguments(pandocARGS);
      return await cmd.ExecuteBufferedAsync();
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)