# Docker镜像安装版导出PDF中文乱码问题

当前安装环境为官方docker镜像，使用的基础系统镜像为alpine-3.19。

#### 一、安装wkhtmltopdf

[![image.png](https://bookstack.freedom-yun.com/uploads/images/gallery/2024-04/scaled-1680-/Vcw2DQMwnL2nTSBp-image.png)](https://bookstack.freedom-yun.com/uploads/images/gallery/2024-04/Vcw2DQMwnL2nTSBp-image.png)

可能是因为alpine使用musl libc而不是glibc，会有很多兼容性问题，所以官方仓库中从3.15版开始已经不再包含wkhtmltopdf包，不过后来还是在docker仓库中找到还算是在维护中的镜像”[alpine-wkhtmltopdf](https://hub.docker.com/r/surnet/alpine-wkhtmltopdf)“，通过从该镜像中直接复制二进制发行包即可。

```
FROM docker.freedom-yun.com/wkhtmltopdf:alpine-3.19 as wkhtmltopdf
FROM linuxserver/bookstack:amd64-24.02.3

# Copy wkhtmltopdf files from docker-wkhtmltopdf image
COPY --from=wkhtmltopdf /bin/wkhtmltopdf /usr/bin/wkhtmltopdf
COPY --from=wkhtmltopdf /bin/wkhtmltoimage /usr/bin/wkhtmltoimage
COPY --from=wkhtmltopdf /bin/libwkhtmltox* /usr/bin/

# 安装依赖库
RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g" /etc/apk/repositories && \
    apk add --no-cache \
    libstdc++ \
    libx11 \
    libxrender \
    libxext \
    libssl3
```

[【DockerHub】 - apline-wkhtmltopdf](https://hub.docker.com/r/surnet/alpine-wkhtmltopdf)

[![image.png](https://bookstack.freedom-yun.com/uploads/images/gallery/2024-04/scaled-1680-/qnE8bPsPvTEMzcsz-image.png)](https://bookstack.freedom-yun.com/uploads/images/gallery/2024-04/qnE8bPsPvTEMzcsz-image.png)

#### 二、安装中文字体

将需要使用到的字体文件复制到”`/usr/share/fonts/`“目录下，然后使用以下命令检测是否有效。

```
fc-list :lang=zh
```

中间我尝试了其他alpine安装中文字体的文章但都没有效果，最后在另外一台linux服务器上找到以前不知道什么时候在windows下复制的字体文件，复制到”`/usr/share/fonts/chinese`“目录后检测成功，当前使用到中文字体已经上传到了个人maven仓库([chinese-fonts-10.0.0.tar.gz](https://registry.freedom-yun.com/repository/public-archives/com/microsoft/fonts/chinese-fonts/10.0.0/chinese-fonts-10.0.0.tar.gz))中方便后续下载。

[![image.png](https://bookstack.freedom-yun.com/uploads/images/gallery/2024-04/scaled-1680-/PooQe68ZkJIzSA1W-image.png)](https://bookstack.freedom-yun.com/uploads/images/gallery/2024-04/PooQe68ZkJIzSA1W-image.png)

#### 三、制作Docker镜像

将第一步和第二步的内容合并最终生成如下Dockerfile

```
FROM surnet/alpine-wkhtmltopdf:3.19.0-0.12.6-full as wkhtmltopdf
FROM linuxserver/bookstack:amd64-24.02.3

# Copy wkhtmltopdf files from docker-wkhtmltopdf image
COPY --from=wkhtmltopdf /bin/wkhtmltopdf /usr/bin/wkhtmltopdf
COPY --from=wkhtmltopdf /bin/wkhtmltoimage /usr/bin/wkhtmltoimage
COPY --from=wkhtmltopdf /bin/libwkhtmltox* /usr/bin/

# 下载中文字体
ADD https://registry.freedom-yun.com/repository/public-archives/com/microsoft/fonts/chinese-fonts/10.0.0/chinese-fonts-10.0.0.tar.gz /usr/share/fonts/

# 安装wkhtmltopdf运行时依赖库
RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g" /etc/apk/repositories && \
    apk add --no-cache \
    libstdc++ \
    libx11 \
    libxrender \
    libxext \
    libssl3 && \
    tar -zxvf /usr/share/fonts/chinese-fonts-10.0.0.tar.gz -C /usr/share/fonts/ && \
    rm -f /usr/share/fonts/chinese-fonts-10.0.0.tar.gz
```

#### 四、启用wkhtmltopdf

修改“`${project_dir}/config/www/.env`”文件，添加如下配置

```
WKHTMLTOPDF=/usr/bin/wkhtmltopdf
ALLOW_UNTRUSTED_SERVER_FETCHING=true
```

##### 参考资料

- [【CSDN】 - alpinelinux镜像安装中文字体](https://blog.csdn.net/m0_60028455/article/details/125931616)
- [【CSDN】 - Linux下查看已安装字体的方法](https://blog.csdn.net/dmcpxy/article/details/9131181)
- [【wkhtmltopdf】 - 官方网站](https://wkhtmltopdf.org/)
- [【DockerHub】- surnet/alpine-wkhtmltopdf](https://hub.docker.com/r/surnet/alpine-wkhtmltopdf)
- [【book.fanfou.dev】 - BookStack 导出中文 PDF](https://book.fanfou.dev/books/how-to-do/page/bookstack-pdf)
- [【Github】 - wkhtmltopdf](https://github.com/wkhtmltopdf/wkhtmltopdf)
- [【Github】 - docker-wkhtmltopdf](https://github.com/Surnet/docker-wkhtmltopdf)
- [【Alpine】 - Alpine Linux packages](https://pkgs.alpinelinux.org/packages)
- [【Maven仓库】 - chinese-fonts-10.0.0.tar.gz](https://registry.freedom-yun.com/repository/public-archives/com/microsoft/fonts/chinese-fonts/10.0.0/chinese-fonts-10.0.0.tar.gz)