@okkkde/pdfjs-dist
v0.0.501
Published
Generic build of Mozilla's PDF.js library.
Readme
PDF.js
NOTE
[!NOTE] This is a fork of mozilla/pdfjs, with 3 changes:
- Export ES modules in both .js and .mjs formats, since nginx's default MIME types do not serve .mjs files as
application/javascript.- Downgrade the target browsers for the legacy build to ES6 (more core-js polyfills).
- Added extra handling for web/DOM APIs used in worker scripts that are not polyfilled by core-js, since bundler like Vite do not automatically polyfill these APIs inside worker scripts.
- Add a
typeofcheck forFinalizationRegistry, as it is not supported in older browsers.
[!TIP] This repository is regularly merged and synchronized with the upstream [[mozilla/pdfjs]]. When a new version is released upstream, a new version with the same version number will be released here.
Quick Start
install
npm install @okkkde/pdfjs-distuse in vite
const pdfjsLib = await import('@okkkde/pdfjs-dist');
// you can use the legacy worker for improved compatibility
const pdfjsWorker = await import('@okkkde/pdfjs-dist/legacy/build/pdf.worker.js?worker&url');
// or modern worker which works on modern browsers, choose at your own
const pdfjsWorker = await import('@okkkde/pdfjs-dist/build/pdf.worker.js?worker&url');
pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsWorker.default;提示
[!NOTE] 本仓库是 [[mozilla/pdfjs]] 的 fork,有三点改动:
- 导出 ES 模块时同时提供
.js与.mjs,解决 nginx 默认不按application/javascript提供.mjs的问题。- legacy 构建的目标浏览器降级到 ES6(更多的 core-js 模块)。
- 为 worker 脚本中未被 core-js 覆盖的 web/DOM API 做了额外处理,因为像 Vite 这类打包器不会自动在 worker 里填充这些 API。
- 为
FinalizationRegistry增加typeof检查,以兼容不支持该特性的旧浏览器。
[!TIP] 本仓库会定期与上游 [[mozilla/pdfjs]] 合并同步,上游有新版本时跟随发布新的版本号一致的版本。
快速开始
安装
npm install @okkkde/pdfjs-dist在 vite 中使用
const pdfjsLib = await import('@okkkde/pdfjs-dist');
// 为了更好的兼容性,可以使用 legacy worker
const pdfjsWorker = await import('@okkkde/pdfjs-dist/legacy/build/pdf.worker.js?worker&url');
// 或者使用现代 worker,适用于现代浏览器,根据需要选择
const pdfjsWorker = await import('@okkkde/pdfjs-dist/build/pdf.worker.js?worker&url');
pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsWorker.default;PDF.js is a Portable Document Format (PDF) library that is built with HTML5. Our goal is to create a general-purpose, web standards-based platform for parsing and rendering PDFs.
This is a pre-built version of the PDF.js source code. It is automatically generated by the build scripts.
For usage with older browsers/environments, without native support for the
latest JavaScript features, please see the legacy/ folder.
Please see this wiki page for information about supported browsers/environments.
See https://github.com/mozilla/pdf.js for learning and contributing.
