更新记录

1.0.0(2026-05-16)

  • 发布 HTML / Markdown 转 PDF、DOCX 的基础能力。
  • 支持 Android、iOS、HarmonyOS 和 Web 端常用导出场景。
  • 提供文件输入、进度回调、取消转换、能力判断、Web Blob URL 释放和调试日志开关。
  • 补充使用文档、错误码和平台限制说明。

平台兼容性

uni-app(5.07)

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

uni-app x(5.07)

Chrome Safari Android iOS 鸿蒙 微信小程序
-

hans-docify

hans-docify 是一个 HTML / Markdown 转 PDF、DOCX 的 uni-app / uni-app x UTS 插件,适合在 App、HarmonyOS 和 Web 端生成报告、合同、说明文档等文件。

支持平台

平台 PDF DOCX
uni-app App Android / uni-app x App Android 支持 支持
uni-app App iOS / uni-app x App iOS 支持 支持
uni-app x App HarmonyOS 支持,需 API 14+ 支持
uni-app Web / uni-app x Web 支持,返回 Blob URL 支持,返回 Blob URL

调用前可以用 isDocifySupportedSync(format) 做能力判断。

import { isDocifySupportedSync } from '@/uni_modules/hans-docify'

const canExportPdf = isDocifySupportedSync('pdf')
const canExportDocx = isDocifySupportedSync('docx')

安装与导入

把插件放入项目的 uni_modules/hans-docify 目录后,在页面或业务模块中导入需要的 API:

import {
  convertDocument,
  convertHtmlToPdf,
  convertMarkdownToPdf,
  convertHtmlToDocx,
  convertMarkdownToDocx,
  revokeObjectUrl,
} from '@/uni_modules/hans-docify'

页面里也可以直接从插件入口导入公共类型,不需要引用 utssdk/interface.uts

import type {
  DocifyChartSnapshotOptions,
  DocifyConvenienceOptions,
  DocifyConvertOptions,
  DocifyConvertResult,
  DocifyFail,
  DocifyMargins,
  DocifyProgress,
  DocifyStyleOptions,
} from '@/uni_modules/hans-docify'

const margins: DocifyMargins = { top: 24, right: 24, bottom: 24, left: 24 }

const style: DocifyStyleOptions = {
  title: '报告标题',
  pageSize: 'A4',
  margins,
}

const chartSnapshot: DocifyChartSnapshotOptions = {
  selector: '#chart',
  pixelRatio: 2,
  backgroundColor: '#ffffff',
}

const options: DocifyConvenienceOptions = {
  source: '<h1>报告</h1>',
  fileName: 'report.pdf',
  style,
  chartSnapshot,
  progress: (res: DocifyProgress) => {
    console.log(res.stage, res.progress)
  },
  success: (res: DocifyConvertResult) => {
    console.log(res.filePath)
  },
  fail: (err: DocifyFail) => {
    console.error(err.errCode, err.errMsg)
  },
}

convertHtmlToPdf(options)

const documentOptions: DocifyConvertOptions = {
  sourceType: 'html',
  source: '<h1>报告</h1>',
  format: 'docx',
  fileName: 'report.docx',
  style,
}

convertDocument(documentOptions)

快速开始

import { convertMarkdownToPdf, revokeObjectUrl } from '@/uni_modules/hans-docify'

const task = convertMarkdownToPdf({
  source: '# 报告\n\n- PDF\n- DOCX',
  fileName: 'report.pdf',
  progress: (res) => {
    console.log(res.stage, res.progress)
  },
  success: (res) => {
    console.log(res.filePath)

    // Web 端返回 Blob URL,不再使用时建议释放。
    if (res.filePath.startsWith('blob:')) {
      revokeObjectUrl(res.filePath)
    }
  },
  fail: (err) => {
    console.error(err.errCode, err.errMsg)
  },
})

// 如需取消转换:
// task.abort()

API

convertHtmlToPdf(options)

把 HTML 字符串转成 PDF。

convertHtmlToPdf({
  source: '<h1>月度报告</h1><p>这里是正文。</p>',
  fileName: 'monthly-report.pdf',
  style: {
    title: '月度报告',
    pageSize: 'A4',
    orientation: 'portrait',
    margins: { top: 24, right: 24, bottom: 24, left: 24 },
  },
  success: (res) => {
    console.log(res.filePath)
  },
})

convertMarkdownToPdf(options)

把 Markdown 字符串转成 PDF。

convertMarkdownToPdf({
  source: '# 会议纪要\n\n1. 议题一\n2. 议题二',
  fileName: 'meeting.pdf',
  success: (res) => {
    console.log(res.filePath)
  },
})

convertHtmlToDocx(options)

把 HTML 字符串转成 DOCX。

convertHtmlToDocx({
  source: '<h1>合同</h1><p><strong>甲方:</strong>示例公司</p>',
  fileName: 'contract.docx',
  style: {
    title: '合同',
    defaultFontFamily: 'Microsoft YaHei',
    defaultFontSize: 12,
  },
  success: (res) => {
    console.log(res.filePath)
  },
})

convertMarkdownToDocx(options)

把 Markdown 字符串转成 DOCX。

convertMarkdownToDocx({
  source: '# 产品说明\n\n支持标题、段落、列表、表格和链接。',
  fileName: 'product-guide.docx',
  success: (res) => {
    console.log(res.filePath)
  },
})

convertDocument(options)

通用转换 API。需要读取本地 .html.htm.md.markdown 文件时使用 sourceType: 'file'

convertDocument({
  sourceType: 'file',
  source: '/path/to/input.md',
  inputFormat: 'markdown',
  format: 'docx',
  fileName: 'from-file.docx',
  success: (res) => {
    console.log(res.filePath)
  },
})

App Android、App iOS、App HarmonyOS 会读取应用可访问的本地路径。Web 端不能直接读取任意本地路径,建议业务侧先读取文件内容,再用 sourceType: 'html'sourceType: 'markdown' 传入。

revokeObjectUrl(url)

Web 端转换结果是 Blob URL。上传、下载或预览完成后,可以调用 revokeObjectUrl(url) 释放资源。

revokeObjectUrl(filePath)

setDocifyLogEnabled(enabled)

排查问题时可打开插件日志。日志只输出调用状态、进度、成功、失败、取消等信息,不输出完整 HTML / Markdown 正文。

import { setDocifyLogEnabled } from '@/uni_modules/hans-docify'

setDocifyLogEnabled(true)

可通过 isDocifyLogEnabledSync() 读取当前日志开关状态。

options 参数

参数 类型 说明
source string HTML / Markdown 内容,或本地文件路径
sourceType 'html' \| 'markdown' \| 'file' convertDocument 需要
inputFormat 'html' \| 'markdown' sourceType: 'file' 时建议传入
format 'pdf' \| 'docx' convertDocument 需要
docxMode 'structured' \| 'snapshot' DOCX 模式,默认 structured
fileName string 输出文件名
outputPath string 输出路径,不传则使用默认缓存路径
style DocifyStyleOptions 页面标题、纸张、边距、字体和自定义 CSS
allowJavaScript boolean 是否允许执行页面 JavaScript
allowRemoteResources boolean 是否允许加载远程图片等资源
renderDelay number 渲染等待时间,单位毫秒
readySelector string 等待指定选择器出现后再导出
readyExpression string 等待 JS 表达式为 true 后再导出
chartSnapshot DocifyChartSnapshotOptions 图表静态化配置
timeout number 转换超时时间,单位毫秒
progress (res) => void 进度回调
success (res) => void 成功回调
fail (err) => void 失败回调
complete (res) => void 完成回调

style 参数

style: {
  title: '报告标题',
  baseUrl: 'https://example.com/',
  pageSize: 'A4',
  orientation: 'portrait',
  margins: { top: 24, right: 24, bottom: 24, left: 24 },
  defaultFontFamily: 'Microsoft YaHei',
  defaultFontSize: 12,
  css: 'body { line-height: 1.6; }'
}

图表和动态内容

如果 HTML 中有图表、异步渲染内容或远程资源,建议显式开启相关选项。

convertHtmlToDocx({
  source: html,
  fileName: 'chart-report.docx',
  allowJavaScript: true,
  allowRemoteResources: true,
  readyExpression: 'window.__CHART_READY === true',
  renderDelay: 300,
  chartSnapshot: {
    selector: '#chart',
    pixelRatio: 2,
    backgroundColor: '#ffffff',
  },
  success: (res) => {
    console.log(res.filePath)
  },
})

多个图表可以使用 selectors

chartSnapshot: {
  selectors: ['#line-chart', '#bar-chart', '#pie-chart'],
  pixelRatio: 2,
  backgroundColor: '#ffffff',
}

返回结果

type DocifyConvertResult = {
  success: boolean
  format: 'pdf' | 'docx'
  filePath: string
  fileName: string
  mimeType: string
  size: number
  pages?: number
}

错误码

错误码 说明
9090001 source 为空
9090002 sourceType 或 inputFormat 不支持
9090003 format 不支持
9090004 输入文件不存在或不可读
9090005 Markdown 解析失败
9090006 HTML 解析失败
9090007 远程资源加载失败
9090008 PDF 渲染失败
9090009 DOCX 渲染失败
9090010 输出路径不可写
9090011 转换超时
9090012 当前平台能力不可用
9090013 转换被取消
9090014 JavaScript 渲染未就绪或执行失败
9090015 图表静态化失败

使用限制

  • docxMode: 'snapshot' 目前支持 Android / iOS,HarmonyOS / Web 会返回 9090012
  • chartSnapshot 目前支持 Android / iOS;使用时必须设置 allowJavaScript: true
  • HarmonyOS PDF 需要 API 14+,建议在页面显示后调用;页面未准备好时可能返回 9090012
  • Web 端返回 Blob URL,不是持久文件路径;上传、下载或预览完成后建议调用 revokeObjectUrl(url)
  • 远程图片、字体、脚本等资源需要设置 allowRemoteResources: true,并确保设备网络和资源跨域策略允许访问。

隐私、权限声明

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

读取本地文件、写入应用缓存目录、WebView 渲染、可选网络访问远程资源

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

-

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

-

暂无用户评论。