A jsPDF utility to render Markdown directly into formatted PDFs with custom designs.
npm install jspdf-md-rendererA jsPDF utility to render Markdown directly into formatted PDFs with custom designs.



- Installation
- Usage
- API
- Examples
- Contributing
- License
To install the library, you can use npm:
``sh`
npm install jspdf-md-renderer
Here is a basic example of how to use the library to generate a PDF from Markdown content:
`ts
import { jsPDF } from 'jspdf';
import { MdTextRender } from 'jspdf-md-renderer';
const mdString =
This is a brief introduction paragraph. It sets the tone for the document and introduces the main topic in a concise manner.
Here is a medium-length paragraph that goes into more detail about the first section. It explains the context, provides background information, and sets up the discussion for the subsections.
This section showcases how to create simple and nested lists.
- Item 1
- Item 2
- Item 3
1. First Level 1
- First Level 2
- First Level 3
2. Second Level 1
- Second Level 2
- Another Second Level 2
- Nested deeper
- Topic 1
1. Subtopic 1.1
2. Subtopic 1.2
- Topic 2
- Subtopic 2.1
- Subtopic 2.2
1. Nested Subtopic 2.2.1
2. Nested Subtopic 2.2.2
;
const generatePDF = async () => {
const doc = new jsPDF({
unit: 'mm',
format: 'a4',
orientation: 'portrait',
});
const options = {
cursor: { x: 10, y: 10 },
page: {
format: 'a4',
unit: 'mm',
orientation: 'portrait',
maxContentWidth: 190,
maxContentHeight: 277,
lineSpace: 1.5,
defaultLineHeightFactor: 1.2,
defaultFontSize: 12,
defaultTitleFontSize: 14,
topmargin: 10,
xpading: 10,
xmargin: 10,
indent: 10,
},
font: {
bold: { name: 'helvetica', style: 'bold' },
regular: { name: 'helvetica', style: 'normal' },
light: { name: 'helvetica', style: 'light' },
},
endCursorYHandler: (y) => {
console.log('End cursor Y position:', y);
},
};
await MdTextRender(doc, mdString, options);
doc.save('example.pdf');
};
generatePDF();
`
Renders parsed markdown text into a jsPDF document.
#### Parameters
- doc: The jsPDF document instance.text
- : The markdown content to render.options
- : The render options (fonts, page margins, etc.).
Parses markdown into tokens and converts to a custom parsed structure.
#### Parameters
- text: The markdown content to parse.
#### Returns
- Promise: Parsed markdown elements.
The following Markdown elements are currently supported by jspdf-md-renderer:
- Headings: #, ##, ###, etc.-
- Paragraphs
- Lists:
- Unordered lists: , *, +1.
- Ordered lists: , 2., 3., etc.---
- Horizontal Rules: , *, ___bold
- Text Styles:
- Bold: or __bold__italic
- Italic: or _italic_bold italic
- Bold Italic: or ___bold italic___``
- Code Blocks (fenced and indented):
markdown`
js`
console.log('Hello, world!');
``
`
- Links:
markdown`
GitHub
`
- Blockquotes:
markdown`
> This is a blockquote.
`
- Images:
markdown`
!Alt text
`
- Inline Code:
markdowninline code
This is an example.`
`
- Tables:
markdown``
| Header 1 | Header 2 | Header 3 |
| -------- | -------- | -------- |
| Row 1 | Data | Value |
| Row 2 | Data | Value |
Output of above basic Example => Sample Generated PDF
You can find more examples in the examples directory.
Contributions are welcome! Please read the contributing guidelines first.
If you find this library useful, please consider giving it a ⭐ on GitHub! It helps others find the project and motivates continued development.
This project is licensed under the MIT License. See the LICENSE file for details.