npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ionic-native-ohos/pdf-generator

v5.36.0

Published

Cordova PDF Generator Plugin

Readme

@ionic-native/pdf-generator

zh-CN en

本项目基于@ionic-native/[email protected]开发。

简介

一个用于生成PDF文档的插件,支持从HTML内容生成PDF文件,为跨平台应用开发提供统一的PDF生成能力。兼容Android、iOS和OpenHarmony平台,本文档主要说明在OpenHarmony系统中的应用。

在移动应用开发中,PDF文档生成是常见的功能需求,常用于文档导出、报告生成、票据打印等场景,提升应用的实用性和专业性。@ionic-native/pdf-generator插件通过封装原生平台API,为开发者提供了统一的跨平台接口,无需深入原生开发即可实现PDF文档的生成。

支持平台

  • OpenHarmony:5.0+

下载安装

通过命令行或手动引入即可快速安装插件,支持从npm仓库获取。

命令行安装(推荐)

安装hionic CLI:

npm install -g hionic

以下两种方式中任选其一即可,无需重复操作:

npm安装:

# 安装插件
npm install @ionic-native/pdf-generator

# 同步插件
hionic sync

hionic CLI安装:

hionic plugin add @ionic-native/pdf-generator

手动引入安装

1. 添加插件配置

根据 plugin.xmlconfig-json 项,找到 entry 模块中 config.xml 文件,并根据 param 标签添加配置

<feature name="PDFGenerator">
  <param name="harmony-package" value="PDFGenerator" />
</feature>

2. 修改CMake配置

根据 plugin.xmlCMakeLists 项,找到 cordova 模块,路径为 target 字段的文件 CMakeLists.txt,添加 add_library

add_library(cordova SHARED
  // ...
  PDFGenerator/PDFGenerator.cpp
  // ...
)

3. 复制源码文件

根据 plugin.xmlsource-file 项,将 src 字段的路径代码复制到 cordova 模块中 target-dir 字段的目录中:

将源码中src/main/cpp/PDFGenerator目录下的PDFGenerator.h、PDFGenerator.cpp文件引入到cordova模块中src/main/cpp/PDFGenerator目录下。

将源码中src/main/ets/components/PDFGenerator目录下的PDFGenerator.ets文件引入到cordova模块中src/main/ets/components/PDFGenerator目录下。

4. 添加ArkTS配置

cordova 模块的 build-profile.json5 文件中,buildOption/arkOptions/runtimeOnly/sources 配置项数组中加入步骤3中拷贝的ets文件路径:

"buildOption":{
  "arkOptions": {
    "runtimeOnly": {
      "sources": [
        // ...
        "./src/main/ets/components/PDFGenerator/PDFGenerator.ets"
        // ...
      ]
    }
  }
}

卸载

# hionic CLI 卸载
hionic plugin remove @ionic-native/pdf-generator

约束与限制

兼容性

在以下版本中已测试通过:

  1. SDK: 5.0.5(17); IDE: DevEco Studio: 6.0.0; ROM: 5.1.0.150;

权限要求

不涉及

使用示例

示例1:从HTML字符串生成PDF

实现从HTML字符串生成PDF文档的功能,支持自定义配置选项。

import { PDFGenerator } from "@ionic-native/pdf-generator";

const generatePDF = async () => {
  try {
    const html = `
      <html>
        <head>
          <style>
            body { font-family: Arial, sans-serif; }
            h1 { color: #333; }
            .content { padding: 20px; }
          </style>
        </head>
        <body>
          <h1>PDF生成示例</h1>
          <div class="content">
            <p>这是一个从HTML字符串生成的PDF文档。</p>
            <p>生成时间:${new Date().toLocaleString()}</p>
          </div>
        </body>
      </html>
    `;
    const options = {
      documentSize: 'A4',
      landscape: 'portrait',
      type: 'base64',
      fileName: 'sample.pdf',
      baseUrl: ''
    };
    const base64String = await PDFGenerator.fromData(html, options);
    console.log('PDF生成成功:', base64String);
  } catch (error) {
    const errorMsg = error instanceof Error ? error.message : String(error);
    console.error('PDF生成失败:', errorMsg);
  }
};

示例2:从URL生成PDF

实现从URL生成PDF文档的功能,支持自定义配置选项。

import { PDFGenerator } from "@ionic-native/pdf-generator";

const generatePDFFromURL = async () => {
  try {
    const url = 'https://example.com/invoice.html';
    const options = {
      documentSize: 'A4',
      landscape: 'landscape',
      type: 'base64',
      fileName: 'invoice.pdf'
    };
    const base64String = await PDFGenerator.fromURL(url, options);
    console.log('PDF生成成功:', base64String);
  } catch (error) {
    const errorMsg = error instanceof Error ? error.message : String(error);
    console.error('PDF生成失败:', errorMsg);
  }
};

示例3:生成并分享PDF

实现生成并分享PDF文档的功能,支持通过系统分享功能发送PDF。

import { PDFGenerator } from "@ionic-native/pdf-generator";

const generateAndSharePDF = async () => {
  try {
    const html = `
      <html>
        <body>
          <h1>分享PDF示例</h1>
          <p>这个PDF将通过系统分享功能发送。</p>
        </body>
      </html>
    `;
    const options = {
      documentSize: 'A4',
      landscape: 'portrait',
      type: 'share',
      fileName: 'shareable.pdf'
    };
    await PDFGenerator.fromData(html, options);
    console.log('PDF生成并分享成功');
  } catch (error) {
    const errorMsg = error instanceof Error ? error.message : String(error);
    console.error('PDF生成失败:', errorMsg);
  }
};

使用说明

插件在全局对象PDFGenerator下暴露所有功能接口,使用前需确保设备就绪事件(deviceready)已触发。

1. 从HTML字符串生成PDF

从HTML字符串生成PDF文档,支持自定义配置选项。

方法签名

PDFGenerator.fromData(data, options)

2. 从URL生成PDF

从URL生成PDF文档,支持自定义配置选项。

方法签名

PDFGenerator.fromURL(url, options)

3. PDFGeneratorOptions配置选项

| 选项名 | 类型 | 默认值 | 描述 | 备注 | | ---------- | ----- | ------- | ------ | ------ | | documentSize | string | "A4" | 页面大小,如 "A2", "A3", "A4" 等 | OpenHarmony不支持 | | landscape | string | "portrait" | 页面方向,"landscape" 或 "portrait" | - | | type | string | "base64" | 返回类型,"share" 或 "base64" | 如果选择"share",PDF将与系统功能共享 | | fileName | string | "default.pdf" | 生成的PDF文件名 | - | | baseUrl | string | "null" | 用于路径解析的基础URL | - |

目录结构

|---- 目录
|     |---- src/main  # 插件的实现代码
|           |---- cpp  # C++ 代码
|           |---- ets  # ArkTS 代码
|     |---- www    # Web 侧代码
|     |---- README.md          # 说明文档
|     |---- package.json       # 配置文件
|     |---- plugin.xml         # 插件配置文件

贡献代码

使用过程中发现任何问题都可以提Issue,当然,也非常欢迎发PR共建。

许可证

本插件基于Apache License 2.0开源,详见LICENSE文件。