F# – PDF fun with Markdown and FSharp.Formatting

I spent the last cou­ple of nights putting togeth­er a sim­ple Mark­down to PDF for­mat­ter using Tomas Pet­ricek’s FSharp.Formatting project and the Pdf­Sharp-MigraDoc library.

To use this library, you can either grab the source from the GitHub repos­i­to­ry or get it from Nuget using the fol­low­ing com­mand:

NuGet package

F# Usage

To use the library from F#, you can use the Mark­down type defined in the FSharp.Markdown name­space (from the FSharp.Formating library), once you’ve opened the FSharp.Markdown.Pdf name­space you’ll have access to two sta­t­ic exten­sion meth­ods:

  • Trans­form­Pdf – accepts a string as input and out­puts the result­ing PDF to a local file path or to a spec­i­fied Stream.
  • WritePdf – accepts a Mark­down­Doc­u­ment gen­er­at­ed by the Markdown.Parse method, and out­puts the result­ing PDF to a local file path or to a spec­i­fied Stream.

C# Usage

You can also use this library from C#, how­ev­er, the sta­t­ic exten­sion meth­ods men­tioned above is not acces­si­ble in C# because type exten­sions defined in F# are com­piled dif­fer­ent­ly to stan­dard exten­sion meth­ods you find in C# (with far more pos­si­bil­i­ties I might add, such as exten­sion prop­er­ties, and sta­t­ic meth­ods, for instance!) and is not rec­og­nized by the C# com­pil­er..

You can still access the same func­tion­al­i­ties using a Mark­down­Pdf type, with two sta­t­ic meth­ods:

  • Trans­form – equiv­a­lent to Markdown.TransformPdf.
  • Write – equiv­a­lent to Markdown.WritePdf.

You can see an exam­ple out­put here with the cor­re­spond­ing Mark­down here.