一、PDFium 简介
Pdfium是一个由Google开发的开源PDF渲染库,可以用来显示、处理PDF文档。它是Google Chrome浏览器中用于处理PDF文档的核心引擎。该库不只限于Chrome浏览器使用,其他开发者也可在自己的项目中使用Pdfium库。Pdfium提供了丰富的特性,如文本提取、搜索、注解、表单填充等,且性能高、稳定性好、兼容性强,被许多第三方项目所使用。
二、编译环境
1. Docker unbuntu 22.04
2. Dockerfile
Dockerfile
FROM ubuntu:22.04
RUN apt-get update
RUN apt-get install -y build-essential git subversion pkg-config python3 libtool cmake glib2.0-dev libatspi2.0-dev wget nano
RUN mkdir /pdfium_build
WORKDIR /pdfium_build
RUN git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
ENV PATH=/pdfium_build/depot_tools:${PATH}
RUN gclient config --unmanaged https://pdfium.googlesource.com/pdfium.git@9eec95782c4ecdae50cd8c8e9f88d3f8941db1cd
RUN echo "target_os = [ 'android' ]" >> .gclient
RUN gclient sync
# 可根据需要指定不同版本
RUN mv pdfium.git\@9eec95782c4ecdae50cd8c8e9f88d3f8941db1cd/ pdfium
# working dir
WORKDIR /app
3. build.sh
bash
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
set -e
build() {
local ARCH="$1"
shift
local OUT="$DIR/../libpdfsdk/jniLibs/$ARCH/libpdfsdk.so"
local OUT_UNSTRIPPED="$DIR/../libpdfsdk/jniLibs/$ARCH/libpdfsdk_unstripped.so"
[ -e "$OUT" ] && return
echo "building $OUT"
gn gen out --args="target_os=\"android\" pdf_bundle_freetype=true pdf_is_standalone=false is_component_build=false pdf_enable_xfa=false pdf_enable_v8=false is_debug=false is_official_build=true $*"
ninja -C out pdfsdk
mkdir -p $(dirname "$OUT") && cp out/libpdfsdk.so "$OUT" && cp out/lib.unstripped/libpdfsdk.so "$OUT_UNSTRIPPED"
}
build armeabi-v7a target_cpu=\"arm\" arm_version=7
build arm64-v8a target_cpu=\"arm64\"
build x86 target_cpu=\"x86\"
build x86_64 target_cpu=\"x64\"
mkdir -p "$DIR"/../libpdfsdk/jniLibs/include/ && cp "$DIR"/public/*.h "$DIR"/../libpdfsdk/jniLibs/include/
三、编译步骤
1. mkdir pdf & cd pdf
2. 将 build.sh 放入 pdf 目录
3. 将 Dockerfile 放入 pdf/docker 目录下
4. 创建docker image
bash
docker build -t pdfium-android -f ./docker
5. 运行docker并进入bash
bash
docker run -v ${PWD}:/app -it pdfium-android /bin/bash
6. 在docker的bash中进入目录
cd /pdfium_build/pdfium/
7. 编辑BUILD.gn
将
python
config("pdfium_common_config") {
cflags = []
cflags_cc = []
ldflags = []
include_dirs = [ "." ]
defines = [
]
改为
python
config("pdfium_common_config") {
cflags = [ "-fvisibility=default" ]
cflags_cc = []
ldflags = []
include_dirs = [ "." ]
defines = [
"PNG_PREFIX",
"PNG_USE_READ_MACROS",
"FPDFSDK_EXPORTS"
]
找到 component("pdfium") 节,在这节之后添加
python
shared_library("pdfsdk") {
deps = [":pdfium"]
if (target_os == "android") {
configs -= [ "//build/config/android:hide_all_but_jni_onload" ]
}
}
8. 修改 build_overrides/build.gni
将以下三行注释掉
python
if (current_cpu == "arm") {
arm_use_neon = true
}
变为
python
#if (current_cpu == "arm") {
# arm_use_neon = true
#}
9. 将build.sh拷贝至此
bash
copy /app/build.sh .
10. 运行build.sh
11. 完成后结果会输出在
bash
/pdfium_build/libpdfsdk
12. 将结果copy到app目录。这样就可以在宿主机使用了。
bash
cp -r /pdfium_build/libpdfsdk /app
See Also
https://github.com/benjinus/android-support-pdfium
https://github.com/benjinus/android-support-pdfium