Class MarkdownToPdfConverter
- Namespace
- DocToolkit
- Assembly
- DocToolkit.dll
Converts Markdown to PDF by way of DOCX.
public static class MarkdownToPdfConverter
- Inheritance
-
MarkdownToPdfConverter
- Inherited Members
Remarks
This is a composition, and must stay one. It calls MarkdownToDocxConverter and then DocxToPdfConverter — exactly as HtmlToPdfConverter pivots through DOCX, and for the same reason: no permissively-licensed, NuGet-only, Linux-safe library renders either format to PDF directly. Do not reimplement conversion inside this class; a second rendering path is how the two come to disagree about what a document looks like.
Everything MarkdownToDocxConverter guarantees carries over unchanged, because this performs no conversion of its own: nothing here reaches the network or the disk, a remote image reference becomes a hyperlink rather than a fetch, and a local file reference is refused.
The fidelity caveats of DOCX → PDF apply — see DocxToPdfConverter.
Methods
Convert(string)
Converts markdown to a PDF.
public static byte[] Convert(string markdown)
Parameters
markdownstringThe Markdown to convert.
Returns
- byte[]
Exceptions
- ArgumentNullException
markdownis null.- DocumentConversionException
The Markdown could not be converted.
ConvertAsync(string, Stream, CancellationToken)
Converts markdown and writes the PDF to destination.
destination is written and is neither disposed, closed nor sought,
so it may be forward-only — an HTTP response body, for instance.
public static Task ConvertAsync(string markdown, Stream destination, CancellationToken ct = default)
Parameters
markdownstringThe Markdown to convert.
destinationStreamThe stream the PDF is written to.
ctCancellationTokenCancels the conversion and the write.
Returns
Remarks
The PDF reaches destination as it is produced rather than being
assembled in full first, which is the one place this class is more than two calls in a row:
it hands the destination to the renderer the way
HtmlToPdfConverter does. Buffering instead would be simpler and would give up
exactly the property the Stream overloads exist for.
Exceptions
- ArgumentNullException
Either argument is null.
- ArgumentException
destinationis not writable.- OperationCanceledException
ctwas cancelled.- DocumentConversionException
The Markdown could not be converted or written.
ConvertWithReport(string)
Converts markdown to a PDF, and reports what the conversion could not
carry across.
public static ConversionResult<byte[]> ConvertWithReport(string markdown)
Parameters
markdownstringThe Markdown to convert.
Returns
Remarks
The warnings come from the Markdown → DOCX half only. The DOCX → PDF half renders rather than converts and produces no report, so a caveat that applies to it — a chart or a shape effect the renderer cannot draw — will not appear here. Saying so is the point: the README's known-limitations table already records that the PDF renderers have no warning channel, and this method must not be read as having closed that gap.
Exceptions
- ArgumentNullException
markdownis null.- DocumentConversionException
The Markdown could not be converted.