npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

md2pdf-cli

v1.0.0

Published

一个简单的命令行工具,用于将Markdown文件批量转换为PDF,并保留图片引用

Downloads

24

Readme

md2pdf-cli

一个简单的命令行工具,用于将Markdown文件批量转换为PDF,并保留图片引用。

npm version License: MIT

特点

  • 自动扫描目录及子目录中的所有.md文件
  • 保留Markdown中引用的图片,并将其包含在生成的PDF中
  • 支持自定义PDF样式
  • 将所有生成的PDF文件统一保存到指定目录
  • 兼容Windows、macOS和Linux系统
  • 提供详细的转换统计信息
  • 支持作为命令行工具或Node.js库使用

安装

全局安装(推荐)

npm install -g md2pdf-cli

本地安装

npm install md2pdf-cli

使用方法

命令行使用

安装后,可以直接在任何包含Markdown文件的目录中运行:

md2pdf

程序将自动:

  1. 扫描当前目录及子目录中的所有.md文件
  2. 创建pdf文件夹(如果不存在)
  3. 将每个Markdown文件转换为PDF并保存到pdf文件夹中
  4. 在控制台显示转换进度和结果

作为Node.js库使用

const { convertMarkdownToPdf } = require('md2pdf-cli');

// 默认选项
convertMarkdownToPdf(process.cwd())
  .then(result => {
    console.log(`转换完成,成功: ${result.success.length}, 失败: ${result.failed.length}`);
  })
  .catch(err => {
    console.error('转换出错:', err);
  });

// 自定义选项
convertMarkdownToPdf('/path/to/markdown/files', {
  outputDir: '/path/to/output/directory',
  cssPath: '/path/to/custom.css',
  globPattern: '**/*.md',
  ignore: ['node_modules/**', 'pdf/**', 'ignored/**']
})
  .then(result => {
    console.log(`转换完成,成功: ${result.success.length}, 失败: ${result.failed.length}`);
  })
  .catch(err => {
    console.error('转换出错:', err);
  });

自定义样式

程序会自动创建一个custom.css文件,您可以编辑此文件来自定义PDF的样式。

API文档

convertMarkdownToPdf(workDir, options)

将指定目录中的所有Markdown文件转换为PDF。

参数:

  • workDir (String): 包含Markdown文件的目录路径
  • options (Object, 可选): 配置选项
    • outputDir (String): PDF输出目录,默认为${workDir}/pdf
    • cssPath (String): CSS样式文件路径,默认为${workDir}/custom.css
    • globPattern (String): 用于匹配Markdown文件的glob模式,默认为**/*.md
    • ignore (Array): 要忽略的文件或目录,默认为['node_modules/**', 'pdf/**']

返回值:

返回一个Promise,解析为包含以下属性的对象:

  • total (Number): 总文件数
  • success (Array): 成功转换的文件列表
  • failed (Array): 转换失败的文件列表

注意事项

  • 对于Markdown中引用的图片,支持相对路径和绝对路径
  • 如果图片路径无效,程序会在控制台输出警告,但会继续处理文件
  • 程序会自动忽略node_modules目录和pdf目录中的Markdown文件
  • 如果有多个同名的Markdown文件(位于不同目录),转换后的PDF文件会相互覆盖,最后处理的文件将保留

依赖项

  • md-to-pdf: 用于将Markdown转换为PDF
  • glob: 用于文件路径匹配

许可证

MIT