360 Secure SkillHub
360 安全技能中心

md-to-pdf

基于 Node.js 的脚本,使用 marked 解析 Markdown、puppeteer-core 驱动无头浏览器渲染并导出 PDF,支持标题层级、代码高亮、表格、图片、可点击链接、页码与中文/CJK 文本;可指定输出文件名或关闭页码,通过环境变量指定浏览器路径。

zhuleikiro222
zhuleikiro222
热度 20.70
最近更新 2026-06-03 09:26:16
分类:效率依赖:代码执行 / 读写文件 / 联网浏览搜索应用场景:文件转换

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add zl-md-to-pdf
Metadata
name
md-to-pdf
description
Use when converting markdown files to PDF with styling, code highlighting, and CJK support

MD to PDF Conversion

Overview

Converts markdown files to styled PDF documents using Node.js, marked, and puppeteer-core. Supports Chinese/CJK text, code syntax highlighting, tables, and images.

When to Use

  • User asks to convert a .md file to PDF
  • Need to generate a printable document from markdown
  • Creating offline documentation or reports
  • Sharing formatted technical documents

Prerequisites

  • Node.js installed (v18+ recommended)
  • Google Chrome or Microsoft Edge browser installed

Quick Start

# Convert a markdown file to PDF
node convert.js input.md

# Specify output file
node convert.js input.md output.pdf

# Without page numbers
node convert.js input.md --no-page-numbers

Installation

First-time setup in the target project directory:

mkdir -p .md-to-pdf-tool && cd .md-to-pdf-tool
npm init -y
npm install marked@4.3.0 puppeteer-core@19.11.1

Then copy the convert.js script from this skill's directory.

Features

Feature Support
Headings (H1-H6) Styled with hierarchy
Code blocks Syntax highlighting via highlight.js
Tables Bordered with zebra striping
Lists Bulleted and numbered
Images Embedded (relative paths resolved)
Links Clickable in PDF
CJK/Chinese Full support
Page numbers Bottom center by default

Customization

Edit the CSS in convert.js to customize:

  • Font: Change font-family in body style
  • Colors: Modify color and background values
  • Margins: Adjust --marginTop, --marginBottom, etc. in options
  • Code theme: Change pre and code background colors

Common Issues

Issue Solution
Browser not found Set CHROME_PATH or EDGE_PATH environment variable
Permission denied Use Edge instead of Chrome, or run with elevated permissions
ESM module errors Use marked@4.3.0 and puppeteer-core@19.11.1 (CommonJS compatible)
Large file timeout Increase timeout in page.setContent() options

Implementation

The conversion script uses:

  1. marked - Parse markdown to HTML
  2. puppeteer-core - Launch headless browser to render HTML
  3. Page.pdf() - Generate PDF from rendered page

See convert.js in this skill directory for the complete implementation.