更新记录

1.0.0(2026-07-04) 下载此版本

初始版本


平台兼容性

uni-app(4.25)

Vue2 Vue2插件版本 Vue3 Vue3插件版本 Chrome Safari app-vue app-vue插件版本 app-nvue app-nvue插件版本 Android Android插件版本 iOS iOS插件版本 鸿蒙 鸿蒙插件版本
1.0.0 1.0.0 - - 1.0.0 1.0.0 5.0 1.0.0 12 1.0.0 12 1.0.0
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 小红书小程序 快应用-华为 快应用-联盟
- - - - - - - - - - - -

uni-app x(4.25)

Chrome Safari Android Android插件版本 iOS iOS插件版本 鸿蒙 鸿蒙插件版本 微信小程序
- - 5.0 1.0.0 12 1.0.0 12 1.0.0 -

其他

多语言 暗黑模式 宽屏模式
×

xyz-pdf

将本地图片拼接生成 PDF 文件的 UTS 插件,支持 Android、iOS、HarmonyOS 三端。

功能

  • 将多张本地图片(jpg/png)按序生成为多页 PDF
  • 图片等比缩放居中适配页面(contain 模式)
  • 可配置页面尺寸(默认 A4)和边距
  • 支持多次 addImages 追加页面
  • 生成的 PDF 保存到临时目录,返回文件路径

安装

uni_modules/xyz-pdf 目录复制到项目的 uni_modules 下即可。

基本用法

import { XyzPdfDocument } from "@/uni_modules/xyz-pdf"

// 1. 创建文档实例
const doc = new XyzPdfDocument({
  pageSize: { width: 595, height: 842 }, // A4,可选
  margin: 20 // 页面边距(pt),可选,默认 0
})

// 2. 添加图片页
doc.addImages({
  images: ['/path/to/img1.png', '/path/to/img2.jpg'],
  success: (res) => {
    console.log(res.errMsg) // 'addImages:ok'

    // 3. 保存 PDF
    doc.save({
      success: (saveRes) => {
        console.log('PDF 路径:', saveRes.tempFilePath)
        // 可通过 uni.openDocument 预览
        // 可通过 uni.getFileSystemManager() 保存到指定位置
      },
      fail: (err) => {
        console.error(err.errCode, err.errMsg)
      }
    })
  },
  fail: (err) => {
    console.error(err.errCode, err.errMsg)
  }
})

API

new XyzPdfDocument(options?)

创建 PDF 文档实例。

参数 类型 默认值 说明
options.pageSize PdfPageSize { width: 595, height: 842 } 默认页面尺寸(pt)
options.margin number 0 默认页面边距(pt)

doc.addImages(options)

批量添加图片页,每张图片占一页,等比缩放居中。可多次调用追加页面。

参数 类型 必填 说明
options.images string[] 本地图片路径数组
options.pageSize PdfPageSize 覆盖本次添加的页面尺寸
options.margin number 覆盖本次添加的页面边距
options.success Function 成功回调
options.fail Function 失败回调
options.complete Function 完成回调

doc.save(options)

保存 PDF 到临时文件。

参数 类型 必填 说明
options.success Function 成功回调,返回 { tempFilePath: string }
options.fail Function 失败回调
options.complete Function 完成回调

图片要求

  • 支持格式:jpg/jpeg、png
  • 图片必须为本地路径(网络图片请先通过 uni.getImageInfo 下载到本地)
  • 截图路径(takeSnapshot 返回的 tempFilePath)可直接使用
  • 图片以等比缩放适配方式放置(保持宽高比,居中,可能留白)
  • 如需 cover/fill 效果,请先裁剪图片到目标比例后传入

错误码

错误码 类别 说明
9003001 参数错误 images 参数为空或未提供
9003002 参数错误 无效的页面参数(pageSize 或 margin)
9003100 文件错误 图片文件不存在
9003101 文件错误 图片解码失败(格式不支持或文件损坏)
9003200 生成错误 PDF 文档创建失败
9003201 生成错误 PDF 写入磁盘失败
9003900 状态错误 文档无页面内容(save 时无已添加的页面)

完整示例

import { XyzPdfDocument } from "@/uni_modules/xyz-pdf"

// 截图 + 选图 → 生成 PDF → 打开预览
const imagePaths: string[] = []

// 添加截图
const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
currentPage.takeSnapshot({
  success: (res) => {
    imagePaths.push(res.tempFilePath)
  }
})

// 添加相册图片
uni.chooseImage({
  count: 9,
  success: (res) => {
    imagePaths.push(...res.tempFilePaths)
  }
})

// 生成 PDF
const doc = new XyzPdfDocument({ margin: 20 })
doc.addImages({
  images: imagePaths,
  success: () => {
    doc.save({
      success: (res) => {
        // 打开预览
        uni.openDocument({
          filePath: res.tempFilePath,
          fileType: 'pdf'
        })
      }
    })
  }
})

注意事项

  • 生成的文件存储在系统临时目录,如需持久保存请使用 uni.getFileSystemManager() 复制到指定路径
  • 图片数量较多时可能占用较多内存,建议控制单次 addImages 的数量
  • 页面尺寸单位为 pt(1pt = 1/72 inch),A4 = 595×842pt

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT协议