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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@cqsjjb/pdf-utils

v0.0.1-alpha.1

Published

PDF Utils - 支持按需加载的HTML和图片转PDF工具

Readme

@cqsjjb/jjb-pdf-utils

支持按需加载的HTML和图片转PDF工具库。

安装

npm install @cqsjjb/jjb-pdf-utils

使用方法

1. 完整导入(包含所有功能)

import pdfUtils from '@cqsjjb/jjb-pdf-utils';

// 使用 htmlToPDF
await pdfUtils.htmlToPDF('<div>Hello World</div>', {
  fileName: 'output.pdf'
});

// 使用 imageToPDF
await pdfUtils.imageToPDF('image.jpg', {
  fileName: 'image.pdf'
});

2. 按需导入(推荐)

只导入 HTML 转 PDF 功能

import htmlToPDF from '@cqsjjb/jjb-pdf-utils/htmlToPDF';

await htmlToPDF('<div>Hello World</div>', {
  fileName: 'output.pdf',
  singlePage: true
});

只导入图片转 PDF 功能

import imageToPDF from '@cqsjjb/jjb-pdf-utils/imageToPDF';

await imageToPDF('image.jpg', {
  fileName: 'image.pdf',
  fitToPage: true
});

按需导入多个功能

import { htmlToPDF, imageToPDF } from '@cqsjjb/jjb-pdf-utils';

// 使用 htmlToPDF
await htmlToPDF('<div>Hello World</div>');

// 使用 imageToPDF
await imageToPDF('image.jpg');

API 文档

htmlToPDF

将 HTML 内容转换为 PDF。

htmlToPDF(html, options)

参数:

  • html (string | HTMLElement): HTML 字符串或 DOM 元素
  • options (HtmlToPDFOptions): 配置选项

HtmlToPDFOptions:

  • fileName (string): 导出的PDF文件名,默认: "output.pdf"
  • leftMargin (number): 左边距(mm),默认: 10
  • topMargin (number): 上边距(mm),默认: 10
  • pdf (object): PDF相关配置
    • format ('p' | 'l'): PDF格式,默认: 'p'
    • unit ('mm' | 'pt' | 'in' | 'cm'): PDF单位,默认: 'mm'
    • size (string): PDF尺寸,默认: 'a4'
  • canvas (object): html2canvas配置
    • scale (number): 清晰度缩放比例,默认: 2
    • useCORS (boolean): 是否使用CORS,默认: true
    • options (object): html2canvas的额外选项
  • singlePage (boolean): 是否单页模式,默认: false

imageToPDF

将图片转换为 PDF。

imageToPDF(image, options)

参数:

  • image (string | HTMLImageElement | File): 图片URL、HTMLImageElement或File对象
  • options (ImageToPDFOptions): 配置选项

ImageToPDFOptions:

  • fileName (string): 导出的PDF文件名,默认: "output.pdf"
  • leftMargin (number): 左边距(mm),默认: 10
  • topMargin (number): 上边距(mm),默认: 10
  • pdf (object): PDF相关配置
    • format ('p' | 'l'): PDF格式,默认: 'p'
    • unit ('mm' | 'pt' | 'in' | 'cm'): PDF单位,默认: 'mm'
    • size (string): PDF尺寸,默认: 'a4'
  • fitToPage (boolean): 是否适应页面大小,默认: true
  • scale (number): 图片缩放比例,默认: 1

按需加载的优势

  1. 减少包体积:只导入需要的功能,减少最终打包体积
  2. 提高加载速度:按需加载可以显著提高应用启动速度
  3. 更好的 Tree Shaking:现代打包工具可以更好地进行代码优化

示例

HTML 转 PDF 示例

import htmlToPDF from '@cqsjjb/jjb-pdf-utils/htmlToPDF';

// 基本使用
await htmlToPDF('<h1>Hello World</h1>');

// 高级配置
await htmlToPDF(document.getElementById('content'), {
  fileName: 'report.pdf',
  singlePage: true,
  leftMargin: 20,
  topMargin: 20,
  pdf: {
    format: 'l', // 横向
    size: 'a3'
  },
  canvas: {
    scale: 3, // 更高清晰度
    useCORS: true
  }
});

图片转 PDF 示例

import imageToPDF from '@cqsjjb/jjb-pdf-utils/imageToPDF';

// 基本使用
await imageToPDF('image.jpg');

// 高级配置
await imageToPDF(fileInput.files[0], {
  fileName: 'photo.pdf',
  fitToPage: true,
  scale: 0.8,
  pdf: {
    format: 'p', // 纵向
    size: 'a4'
  }
});

许可证

MIT