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 版的原因?

參考

發佈了78 篇原創文章 · 獲贊 16 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章