ubuntu上使用 gitbook 生成 pdf 电子书:解决中文乱码问题

gitbook 上有很多电子书, 有些没有提供下载 pdf 功能,

在网上搜了下, 可以使用 gitbook 命令行工具来生成 pdf mobi epub 格式的文件.
这里在 windows 子系统 ubuntu 的 shell 环境

安装

  • npm
sudo apt-get install npm
  • gitbook 命令行工具
sudo npm install gitbook-cli -g

{% note warnig %} 加了 -g 参数后, 注意要加 sudo, 否则报错 {% endnote %}

npm ERR! Linux 4.4.0-43-Microsoft
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install" "fanyi" "-g"
npm ERR! node v4.2.6
npm ERR! npm  v3.5.2
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access

npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!     at Error (native)
npm ERR!  { [Error: EACCES: permission denied, access '/usr/local/lib/node_modules']
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! Please include the following file with any support request:
npm ERR!     /mnt/c/Users/mbinary/gitrepo/personal-awesome-src/public_html/htmls/npm-debug.log
  • calibre
sudo apt-get install calibre

不安装会报错

info: 7 plugins are installed 
info: 6 explicitly listed 
info: loading plugin "highlight"... OK 
info: loading plugin "search"... OK 
info: loading plugin "lunr"... OK 
info: loading plugin "sharing"... OK 
info: loading plugin "fontsettings"... OK 
info: loading plugin "theme-default"... OK 
info: found 5 pages 
info: found 4 asset files 

InstallRequiredError: "ebook-convert" is not installed.
Install it from Calibre: https://calibre-ebook.com

下载内容

将 gitbook 书籍的 github 源码 clone 下来
git clone repo

配置

如果 repo 中没有 book.json , 则需要配置生成电子书的参数,
例如

{
    "title": "python数据结构",
    "description": "problem solving ",
    "author": "zh",
    "language": "zh-hans",
    "styles": {
        "website": "style.css"
    },
    "plugins": [
        "-lunr",
        "-search",
        "-livereload",
        "-sharing",
        "book-summary-scroll-position-saver",
        "expandable-chapters",
        "search-plus",
        "splitter",
        "tbfed-pagefooter",
        "back-to-top-button",
        "yahei",
        "katex",
        "theme-api",
        "theme-comscore"
    ],
    "pluginsConfig": {
        "expandable-chapters": {},
        "theme-api": { "theme": "light" },
        "tbfed-pagefooter": {
            "copyright": "© HeDonghai",
            "modify_label": "文件修订时间:",
            "modify_format": "YYYY-MM-DD HH:mm:ss"
        },
        "fontSettings": {
            "theme": "white",
            "family": "msyh",
            "size": 2
          }
    },
    "pdf": {
        "toc": true,
        "pageNumbers": true,
        "fontSize": 18,
        "paperSize": "a4",
        "margin": {
          "right": 30,
          "left": 30,
          "top": 30,
          "bottom": 50
        }
      }
}

安装插件

最开始我直接在 任意目录 下用 gitbook install someplugin, 结果显示 nothing to install
应该在 repo 目录下 使用 gitbook install 就自动安装 book.json 中的插件了

中文字体

如果这时直接在 repo 下 gitbook pdf/mobi/epub, 生成的电子书会乱码, 因为没有对应的字体
应该这样解决
book.json 中配置了什么字体, 如上面的是 微软雅黑,
控制面板>外观和个性化>字体 中复制对应的字体文件 到 /usr/share/fonts/truetype

使用

在 repo 中 使用 gitbook pdf/mobi/epub 即可, 在当前目录下生成 book.pdf/mobi/epub

常用的功能 比如 gitbook serve 可以在本地
localhost:4000 浏览, 更多用途可以 查看gitbook help

总结

这是生成的效果


image.png

有些页面没有占满, 图片的 caption 位置移动了, 排版有点不好, (没配置好?) , 可能这就是作者没给出 pdf 版的原因?

参考

猜你喜欢

转载自blog.csdn.net/marvellousbinary/article/details/80527189
今日推荐