Here’s a few examples of how to use markdown2pdf.ai in your code.
pip install markdown2pdf-python
from markdown2pdf import MarkdownPDF
def pay(offer):
print("⚡ Lightning payment required")
print(f"Amount: {offer['amount']} {offer['currency']}")
print(f"Description: {offer['description']}")
print(f"Invoice: {offer['payment_request']}")
input("Press Enter once paid...")
client = MarkdownPDF(on_payment_request=pay)
path = client.convert(markdown="# Hello from Python", title="My document title", download_path="output.pdf")
print("Saved PDF to:", path)
pip install markdown2pdf-python
from markdown2pdf import MarkdownPDF
def pay(offer):
print("⚡ Lightning payment required")
print(f"Amount: {offer['amount']} {offer['currency']}")
print(f"Description: {offer['description']}")
print(f"Invoice: {offer['payment_request']}")
input("Press Enter once paid...")
client = MarkdownPDF(on_payment_request=pay)
path = client.convert(markdown="# Hello from Python", title="My document title", download_path="output.pdf")
print("Saved PDF to:", path)
npm install @serendipityai/markdown2pdf-typescript
import { convertMarkdownToPdf } from "@serendipityai/markdown2pdf-typescript";
import type { OfferDetails } from "@serendipityai/markdown2pdf-typescript";
async function pay(offer: OfferDetails) {
console.log("⚡ Lightning payment required");
console.log(`Amount: ${offer.amount} ${offer.currency}`);
console.log(`Description: ${offer.description}`);
console.log(`Invoice: ${offer.payment_request}`);
await new Promise<void>(resolve => { process.stdin.once("data", () => { resolve(); }); });
}
async function main() {
const result = await convertMarkdownToPdf("# Hello from Typescript", {
title: "My document title",
downloadPath: "output.pdf",
onPaymentRequest: pay
});
console.log("Saved PDF to:", result);
}
main().catch(console.error);
You can drop the below configuration in to your Claude MCP config to add PDF conversion to your chat workflow.
"mcpServers": {
"markdown2pdf": {
"command":"npx",
"args": ["@serendipityai/markdown2pdf-mcp"],
"cwd": "~"
}
}
You’ll see that, in general, you pass your markdown content, a title and a download path to the convert
function and it will handle the markdown conversion for you. A mandatory function must be provided which is used to handle the L402 payment using a payment mechanism of your choice. This could be a manual payment (handled by copy and pasting the lightning invoice into a wallet of your choice), but more likely, will be handled using an automated means of programatically manipulating a lightning wallet directly.