There are many ways to export PDF. The export function of MAC Flyby has been used before, but a simple export cannot add a directory, because the source file is written by Markdown. After screening, gitbook is used to write the document and Gitbook PDF is used. Is exported as a PDF file.

Note: This machine requires a NodeJs environment.

Blog post: blog.yuqiyu.com/use-gitbook…

Environment to prepare

If you want to use Gitbook, you need to install it on your computer, which can be easily installed through the NPM command.

Install GitBook

To install the Gitbook environment through NPM, run the following command:

npm install gitbook -gCopy the code

Install Calibre & EBOOK-convert

The export function of Gitbook requires the support of the third-party plug-in ebook. The following describes how to configure the environment for two different operating systems.

Linux system

Download: calibre-ebook.com/download_li…

  • Download and Install
  sudo -v && wget -nv -O- https://download.calibre-ebook.com/linux-installer.sh | sudo sh /dev/stdinCopy the code

  • Configuring soft Links
  sudo ln -s /usr/bin/nodejs /usr/bin/nodeCopy the code

Mac system

Download: calibre-ebook.com/download_os…

  • Download and Install

Download the DMG file and double-click it to install it.

  • Configuring soft Links
  sudo ln -s ~/Applications/calibre.app/Contents/MacOS/ebook-convert /usr/binCopy the code

Test the installation

After the installation, run the following command to check whether the installation takes effect:

ebook-convert --versionCopy the code

Generating documentation

A new Gitbook document consists of two files, readme.md and summary.md (you can create your own folder and create both files within the folder).

  • README.md: A detailed description of the current document
  • SUMMARY.md: Directory hierarchy configuration of the current document, which can be directly generated by using the initialization commandmarkdownFiles and folders.

Write the Summary

Here is a sample document hierarchy:

# Summary - Level 1 directory - [level 1 subdirectory](one/first.md) - Level 2 directory - [Level 2 subdirectory](two/first.md)Copy the code

GitBook initialization

Gitbook provides an initialization command that automatically generates the corresponding MD file and folder according to the hierarchical content of the summary. md file. Run the following command:

➜ gitbook-example gitbook init
info: create one/first.md 
info: create two/first.md 
info: create SUMMARY.md 
info: initialization is finished Copy the code

The console output already tells us that the one/first.md and two/first.md files were created successfully.

Configuration language

The default language of Gitbook is not Chinese by default. We need to configure the book.json file to change the default language, as shown below:

{
    "language": "zh-hans"
}Copy the code

Export document

There are many ways to export documents from Gitbook. Here are a few of them.

Export to PDF

Execute gitbook PDF in the same directory as the summary.md file. Run the following command to export the PDF file:

➜ gitbook-example gitbook pdf.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 3 pages info: found 0 asset Files Info: >> Generation finished with success in 5.7s! info: >> 1 file(s) generatedCopy the code

If you want to customize the generated PDF file name, you can use the gitbook PDF.. /xxxx.pdf command.

Export to epub

Execute gitbook epub in the same directory as the summary.md file. Run the following command to export the epub file:

➜ gitbook-example gitbook epub.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 3 pages info: found 2 asset Files Info: >> Generation finished with success in 2.4s! info: >> 1 file(s) generatedCopy the code

Exported as.mobi

Execute gitbook mobi in the same directory as the summary.md file. Command to export the MOBI file. The execution log is as follows:

➜ gitbook-example gitbook mobi. 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 3 pages info: found 3 asset Files Info: >> Generation finished with success in 1.9s! info: >> 1 file(s) generatedCopy the code

Exporting Logs

If you are having problems exporting, you can add the –log=debug command parameter when executing the export command, so that you can see the complete log information output on the console when exporting, as shown below:

➜ gitbook-example gitbook PDF.. /example.pdf --log=debug debug: readme found at readme.md debug: summary file found at SUMMARY.md debug: cleanup folder "/var/folders/c1/5mrhntb13_zfrnjg4grnf8zr0000gn/T/tmp-2291a4Jd8P8oNX4l" ......Copy the code

conclusion

usegitbookIt can be used to write corporate interface usage documents, project design documents, and much moregitbook buildCommand to generate statichtmlFiles that can be deployed toNginx,Ali cloud OSSWhere static pages are hosted.

Author’s Personal blog

Use the open source framework ApiBoot to help you become an Api service architect