简介
PDF是OpenHarmony系统中用来管理PDF文档的预览和编辑,包括文档预览、编辑、注释、转换等功能,为鸿蒙应用开发者提供完整的PDF处理能力。
PDF包含PDF Service和PDF View组件。
- PDF View:
PDF View提供简单易用的PDF预览组件,可以直接在ArkTS中进行调用。支持滑动浏览、缩放、单/双页显示、连续/非连续显示等基础浏览功能,支持文本选择、图像选择、文本搜索等功能,以及添加批注功能。 - PDF Service:
PDF Service提供丰富的PDF文档操作、编辑功能,覆盖大部分PDF办公场景。支持加密文档、保存文档等文档操作,支持删除页面、移动页面等页面操作,支持添加水印、页眉/页脚、背景图片等文档编辑能力。
整体架构
图1 PDF整体架构图
模块功能说明
整体结构采用分层思想,划分为ArkTS层、NAPI层、Native层、Adapter层、PDFium核心库。
- ArkTS层 负责PDF交互与展示
- PdfView 基于
XComponent的 PDF 查看组件,采用混合架构结合了 ArkTS 和 Native C++ 实现。它通过XComponent作为桥梁连接前端 UI 和底层 PDF 渲染引擎。
- PdfView 基于
- NAPI层 负责桥接接口声明,类型导出。
- 桥接通信 定义接口和绑定接口,达到桥接ArkTS层和Native层的作用,主要封装
PDFViewManager相关接口和PDFService相关接口,以及一些类,结构体导出。
- 桥接通信 定义接口和绑定接口,达到桥接ArkTS层和Native层的作用,主要封装
- Native层 负责渲染,合成,处理ArkTS层传递过来的事件。
- PDF渲染引擎 Native 侧主要以
PdfRenderEngine协调ViewPortManager(视窗管理)、Compositor(页面合成)、RenderQueue(渲染队列)、Controller(渲染交互控制)等实现页面排版与位图绘制,回传页面事件与文本选择等结果。 跟PDFium库通信获取实际的文档数据,如pixelmap或者页面对象等。
- PDF渲染引擎 Native 侧主要以
- Adapter层 负责PDFium库的兼容处理。
- 平台适配 PDF的核心实现层,负责封装PDFium原生库的功能,提供统一的C++接口给Native层调用。
- PDFium核心库 负责完成PDF操作。
- 适配OpenHarmony的PDFium 整个PDF功能开发套件的底层基础,采用Google开源的PDFium引擎作为PDF处理的核心。本层不对PDFium库进行过多侵入式修改,仅通过其公开的C API接口进行调用。
目录
仓目录结构如下:
/foundation/filemanagement/pdf_service
├── BUILD.gn # 编译规则文件
├── bundle.json # 部件注册文件
├── common # 通用功能模块
├── framework # 框架层实现
│ ├── adapter # 适配层
│ ├── business # 业务模块
│ └── render # 渲染模块
├── interface # 对外接口 napi capi
├── pdfview # PdfView模块
└── figures # 架构图等资源文件
集成PDF
cd {your_openharmony_path}/foundation/filemanagement
git clone https://gitee.com/harmony_tsc/filemanagement_pdf_service.git
mv filemanagement_pdf_service pdf_service
将源码放置在路径foundation/filemanagement目录下并将filemanagement_pdf_service改名为pdf_service。
并在vendor/hihope/rk3568/config.json配置pdf_service为filemanagement子系统的一个组件。
{
"subsystem": "filemanagement",
"components": [
{
"component": "pdf_service",
"features": []
}
]
}
pdf_service组件会外部依赖pdfium组件,所以先要配置并编译pdfium完成后,再编译pdf_service。
编译pdf_service命令
./build.sh --product-name rk3568 --build-target pdf_service -ccache
主要的产物位于:
out/rk3568/filemanagement/pdf_service/libpdfservice_napi.z.so
out/rk3568/filemanagement/pdf_service/libpdfview.z.so
out/rk3568/filemanagement/pdf_service/libpdfinner.z.so
out/rk3568/filemanagement/pdf_service/pdf_inner_test
将libpdfservice_napi.z.so与libpdfview.z.so导入到开发板中进行测试与开发。
将so库推入到开发板中:
hdc shell mount -o rw,remount /
hdc file send libpdfservice_napi.z.so /system/lib/module/hms/officeservice
hdc file send libpdfview.z.so /system/lib/module/hms/officeservice
hdc shell reboot
libpdfinner.z.so是对外提供接口访问的库,可以使用pdf_inner_test执行文件来进行测试,例如包含提供c语言调用PDF的功能的capi接口。
- 编译异常:
使用源码编译PDF和PDFium组件时不要加上参数
--gn-args ohos_components_checktype=0的属性,否则可能出现device.gni导入错误,导致其他组件编译失败,目前删除out目录后重新编译可以解决。
相关仓
编译说明
1.请将下载好的third_party_pdfium文件夹放入到系统源码根目录下的third_party,并且将third_party_pdfium文件名更换为pdfium。
2.在vendor/hihope/rk3568/config.json文件中找到"subsystem": "thirdparty",并在其"components"下将pdfium组件注册进去。
{
"subsystem": "thirdparty",
"components": [
{
"component": "pdfium",
"features": []
},
]
}
3.在build/compile_standard_whitelist.json中的最后一项"third_deps_bundle_not_add"中添加以下条目后,配置完成。
"third_deps_bundle_not_add": [
"//third_party/pdfium/core/fxcodec:fxcodec",
"//third_party/pdfium/core/fpdfapi/page:page",
"//third_party/pdfium/core/fpdfapi/parser:parser",
"//third_party/pdfium/core/fxge:fxge",
"//third_party/pdfium/core/fxcrt:unowned_ptr",
"//third_party/pdfium/fpdfsdk:fpdfsdk",
"//third_party/pdfium/core/fpdfdoc:fpdfdoc",
"//third_party/pdfium/fxjs:fxjs",
"//third_party/pdfium/fpdfsdk/formfiller:formfiller",
"//third_party/pdfium/third_party/libjpeg_turbo:libjpeg",
"//third_party/pdfium/third_party/icu:icuuc_private",
"//third_party/pdfium/third_party/libjpeg_turbo:simd",
"//third_party/pdfium/core/fpdfapi/edit:edit",
"//third_party/pdfium/core/fpdfapi/font:font",
"//third_party/pdfium/core/fpdfapi/cmaps:cmaps",
"//third_party/pdfium/core/fpdfapi/render:render",
"//third_party/pdfium/core/fdrm:fdrm",
"//third_party/pdfium/core/fpdftext:fpdftext",
"//third_party/pdfium/fpdfsdk/pwl:pwl",
"//third_party/pdfium/third_party:fx_libpng"
]
基本命令格式
编译pdfium组件
./build.sh --product-name rk3568 --build-target pdfium -ccache
编译输出
编译成功后在目录下生成以下4个so库
out/rk3568/thirdparty/pdfium/libpdfium.z.so
out/rk3568/thirdparty/pdfium/libicuuc.z.so
out/rk3568/thirdparty/pdfium/libchrome_zlib.z.so
out/rk3568/thirdparty/pdfium/libabsl.z.so
将4个so库下载好后,pc端新建xx.txt文件,将如下内容输入进去后保存为xx.bat文件,连接好开发板,双击xx.bat文件使用脚本将4个so库推入到开发板中,注意请调整本地so库所在的路径。
hdc shell mount -o rw,remount /
hdc file send libchrome_zlib.z.so /system/lib
hdc file send libicuuc.z.so /system/lib
hdc file send libpdfium.z.so /system/lib
hdc file send libabsl.z.so /system/lib
hdc shell reboot
pause
后续根据修改pdfium的源码,推入指定的so文件更新即可。