This article is published under a SIGNATURE 4.0 International (CC BY 4.0) license. Signature 4.0 International (CC BY 4.0)

Author: Su Yang

Creation time: on May 7, 2019 statistical word count: 3277 words reading time: 7 minutes to read this article links: soulteary.com/2019/05/07/…


Generate GitBook PDF using the Docker wrapper Python gadget

It is well known that the new VERSION of GitBook generates PDFS using Calibre’s Ebook-convert module, which by default generates LARGE PDFS and does not support compression, which is very bad for propagation.

After a simple search, I found that Fuergaosi233 wrote a simple GitBook PDF generation tool based on Weastprint in Python, which felt good when used, so I packaged this container image, hoping to help you with the same needs.

This article will show you how to package a simple Python application as a Docker tool image and use it to generate a PDF file in less than 10 minutes.

The complete project code, which I have uploaded to: github.com/soulteary/d… , students with customized needs can help themselves.

Lead to

Before you start using it, you need two things.

  • Docker
  • Font file you like (if you need to spread the generated ebook, pay attention to copyright risk oh)
    • For example: Apple square, source,…

Once the container environment is installed and the font is ready, we can wrap the container. If you don’t care about the packaging details and just want to use it, you can jump to the “How to Use it” section.

Packaging containers

Since the ebook generation tool we used was written in Python, for faster encapsulation (without messing around with basic tools like PIP), I used a relatively compact Python: 3.7-Alpine3.9 Base image, and the encapsulation command was as simple as ten lines or so.

The FROM python: 3.7 - alpine3.9 LABEL MAINTAINER ="soulteary <[email protected]>"

ENV LIBRARY_PATH /lib:/usr/lib

RUN wget https://github.com/soulteary/gitbook2pdf/archive/master.zip -O /tmp/app.zip && \
    cd /tmp && unzip app.zip && mv /tmp/gitbook2pdf-master /app

RUN apk add build-base python3-dev gcc musl-dev jpeg-dev zlib-dev libffi-dev cairo-dev pango-dev gdk-pixbuf-dev libxslt-dev && \
    cd /app && pip install -r /app/requirements.txt && \
    apk del build-base && rm -rf /var/cache/apk/*

VOLUME [ "/app/output" ]
VOLUME [ "/usr/share/fonts/" ]

WORKDIR /app

ENTRYPOINT [ "python"."/app/gitbook.py" ]
Copy the code

As you can see from the above, the encapsulation logic is also quite simple:

  • Download the code (I forked the author’s repository in case there was a break change later)
  • After installing the build dependencies, project execution dependencies, download the project dependencies package, perform the build, and then clean up the build dependencies that are no longer used
  • Declare where files can be mounted, switch working directories, declare container entry points (default command)

If we build on the server side, because most servers have good network conditions, we can get results quickly. But if we choose to build locally, when network conditions are not as good, our access to alpine, Python PIP software sources is not as fast, and building the image will be extremely slow.

At this point, Mirror can be used to speed up the build, and the build command above can be changed to the following:

The FROM python: 3.7 - alpine3.9 LABEL MAINTAINER ="soulteary <[email protected]>"

ENV LIBRARY_PATH /lib:/usr/lib

RUN wget https://github.com/soulteary/gitbook2pdf/archive/master.zip -O /tmp/app.zip && \
    cd /tmp && unzip app.zip && mv /tmp/gitbook2pdf-master /app

RUN cat /etc/apk/repositories | sed -e "s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/" | tee /etc/apk/repositories && \
    apk add build-base python3-dev gcc musl-dev jpeg-dev zlib-dev libffi-dev cairo-dev pango-dev gdk-pixbuf-dev libxslt-dev && \
    cd /app && pip install -i https://mirrors.aliyun.com/pypi/simple/ -r /app/requirements.txt && \
    apk del build-base && rm -rf /var/cache/apk/*

VOLUME [ "/app/output" ]
VOLUME [ "/usr/share/fonts/" ]

WORKDIR /app

ENTRYPOINT [ "python"."/app/gitbook.py" ]
Copy the code

Of course, you can also according to their actual situation, the above Ali Cloud software source to replace tsinghua source, or their own source, to get a faster build experience.

Save the above content as a Dockerfile, then execute the docker build -t gitbook.

Next, let’s talk about usage.

Method of use

We create a folder called fonts in the current directory, and put the fonts we already have in it. If we don’t do this, the resulting e-book will show a lot of “mouths” because the fonts are missing.

Then you can choose to use the image we built above, or the image I prepared for you to start the ebook generation operation.

For example, we will http://self-publishing.ebookchain.org web content into e-books, only need to perform the following command:

docker run --rm -v `pwd`/fonts:/usr/share/fonts \
                -v `pwd`/output:/app/output \
                soulteary/docker-gitbook-pdf-generator "http://self-publishing.ebookchain.org"
Copy the code

If you built the container image yourself in the previous step, soulteary/ Docker-gitbook-pdF-generator can be replaced with gitbook.

Wait a moment and you will see the log message:

crawl : all done!
Generating pdf,please wait patiently
Generated
Copy the code

At the same time, your current directory will automatically create a new directory called Output, and the ebook we want to generate will already be sitting in it.

If you find the above command too complex and prefer docker-compose to simplify operations, you can use the following configuration:

version: '2'

services:

  pdf-generator:
    image: soulteary/docker-gitbook-pdf-generator
    volumes:
      - ./output:/app/output:rw
      - ./fonts/:/usr/share/fonts:ro
    Replace the URL below with the address where you want to generate the ebook
    command: "http://self-publishing.ebookchain.org"
Copy the code

Save the above content as docker-compose. Yml and execute docker-compose up to wait for the ebook to be generated.

other

If you have additional customization requirements for the style of the generated ebook, you can modify the /app/gitbook.css style file using file mount.

Thanks to Fuergaosi233 for his open source project. There are still several todo unfinished in his project. If you are interested, you can give him PR to make the tool more useful.

I’ll leave it there.

– EOF


I now have a small toss group, which gathered some like to toss small partners.

In the case of no advertisement, we will talk about software, HomeLab and some programming problems together, and also share some technical salon information in the group from time to time.

Like to toss small partners welcome to scan code to add friends. (Please specify source and purpose, otherwise it will not be approved)

All this stuff about getting into groups