Markdown to PDF
Markdown is a lightweight markup language that can be used to add formatting to your plain text without using HTML tags, and with IronPDF, you can convert this markdown content to PDF just as easily as you could if it were HTML or a simple text document. As you can see here, through the use of the ChromePdfRenderer
rendering engine, the provided Markdown content is converted into a high-quality PDF document in just a couple of lines. Whether it's a Markdown file or a line of Markdown string, IronPDF handles the conversion with ease.
5 Steps to Converting Markdown to PDF in C#
// Step 1: Define a markdown string
const string exampleMdString = "This text is ***really important***.";
// Step 2: Create an instance of the ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Step 3: Render markdown file as PDF
// The RenderMarkdownFileAsPdf method takes the file path of the markdown file and converts it to a PDF document
PdfDocument pdfFromFile = renderer.RenderMarkdownFileAsPdf("report.md");
// Step 4: Render markdown string as PDF
// The RenderMarkdownStringAsPdf method converts the markdown string to a PDF document
PdfDocument pdfFromString = renderer.RenderMarkdownStringAsPdf(exampleMdString);
// Step 5: Save the PDF documents to files
// The SaveAs method saves the PDF document to the specified file path
pdfFromFile.SaveAs("report.pdf");
// Step 1: Define a markdown string
const string exampleMdString = "This text is ***really important***.";
// Step 2: Create an instance of the ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Step 3: Render markdown file as PDF
// The RenderMarkdownFileAsPdf method takes the file path of the markdown file and converts it to a PDF document
PdfDocument pdfFromFile = renderer.RenderMarkdownFileAsPdf("report.md");
// Step 4: Render markdown string as PDF
// The RenderMarkdownStringAsPdf method converts the markdown string to a PDF document
PdfDocument pdfFromString = renderer.RenderMarkdownStringAsPdf(exampleMdString);
// Step 5: Save the PDF documents to files
// The SaveAs method saves the PDF document to the specified file path
pdfFromFile.SaveAs("report.pdf");
' Step 1: Define a markdown string
Const exampleMdString As String = "This text is ***really important***."
' Step 2: Create an instance of the ChromePdfRenderer
Dim renderer = New ChromePdfRenderer()
' Step 3: Render markdown file as PDF
' The RenderMarkdownFileAsPdf method takes the file path of the markdown file and converts it to a PDF document
Dim pdfFromFile As PdfDocument = renderer.RenderMarkdownFileAsPdf("report.md")
' Step 4: Render markdown string as PDF
' The RenderMarkdownStringAsPdf method converts the markdown string to a PDF document
Dim pdfFromString As PdfDocument = renderer.RenderMarkdownStringAsPdf(exampleMdString)
' Step 5: Save the PDF documents to files
' The SaveAs method saves the PDF document to the specified file path
pdfFromFile.SaveAs("report.pdf")
The first step to converting Markdown to PDF is to load your Markdown content, whether it is a Markdown file or string. To demonstrate how IronPDF handles Markdown string conversion, we have created a new string variable named exampleMdString and have assigned our markdown content to it. This will later be passed to the renderer to be rendered into a PDF file.
Next, we need to create a new ChromePdfRenderer
instance. This is IronPDF's powerful, Chromium-based rendering engine which handles PDF rendering with ease, ensuring that the resulting PDF documents maintain the same quality as the original content. This class gives us access to two methods we will be needing today, RenderMarkdownFileAsPdf
and RenderMarkdownStringAsPdf
.
First, we will render a Markdown file as PDF using the first method. To do this, we will pass the Markdown file to the method accessed through the renderer object. This will render the Markdown file's content to PDF and save it to our PdfDocument
object, pdfFromFile. Next, we will render our Markdown string from earlier to PDF, again using the renderer object to access the necessary method before storing the resulting PDF to the PdfDocument
object we created in the same line.
Finally, we need to save the new PDF documents, which can easily be done using the SaveAs
method to save the PDFs to the specified location and filename.
Click here to view the How-to Guide, including examples, sample code, and files