福昕 PDF SDK Python 库
本节主要介绍如何使用福昕 PDF SDK Python 库运行示例及创建一个基础的 Python 工程。该工程演示如何将 PDF 文档的首页渲染成 bitmap,并将其另存为 JPG 图片。
先决条件
开发环境
- Python 版本: Python 2.7 或 Python 3.6-3.10(请确保
python
命令指向您的首选版本,并已将其添加到系统环境变量PATH
中)。 - 福昕 PDF SDK Python 库: 请从福昕官方网站下载并安装与您的操作系统和架构匹配的 Python 库。
系统支持
我们提供 Windows、Linux 和 Mac 平台的详细系统支持信息,包括操作系统版本、编译器要求等。请选择您的操作系统平台,查看详细信息。
Windows 平台 Linux 平台 Mac 平台配置
安装福昕 SDK Python 模块
python
pip install FoxitPDFSDKPython3
安装第三方模块
为了确保顺利运行 Security Demo
和 Signature Demo
,请按以下指南安装第三方模块。
python
pip install cryptography==2.3
pip install pyopenssl==19.0.0
pip install uuid
配置 Python 库
为确保福昕 PDF SDK Python 2.7 库在 Windows 或 Linux 平台正确运行,请根据您的 Python 版本(32 位或 64 位)执行相应的库文件拷贝操作。
Windows 用户:
- 若使用 32 位 Python 2.7,请将
FoxitPDFSDKPython2/x86_vc15/_fsdk.pyd
手动复制到FoxitPDFSDKPython2/
目录下。 - 若使用 64 位 Python 2.7,请将
FoxitPDFSDKPython2/x64_vc15/_fsdk.pyd
手动复制到FoxitPDFSDKPython2/
目录下。
Linux 用户:
- 针对 32 位 Python 2.7,请将
FoxitPDFSDKPython2/x86/_fsdk.so
手动复制到FoxitPDFSDKPython2/
目录下。 - 针对 64 位 Python 2.7,请将
FoxitPDFSDKPython2/x64/_fsdk.so
手动复制到FoxitPDFSDKPython2/
目录下。
或者,您可以通过运行 examples/simple_demo/rundemo_python.py
脚本实现库文件的自动拷贝。
运行示例
福昕PDF SDK 提供了 Python 脚本来运行 Python 示例。请按以下指南操作,来快速启动并运行相关示例。
使用 Python 脚本运行示例
rundemo_python.py
脚本封装了示例运行的配置和依赖处理,简化了示例的运行流程。
运行所有示例
python
cd examples/simple_demo/
python rundemo_python.py
运行某个特定示例
例如,运行 annotation 示例:
python
cd examples/simple_demo/
python rundemo_python.py annotation
直接使用示例脚本运行
如果您已正确配置 Python 环境和依赖,可以直接运行示例脚本。以下命令演示如何运行 annotation 示例:
python
cd examples/simple_demo/annotation/
python -u annotation.py
NOTE
- Python 2.7 用户,参阅 配置 Python 库,确保库文件与 Python 版本对应。
- Python 3 用户,如已安装 FoxitPDFSDKPython3 模块,可以直接运行示例脚本。
快速创建工程
创建项目目录并准备示例文件
- 创建项目文件夹: 新建一个名为
test
的项目文件夹。 - 复制示例数据: 将
/examples/simple_demo/input_files
文件夹下的SamplePDF.pdf
文件复制到test
夹下,作为工程的输入文件。
- 创建项目文件夹: 新建一个名为
配置 Python 库
- 请参阅 配置 Python 库。
创建并运行工程脚本
- 创建示例工程脚本: 在
test
文件夹下,创建一个名为test.py
的 Python 脚本文件,并添加示例代码。示例代码如下:
[test.py]
pythonimport sys import site import platform from shutil import copyfile if sys.version_info.major == 2: _PYTHON2_ = True else: _PYTHON2_ = False # For Python2, copy the corresponding version of the dynamic library to the folder FoxitPDFSDKPython2. if _PYTHON2_: arch = platform.architecture() if arch[0] == "32bit": src_lib_path = "./FoxitPDFSDKPython2/x86_vc15/_fsdk.pyd" elif arch[0] == "64bit": src_lib_path = "./FoxitPDFSDKPython2/x64_vc15/_fsdk.pyd" dest_lib_path = "./FoxitPDFSDKPython2/_fsdk.pyd" if src_lib_path is not None: copyfile(src_lib_path, dest_lib_path) if _PYTHON2_: site.addsitedir('./') from FoxitPDFSDKPython2 import * else: from FoxitPDFSDKPython3 import * # Assuming PDFDoc doc has been loaded. # The value of "sn" can be got from "gsdk_sn.txt" (the string after "SN="). # The value of "key" can be got from "gsdk_key.txt" (the string after "Sign="). sn = " " key = " " def main(): # Load a PDF document, and parse the first page of the document. doc = PDFDoc("SamplePDF.pdf") error_code = doc.Load("") if error_code!= e_ErrSuccess: return 0 page = doc.GetPage(0) page.StartParse(PDFPage.e_ParsePageNormal, None, False) width = int(page.GetWidth()) height = int(page.GetHeight()) matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation()) # Prepare a bitmap for rendering. bitmap = Bitmap(width, height, Bitmap.e_DIBArgb) bitmap.FillRect(0xFFFFFFFF, None) # Render page. render = Renderer(bitmap, False) render.StartRender(page, matrix, None) # Add the bitmap to image and save the image. img = Image() img.AddFrame(bitmap) img.SaveAs("testpage.jpg") return 0 if __name__ == '__main__': code = Library.Initialize(sn, key) if code == e_ErrSuccess: main() Library.Release()
- 运行示例工程脚本: 打开命令行终端,导航到
test
文件夹,然后运行命令python test.py
。 - 验证输出: 如果示例脚本成功运行,将在当前文件夹下生成
testpage.jpg
文件。
- 创建示例工程脚本: 在