
Yan Cui
I help clients go faster for less using serverless technologies.
Following on from my last post on formatting a Markdown document into PDF using FSharp.Markdown.Pdf, if you don’t like the default styling (which I tried to mimic style Github formats Markdown documents with) you can set your own styling for the different types of Markdown elements by going down a level of abstraction.
The FSharp.Markdown.Pdf.MarkdownStyleNames module defines all the names of styles used to format the different Markdown elements (e.g. heading 1-6, code block, listing 1-3, etc.) into PDF. To override the default styles, you need to:
- Instantiate an instance of the Document type from the MigraDoc.DocumentObjectModel namespace.
- Add the predefined style names from the aforementioned MarkdownStyleNames module to the document’s collection of styles.
- Tweak the style to your liking.
- Call the FSharp.Markdown.Pdf.PdfFormatting.formatMarkdown function with the Document value.
- Voila! You now have an instance of PdfDocument which has been formatted with your custom styling.
to see these simple steps in action:
open FSharp.Markdown | |
open FSharp.Markdown.Pdf | |
open MigraDoc.DocumentObjectModel | |
let text = """Some text in Markdown format..." | |
// you still have to parse the markdown document using the Markdown parser in the | |
// FSharp.Markdown namespace | |
let parsed = Markdown.Parse(markdownDoc) | |
// but instead of using Markdown.WritePdf or Markdown.TransformPdf, you can set | |
// override styles by instantiating an instance of the Document type from MigraDoc | |
let doc = new Document() | |
// you can set overrides for any styles used for formatting markdown elements, as defined in | |
// the MarkdownStyleNames module, e.g. Quoted, Code, Heading1-6, etc. | |
let quotedBlockStyle = doc.Styles.AddStyle(MarkdownStyleNames.Quoted, StyleNames.Normal) | |
quotedBlockStyle.ParagraphFormat.Shading.Visible <- true | |
quotedBlockStyle.ParagraphFormat.Shading.Color <- Colors.Black | |
quotedBlockStyle.Font.Color <- Colors.White | |
// the formatMarkdown function in the FSharp.Markdown.Pdf namespace takes the Document | |
// value and returns an instance of PdfDocument which you can then save to local filesystem | |
let pdfDoc = formatMarkdown doc parsed.DefinedLinks parsed.Paragraphs | |
pdfDoc.Save @"C:\temp\markdown-customized.pdf" |
You can compare the original and customized PDF outputs here and here.
Whenever you’re ready, here are 3 ways I can help you:
- Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
- I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
- Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.
Pingback: F# Weekly #26 2013 | Sergey Tihon's Blog