@joaodotwork/md-2-pdf 1.1.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +62 -38
- package/filters/table-fit.lua +27 -0
- package/md_to_pdf.py +7 -1
- package/package.json +16 -8
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Joao Doria de Souza
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,21 +1,39 @@
|
|
|
1
1
|
# md-2-pdf
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@joaodotwork/md-2-pdf)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
5
|
|
|
5
|
-
It
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
A powerful CLI tool to convert Markdown files to PDF with built-in **Mermaid diagram support**. It bridges the gap between GitHub-flavored documentation and professional PDF exports.
|
|
7
|
+
|
|
8
|
+
## Why md-2-pdf?
|
|
9
|
+
|
|
10
|
+
Most markdown-to-pdf converters struggle with diagrams or produce poorly formatted documents. `md-2-pdf` is designed to:
|
|
11
|
+
- **Preserve Vector Quality:** Renders Mermaid diagrams as PDF vectors (not bitmaps) for lossless scaling.
|
|
12
|
+
- **Support GFM Conventions:** Handles lists interrupting paragraphs, task lists, and other GitHub Flavored Markdown features.
|
|
13
|
+
- **Professional Typography:** Uses `xelatex` by default for high-quality typesetting with smart page breaking.
|
|
14
|
+
|
|
15
|
+
## Key Features
|
|
16
|
+
|
|
17
|
+
- **Mermaid Diagrams:** Automatically detects ````mermaid` blocks and renders them as vector graphics.
|
|
18
|
+
- **Smart Formatting:**
|
|
19
|
+
- Standardized 11pt font and optimized line spacing.
|
|
20
|
+
- Interactive, blue clickable links.
|
|
21
|
+
- Automatic widow/orphan protection (keeps headers with content).
|
|
22
|
+
- Wide tables wrap to fit the page — pipe tables with short separator dashes (`|---|---|`) get equal column widths and wrapping cells instead of overflowing.
|
|
23
|
+
- Customizable margins and geometry.
|
|
24
|
+
- **Multi-Engine Support:** Automatically detects and uses the best available PDF engine (`xelatex`, `pdflatex`, `weasyprint`, or `wkhtmltopdf`).
|
|
25
|
+
- **Batch Processing:** Convert single files or entire directories with one command.
|
|
9
26
|
|
|
10
27
|
## Installation
|
|
11
28
|
|
|
12
29
|
### Prerequisites
|
|
13
30
|
|
|
14
|
-
1. **Node.js & npm** (
|
|
15
|
-
2. **Python 3** (Required for the core
|
|
16
|
-
3. **
|
|
17
|
-
|
|
18
|
-
*
|
|
31
|
+
1. **Node.js & npm** (Required for Mermaid rendering)
|
|
32
|
+
2. **Python 3** (Required for the core logic)
|
|
33
|
+
3. **Pandoc** (The engine behind the conversion)
|
|
34
|
+
4. **PDF Engine** (One of the following):
|
|
35
|
+
* **Recommended:** `xelatex` (Install via [MacTeX](https://tug.org/mactex/) on macOS or `texlive-full` on Linux)
|
|
36
|
+
* *Alternative:* `weasyprint` (`pip install weasyprint`)
|
|
19
37
|
|
|
20
38
|
### Install via npm
|
|
21
39
|
|
|
@@ -23,53 +41,59 @@ It automatically handles:
|
|
|
23
41
|
npm install -g @joaodotwork/md-2-pdf
|
|
24
42
|
```
|
|
25
43
|
|
|
44
|
+
*Or use `pnpm` / `yarn`:*
|
|
45
|
+
```bash
|
|
46
|
+
pnpm add -g @joaodotwork/md-2-pdf
|
|
47
|
+
```
|
|
48
|
+
|
|
26
49
|
## Usage
|
|
27
50
|
|
|
28
|
-
###
|
|
51
|
+
### Single File Conversion
|
|
29
52
|
|
|
30
53
|
```bash
|
|
31
|
-
md-2-pdf
|
|
54
|
+
md-2-pdf manual.md
|
|
32
55
|
```
|
|
33
|
-
|
|
56
|
+
*Creates `manual.pdf` in the same directory.*
|
|
34
57
|
|
|
35
|
-
###
|
|
58
|
+
### Batch Processing
|
|
36
59
|
|
|
37
|
-
|
|
60
|
+
Convert all markdown files in a directory:
|
|
38
61
|
|
|
39
62
|
```bash
|
|
40
|
-
md-2-pdf ./
|
|
63
|
+
md-2-pdf ./documentation
|
|
41
64
|
```
|
|
42
65
|
|
|
43
|
-
### Options
|
|
66
|
+
### Advanced Options
|
|
44
67
|
|
|
45
68
|
```bash
|
|
46
|
-
# Specify output
|
|
47
|
-
md-2-pdf
|
|
69
|
+
# Specify a custom output name
|
|
70
|
+
md-2-pdf proposal.md -o Final_Proposal.pdf
|
|
48
71
|
|
|
49
|
-
# Check dependencies
|
|
72
|
+
# Check if all dependencies are correctly installed
|
|
50
73
|
md-2-pdf --check-only
|
|
51
74
|
```
|
|
52
75
|
|
|
53
|
-
##
|
|
76
|
+
## Mermaid Support
|
|
77
|
+
|
|
78
|
+
Simply use standard Mermaid syntax in your markdown:
|
|
79
|
+
|
|
80
|
+
```mermaid
|
|
81
|
+
graph TD;
|
|
82
|
+
A[Markdown File] -->|Processor| B(Extract Mermaid);
|
|
83
|
+
B --> C{Render to PDF};
|
|
84
|
+
C -->|Vector| D[High-Quality PDF];
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
The tool will extract these blocks, render them using `mermaid-cli`, and embed them back into the final document seamlessly.
|
|
54
88
|
|
|
55
|
-
|
|
56
|
-
```mermaid
|
|
57
|
-
graph TD;
|
|
58
|
-
A-->B;
|
|
59
|
-
A-->C;
|
|
60
|
-
B-->D;
|
|
61
|
-
C-->D;
|
|
62
|
-
```
|
|
63
|
-
Just use standard ````mermaid blocks in your markdown. Now renders as vector graphics (PDF) instead of bitmaps for perfect clarity in your PDF exports.
|
|
89
|
+
## Troubleshooting
|
|
64
90
|
|
|
65
|
-
|
|
66
|
-
- Standardized font size (11pt) and line spacing (1.2).
|
|
67
|
-
- Blue clickable links.
|
|
68
|
-
- Horizontal rules (`---`) rendered as vertical space instead of lines.
|
|
69
|
-
- Smart page breaking rules to keep headers with content.
|
|
91
|
+
If you encounter issues with PDF generation, ensure `pandoc` and a LaTeX engine (like `xelatex`) are in your PATH. You can verify this by running:
|
|
70
92
|
|
|
71
|
-
|
|
93
|
+
```bash
|
|
94
|
+
md-2-pdf --check-only
|
|
95
|
+
```
|
|
72
96
|
|
|
73
|
-
|
|
97
|
+
## License
|
|
74
98
|
|
|
75
|
-
|
|
99
|
+
MIT © [Joao Doria de Souza](https://github.com/joaodotwork)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
-- Assign equal column widths to tables that have none set.
|
|
2
|
+
--
|
|
3
|
+
-- Pandoc's pipe-table reader leaves column widths undefined when the
|
|
4
|
+
-- separator-row dashes are short (e.g. `|---|---|`). The LaTeX writer then
|
|
5
|
+
-- emits `l` columns, which do not wrap and overflow the page on wide tables.
|
|
6
|
+
-- Setting equal widths summing to 1.0 makes the writer emit `p{...}` columns
|
|
7
|
+
-- sized to \linewidth, so cells wrap.
|
|
8
|
+
|
|
9
|
+
function Table(tbl)
|
|
10
|
+
local n = #tbl.colspecs
|
|
11
|
+
if n == 0 then return nil end
|
|
12
|
+
|
|
13
|
+
local total = 0
|
|
14
|
+
for _, c in ipairs(tbl.colspecs) do
|
|
15
|
+
local w = c[2]
|
|
16
|
+
if type(w) == "number" then
|
|
17
|
+
total = total + w
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
if total > 0 then return nil end
|
|
21
|
+
|
|
22
|
+
local equal = 1.0 / n
|
|
23
|
+
for i = 1, n do
|
|
24
|
+
tbl.colspecs[i] = { tbl.colspecs[i][1], equal }
|
|
25
|
+
end
|
|
26
|
+
return tbl
|
|
27
|
+
end
|
package/md_to_pdf.py
CHANGED
|
@@ -167,12 +167,18 @@ def convert_markdown_to_pdf(
|
|
|
167
167
|
|
|
168
168
|
print(f"Using PDF engine: {pdf_engine}")
|
|
169
169
|
|
|
170
|
+
# Locate bundled Lua filters
|
|
171
|
+
script_dir = Path(__file__).resolve().parent
|
|
172
|
+
table_fit_filter = script_dir / 'filters' / 'table-fit.lua'
|
|
173
|
+
|
|
170
174
|
# Convert to PDF using pandoc
|
|
171
175
|
cmd = [
|
|
172
176
|
'pandoc',
|
|
173
177
|
input_for_pandoc,
|
|
174
178
|
'-o', output_path,
|
|
179
|
+
'--from=gfm',
|
|
175
180
|
f'--pdf-engine={pdf_engine}',
|
|
181
|
+
f'--lua-filter={table_fit_filter}',
|
|
176
182
|
'-V', 'geometry:top=0.75in',
|
|
177
183
|
'-V', 'geometry:bottom=1in',
|
|
178
184
|
'-V', 'geometry:left=0.75in',
|
|
@@ -183,7 +189,7 @@ def convert_markdown_to_pdf(
|
|
|
183
189
|
'-V', 'colorlinks=true',
|
|
184
190
|
'-V', 'linkcolor=blue',
|
|
185
191
|
'-V', 'urlcolor=blue',
|
|
186
|
-
'-V', 'header-includes=\\renewcommand{\\rule}[2]{\\vspace{0.5em}} \\widowpenalty=10000 \\clubpenalty=10000 \\brokenpenalty=10000'
|
|
192
|
+
'-V', 'header-includes=\\renewcommand{\\rule}[2]{\\vspace{0.5em}} \\widowpenalty=10000 \\clubpenalty=10000 \\brokenpenalty=10000 \\setlength{\\emergencystretch}{3em} \\usepackage{newunicodechar} \\newunicodechar{·}{\\textperiodcentered\\allowbreak} \\newunicodechar{•}{\\textbullet\\allowbreak}'
|
|
187
193
|
]
|
|
188
194
|
|
|
189
195
|
try:
|
package/package.json
CHANGED
|
@@ -1,28 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@joaodotwork/md-2-pdf",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Convert markdown files to PDF with mermaid diagram support",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
7
7
|
"md-2-pdf": "./bin/index.js"
|
|
8
8
|
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
},
|
|
9
12
|
"keywords": [
|
|
10
13
|
"markdown",
|
|
11
14
|
"pdf",
|
|
12
15
|
"mermaid",
|
|
13
16
|
"converter"
|
|
14
17
|
],
|
|
15
|
-
"
|
|
16
|
-
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/joaodotwork/md-2-pdf.git"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"registry": "https://registry.npmjs.org/"
|
|
24
|
+
},
|
|
25
|
+
"author": "Joao Doria de Souza",
|
|
26
|
+
"license": "MIT",
|
|
17
27
|
"dependencies": {
|
|
18
28
|
"@mermaid-js/mermaid-cli": "^11.4.2"
|
|
19
29
|
},
|
|
20
30
|
"files": [
|
|
21
31
|
"md_to_pdf.py",
|
|
22
32
|
"bin/",
|
|
33
|
+
"filters/",
|
|
23
34
|
"requirements.txt"
|
|
24
|
-
]
|
|
25
|
-
|
|
26
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
27
|
-
}
|
|
28
|
-
}
|
|
35
|
+
]
|
|
36
|
+
}
|