这篇文章将介绍一个开源项目 Markdown-PDF 的使用, 以及遇到的一个无数个坑
前提
需要 node.JS 环境, 没有的请自行百度安装 node.JS
安装 Markdown-PDF
这一条是官网的安装教程
解释一下参数, NPM 是一个 node.JS 的包管理工具,-g 是全局安装, 指安装后可以作为一个命令使用,--ignore-scripts 是忽略一些脚本 (package.JSON 中的 script)
NPM install -g Markdown-PDF --ignore-scripts
但是跑着跑着如果发现报错, 原因是权限不足, 我们在 Linux 和 macOS 环境下需要 sudo
sudo NPM install -g Markdown-PDF --ignore-scripts
终于成功装好了, 赶紧试用一个
- jabin@jabin-deepin ~/Code/tmp [16:25:24]
- > $ Markdown-PDF lesson01.md
- internal/validators.JS:125
- throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
- ^
- TypeError [ERR_INVALID_ARG_TYPE]: The "file" argument must be of type string. Received type object
- at validateString (internal/validators.JS:125:11)
- at normalizeSpawnArguments (child_process.JS:399:3)
- at spawn (child_process.JS:532:38)
- at Object.execFile (child_process.JS:221:15)
- at WriteStream.<anonymous> (/usr/lib/node_modules/Markdown-PDF/index.JS:117:22)
- at WriteStream.emit (events.JS:194:15)
- at finishMaybe (_stream_writable.JS:641:14)
- at stream._final (_stream_writable.JS:619:5)
- at WriteStream._final (internal/fs/streams.JS:263:3)
- at callFinal (_stream_writable.JS:612:10)
又发现了错误! 这个错误的解决方法就是先卸载已经装好的模块, 去掉 --ignore-scripts 重装一遍
卸载
sudo NPM uninstall -g Markdown-PDF
去掉参数重装
sudo NPM install -g Markdown-PDF
去掉参数之后, 会被检测出来缺少一个模块 phantomjs, 然后脚本会自动安装, 问题是, 这个模块的下载速度奇慢无比, 挂了 10 分钟甚至卡在 8% 不动了
- jabin@jabin-deepin:~$ sudo NPM install -g Markdown-PDF
- NPM WARN deprecated phantomjs-prebuilt@2.1.16: this package is now deprecated
- /usr/bin/Markdown-PDF -> /usr/lib/node_modules/Markdown-PDF/bin/Markdown-PDF
- > phantomjs-prebuilt@2.1.16 install /usr/lib/node_modules/Markdown-PDF/node_modules/phantomjs-prebuilt
> node install.JS
- PhantomJS not found on PATH
- Downloading https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-linux-x86_64.tar.bz2
- Saving to /tmp/phantomjs/phantomjs-2.1.1-Linux-x86_64.tar.bz2
- Receiving...
- [===-------------------------------------] 8%
输出信息可以看到链接, 尝试使用浏览器下载 https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-linux-x86_64.tar.bz2
发现速度非常慢, 原因是中国特有的设施影响了我们的访问
可以看到脚本会下载资源, 然后放在对应的路径, 所以我们手动下载文件然后放在对应的路径, 至于文件, 你们可以到我的百度云下载,
Linux,macOS,Windows 三版本汇总链接
链接: https://pan.baidu.com/s/1Ubxqe-RF_H_g5HJvc4CFmQ 提取码: qwbw
将文件下载好, 放在对应的路径 (比如我的输出为 / tmp/phantomjs / 文件夹下)
经过测试, macOS 和 Linux 的路径都是 / tmp/phantomjs/
- jabin@jabin-deepin /tmp/phantomjs [17:00:07]
- > $ sudo NPM install -g Markdown-PDF
- NPM WARN deprecated phantomjs-prebuilt@2.1.16: this package is now deprecated
- /usr/bin/Markdown-PDF -> /usr/lib/node_modules/Markdown-PDF/bin/Markdown-PDF
- > phantomjs-prebuilt@2.1.16 install /usr/lib/node_modules/Markdown-PDF/node_modules/phantomjs-prebuilt
> node install.JS
- PhantomJS not found on PATH
- Download already available at /tmp/phantomjs/phantomjs-2.1.1-Linux-x86_64.tar.bz2
- Verified checksum of previously downloaded file
- Extracting tar contents (via spawned process)
- Removing /usr/lib/node_modules/Markdown-PDF/node_modules/phantomjs-prebuilt/lib/phantom
- Copying extracted folder /tmp/phantomjs/phantomjs-2.1.1-Linux-x86_64.tar.bz2-extract-1570611629575/phantomjs-2.1.1-Linux-x86_64 -> /usr/lib/node_modules/Markdown-PDF/node_modules/phantomjs-prebuilt/lib/phantom
- Phantom installation failed { [Error: EACCES: permission denied, link '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1570611629575/phantomjs-2.1.1-linux-x86_64' -> '/usr/lib/node_modules/markdown-pdf/node_modules/phantomjs-prebuilt/lib/phantom']
- errno: -13,
- code: 'EACCES',
- syscall: 'link',
- path:
- '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1570611629575/phantomjs-2.1.1-linux-x86_64',
- dest:
- '/usr/lib/node_modules/markdown-pdf/node_modules/phantomjs-prebuilt/lib/phantom' } Error: EACCES: permission denied, link '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1570611629575/phantomjs-2.1.1-linux-x86_64' -> '/usr/lib/node_modules/markdown-pdf/node_modules/phantomjs-prebuilt/lib/phantom'
- NPM ERR! code ELIFECYCLE
- NPM ERR! errno 1
- NPM ERR! phantomjs-prebuilt@2.1.16 install: `node install.js`
- NPM ERR! Exit status 1
- NPM ERR!
- NPM ERR! Failed at the phantomjs-prebuilt@2.1.16 install script.
- NPM ERR! This is probably not a problem with NPM. There is likely additional logging output above.
- NPM ERR! A complete log of this run can be found in:
- NPM ERR! /root/.NPM/_logs/2019-10-09T09_00_32_201Z-debug.log
发现还是报错, 尽管 sudo 了, 还是 permission denied, 这需要我们在后面加上参数 --unsafe-perm, 执行 sudo NPM install -g Markdown-PDF --unsafe-perm
- jabin@jabin-deepin /usr/lib/node_modules [18:32:58]
- > $ sudo NPM install -g Markdown-PDF --unsafe-perm
- NPM WARN deprecated phantomjs-prebuilt@2.1.16: this package is now deprecated
- /usr/bin/Markdown-PDF -> /usr/lib/node_modules/Markdown-PDF/bin/Markdown-PDF
- > phantomjs-prebuilt@2.1.16 install /usr/lib/node_modules/Markdown-PDF/node_modules/phantomjs-prebuilt
> node install.JS
- PhantomJS not found on PATH
- Download already available at /tmp/phantomjs/phantomjs-2.1.1-Linux-x86_64.tar.bz2
- Verified checksum of previously downloaded file
- Extracting tar contents (via spawned process)
- Removing /usr/lib/node_modules/Markdown-PDF/node_modules/phantomjs-prebuilt/lib/phantom
- Copying extracted folder /tmp/phantomjs/phantomjs-2.1.1-Linux-x86_64.tar.bz2-extract-1570617196654/phantomjs-2.1.1-Linux-x86_64 -> /usr/lib/node_modules/Markdown-PDF/node_modules/phantomjs-prebuilt/lib/phantom
- Writing location.JS file
- Done. Phantomjs binary available at /usr/lib/node_modules/Markdown-PDF/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs
- + Markdown-PDF@10.0.0
- added 106 packages from 381 contributors in 13.885s
- jabin@jabin-deepin /usr/lib/node_modules [18:33:19]
- > $
试用
- jabin@jabin-deepin ~/Code/tmp [18:37:36]
- > $ Markdown-PDF lesson01.md
效果
md 转 PDF
下一篇会尝试更换主题~~
来源: http://www.jianshu.com/p/9be654799449