remark-pdf
v0.0.18
Published
remark plugin to compile markdown to pdf.
Readme
remark-pdf
remark plugin to compile markdown to pdf.
- Uses pdfmake for compilation, to avoid issues with puppeteer.
- Works in browser and Node.js.
🚧 WIP 🚧
This project is aiming to support all nodes in mdast syntax tree, but currently transformation and stylings may not be well.
If you have some feature requests or improvements, please create a issue or PR.
- [x] paragraph
- [x] heading
- [x] thematicBreak
- [ ] blockquote
- [x] list / listItem
- [x] table / tableRow / tableCell
- [x] definition
- [x] text
- [x] emphasis
- [x] strong
- [x] delete
- [ ] inlineCode
- [x] break
- [x] link / linkReference
- [ ] footnote / footnoteReference / footnoteDefinition
- [ ] image / imageReference
- [ ] html
- [ ] code
- [ ] math / inlineMath
Demo
https://inokawa.github.io/remark-pdf/
Install
npm install remark-pdfUsage
Browser
import { unified } from "unified";
import markdown from "remark-parse";
import pdf from "remark-pdf";
import { saveAs } from "file-saver";
const processor = unified().use(markdown).use(pdf, { output: "blob" });
const text = "# hello world";
(async () => {
const doc = await processor.process(text);
const blob = await doc.result;
saveAs(blob, "example.pdf");
})();Node.js
import { unified } from "unified";
import markdown from "remark-parse";
import pdf from "remark-pdf/node";
import * as fs from "fs";
const processor = unified().use(markdown).use(pdf, { output: "buffer" });
const text = "# hello world";
(async () => {
const doc = await processor.process(text);
const buffer = await doc.result;
fs.writeFileSync("example.pdf", buffer);
})();Example: Custom fonts
Use custom fonts in Node by providing a fonts object to configuration, which is a dictionary structured like fonts[fontName][fontStyle][pathToFontFile]. Use the fonts by name in your styles configurations; the font file will be autoselected based on the chosen bold and italic style specifications.
Note that variable-width fonts are supported, but the path to the same font file must be supplied for all four font variant styles.
import { unified } from "unified";
import markdown from "remark-parse";
import pdf from "remark-pdf/node";
import * as fs from "fs";
const pdfOpts = {
output: "buffer",
fonts: {
"National Park": {
normal: "/path/to/fonts/nationalpark-variablevf.ttf",
bold: "/path/to/fonts/nationalpark-variablevf.ttf",
italics: "/path/to/fonts/nationalpark-variablevf.ttf",
bolditalics: "/path/to/fonts/nationalpark-variablevf.ttf",
},
"Merriweather Sans": {
normal: "/path/to/fonts/merriweathersans-light.ttf",
bold: "/path/to/fonts/merriweathersans-bold.ttf",
italics: "/path/to/fonts/merriweathersans-italic.ttf",
bolditalics: "/path/to/fonts/merriweathersans-bolditalic.ttf",
},
},
defaultStyle: { font: "Merriweather Sans", italics: true },
styles: {
head1: {
bold: true,
font: "National Park",
fontSize: 24,
},
},
};
const processor = unified().use(markdown).use(pdf, pdfOpts);
const text = `
# Header in National Park bold
Body text in Merriweather Sans Italic
`;
(async () => {
const doc = await processor.process(text);
const buffer = await doc.result;
fs.writeFileSync("example.pdf", buffer);
})();Documentation
Contribute
All contributions are welcome. If you find a problem, feel free to create an issue or a PR.
Making a Pull Request
- Fork this repo.
- Run
npm install. - Commit your fix.
- Add tests to cover the fix.
- Make a PR and confirm all the CI checks passed.
