Vuepress文章转pdf

11/7/2022 vuevuepress
(adsbygoogle = window.adsbygoogle || []).push({});

# 前言

vuepress原始文件是markdown文件,生成的web页面时html,如果想保存为pdf有2类方法。 版本说明: "vuepress": "^1.9.7" https://www.npmjs.com/及github上搜索的工具/插件

# 1. md文件转pdf

# vuepress2pdf

不能使用 https://github.com/chbndrhnns/vuepress2pdf

# vuepress-plugin-export

https://github.com/ulivz/vuepress-plugin-export

# vuepress-plugin-export-pdf

https://www.npmjs.com/package/@condorhero/vuepress-plugin-export-pdf

  1. 本地转换,速度快
  2. 导出的是整个站点docs目录下所有的文章合并为一个pdf文件
  3. 样式比较乱,有些vuepress特有的语法不支持

# 2. html转pdf

样式可以与网页显示一致

# mr-pdf推荐

支持docusaurus, vuepress, mkdocs

https://github.com/kohheepeace/mr-pdf

注意:

  1. 本地开发url要使用http://127.0.0.1:端口号
  2. 标题不支持中文, 报'xxx' 不是内部或外部命令,也不是可运行的程序
  3. 速度会慢一些,转换一个网页大约需要 10s
  4. 修改了文件名以及导出时去掉评论
npx mr-pdf --initialDocURLs="http://127.0.0.1:8080/3.html" --contentSelector="main" --paginationSelector=".page-nav .next a" --excludeSelectors="header.navbar,aside.sidebar,footer.page-edit .edit-link,.global-ui,.page-nav,.comments-wrapper" --coverImage="https://vuepress.vuejs.org/hero.png" --coverTitle="PanXin's notes" --coverSub="FAW" --outputPDFFilename="panxin-note.pdf"
1

# 优化

使用python脚本,这样每次只需要复制粘贴下url即可

import subprocess
url = input("请输入url: ")
url=url.replace("localhost","127.0.0.1")
name = url.split(".")[-2].split("/")[-1]
print(name)
cmd = "npx mr-pdf --initialDocURLs="+url+ ' --contentSelector="main" --paginationSelector=".page-nav .next a" --excludeSelectors="header.navbar,aside.sidebar,footer.page-edit .edit-link,.global-ui,.page-nav,.comments-wrapper" --coverImage="https://vuepress.vuejs.org/hero.png" --coverTitle="PanXin\'s notes" --coverSub="FAW" --outputPDFFilename='+ name+".pdf"
print(cmd)
subprocess.Popen(cmd,cwd="C:\\Users\\PanXin\\Desktop\\test",shell=True)
1
2
3
4
5
6
7
8