first commit
Some checks failed
Types tests / Test (lts/*) (push) Has been cancelled
Lint / Lint (lts/*) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
CI / Test (20) (push) Has been cancelled
CI / Test (22) (push) Has been cancelled
CI / Test (24) (push) Has been cancelled
20
.editorconfig
Normal file
@@ -0,0 +1,20 @@
|
||||
root = true
|
||||
|
||||
[*.{js,jsm,mjs,json,html,css,pdf.link}]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
indent_size = 2
|
||||
indent_style = space
|
||||
insert_final_newline = true
|
||||
max_line_length = 80
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[*.{json,pdf.link}]
|
||||
max_line_length = off
|
||||
|
||||
[*.md]
|
||||
max_line_length = off
|
||||
trim_trailing_whitespace = false
|
||||
|
||||
[COMMIT_EDITMSG]
|
||||
max_line_length = off
|
||||
6
.git-blame-ignore-revs
Normal file
@@ -0,0 +1,6 @@
|
||||
# Auto-format `.js` files with ESLint/Prettier
|
||||
de36b2aabab2b7fd647d9591f959c4540129541d
|
||||
# Auto-format `.css` files with Stylelint/Prettier
|
||||
8aa2718d225ad701a5b8a2788b42d221f1e4327d
|
||||
# Auto-format `.json` files with Prettier
|
||||
29de9bdce6c9785574994fda0e51533d796a9bb4
|
||||
23
.gitattributes
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
# Force Unix line endings for most file formats (except binary files)
|
||||
*.js text eol=lf
|
||||
*.jsm text eol=lf
|
||||
*.css text eol=lf
|
||||
*.html text eol=lf
|
||||
*.md text eol=lf
|
||||
*.ftl text eol=lf
|
||||
*.yml text eol=lf
|
||||
*.json text eol=lf
|
||||
*.config text eol=lf
|
||||
*.inc text eol=lf
|
||||
*.manifest text eol=lf
|
||||
*.rdf text eol=lf
|
||||
*.jade text eol=lf
|
||||
*.coffee text eol=lf
|
||||
|
||||
# PDF files shall not modify CRLF line endings
|
||||
*.pdf -crlf
|
||||
|
||||
# Linguist language overrides
|
||||
*.js linguist-language=JavaScript
|
||||
*.jsm linguist-language=JavaScript
|
||||
*.inc linguist-language=XML
|
||||
1
.github/CODEOWNERS
vendored
Normal file
@@ -0,0 +1 @@
|
||||
l10n/en-US/*.ftl @mozilla/pdfjs-l10n
|
||||
16
.github/CONTRIBUTING.md
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
# PDF.js issue reporting
|
||||
|
||||
The issues are used to track both bugs filed by users and specific work items for developers. Try to file one issue per problem observed. Please specify a valid title (e.g. "Glyph spacing is incorrect" instead of "PDF.js does not work") and provide more details about the issue: link to the PDF, location in the PDF, screenshot, browser version, operating system, PDF.js version and JavaScript console warning/error messages. Issues that do not have enough details provided will be closed as invalid/incomplete.
|
||||
|
||||
If the issue is related to errors produced by a specific PDF, please always include the PDF by providing a URL where contributors can download it. Without a PDF for reproduction, such issues will be closed. We understand that many PDFs contain sensitive information, however having a PDF is essential to resolving the issue and building our regression testing suite. If possible, try creating a reduced example exhibiting the problem but not containing sensitive data. Also small PDFs are best suited for our regression testing. If an important issue only shows on sensitive PDFs, contributors might be willing to accept these PDFs via a secure exchange.
|
||||
|
||||
The issue tracking system is designed to record a single technical problem. A bug report is something where a developer/contributor can work on. The GitHub issue tracker is not a good place for general, not well thought out or unworkable ideas. Most likely a discussion-type issue will not be addressed for a long time or closed as invalid. The best place for general discussions is our Matrix room at https://chat.mozilla.org/#/room/#pdfjs:mozilla.org.
|
||||
|
||||
If you are developing a custom solution, first check the examples at https://github.com/mozilla/pdf.js#learning and search existing issues. If this does not help, please prepare a short well-documented example that demonstrates the problem and make it accessible online on your website, JS Bin, GitHub, etc. before opening a new issue or contacting us in the Matrix room -- keep in mind that just code snippets won't help us troubleshoot the problem.
|
||||
|
||||
Note that the translations for PDF.js in the `l10n` folder are imported from the Nightly channel of Mozilla Firefox, such that we don't have to maintain them ourselves. This means that we will not accept pull requests that add new languages and/or modify existing translations, unless the corresponding changes have been made in Mozilla Firefox first.
|
||||
|
||||
See also:
|
||||
- https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions
|
||||
- https://github.com/mozilla/pdf.js/wiki/Contributing
|
||||
- https://github.com/mozilla/pdf.js/blob/master/README.md
|
||||
81
.github/ISSUE_TEMPLATE/bug.yml
vendored
Normal file
@@ -0,0 +1,81 @@
|
||||
name: Bug Report
|
||||
description: Report a bug in PDF.js
|
||||
title: "[Bug]: "
|
||||
body:
|
||||
- type: textarea
|
||||
attributes:
|
||||
label: Attach (recommended) or Link to PDF file
|
||||
description: Without this information the issue may be closed without comment
|
||||
placeholder: Please place only the PDF file in this field
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: markdown
|
||||
attributes: { value: "---" }
|
||||
|
||||
- type: input
|
||||
attributes:
|
||||
label: Web browser and its version
|
||||
description: Please ensure that it's supported, refer to [the FAQ](https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-support)
|
||||
validations:
|
||||
required: true
|
||||
- type: input
|
||||
attributes:
|
||||
label: Operating system and its version
|
||||
validations:
|
||||
required: true
|
||||
- type: input
|
||||
attributes:
|
||||
label: PDF.js version
|
||||
description: Please find official releases [here](https://github.com/mozilla/pdf.js/releases)
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
attributes:
|
||||
label: Is the bug present in the latest PDF.js version?
|
||||
description: Please check the [online demo](https://github.com/mozilla/pdf.js#online-demo)
|
||||
options: ["Yes", "No"]
|
||||
default: 0
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
attributes:
|
||||
label: Is a browser extension
|
||||
options: ["Yes", "No"]
|
||||
default: 1
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: markdown
|
||||
attributes: { value: "---" }
|
||||
|
||||
- type: textarea
|
||||
attributes:
|
||||
label: Steps to reproduce the problem
|
||||
placeholder: "1.\n2."
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
attributes:
|
||||
label: What is the expected behavior?
|
||||
description: Also add a screenshot
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
attributes:
|
||||
label: What went wrong?
|
||||
description: Also add a screenshot
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: input
|
||||
attributes:
|
||||
label: Link to a viewer
|
||||
description: Needed if hosted on a site other than mozilla.github.io/pdf.js or as Firefox/Chrome extension
|
||||
|
||||
- type: textarea
|
||||
attributes:
|
||||
label: Additional context
|
||||
description: Do you have anything to add that doesn't fit in the issue template?
|
||||
5
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
blank_issues_enabled: false
|
||||
contact_links:
|
||||
- name: Need help?
|
||||
url: https://github.com/mozilla/pdf.js/discussions
|
||||
about: If you need help on how to use PDF.js, please open a discussion where other community members can reply
|
||||
23
.github/ISSUE_TEMPLATE/feature.yml
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
name: Feature request
|
||||
description: Propose a new feature or enhancement for PDF.js
|
||||
title: "[Feature]: "
|
||||
body:
|
||||
- type: dropdown
|
||||
attributes:
|
||||
label: Is the feature relevant to the Firefox PDF Viewer?
|
||||
options: ["Yes", "No"]
|
||||
default: 1
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
attributes:
|
||||
label: Feature description
|
||||
description: What new feature would you like PDF.js to have? Why would it be useful? What are the current workarounds?
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
attributes:
|
||||
label: Other PDF viewers
|
||||
description: Do other PDF viewers implement similar functionality? Add descriptions, links, and/or screenshots.
|
||||
13
.github/SECURITY.md
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
# Security policy
|
||||
|
||||
Mozilla takes the security of our software seriously. If you believe you have found a security vulnerability in PDF.js, please report it to us as described below.
|
||||
|
||||
## Reporting security vulnerabilities
|
||||
|
||||
**Please don't report security vulnerabilities through public GitHub issues.**
|
||||
|
||||
Instead, please report security vulnerabilities in [Bugzilla](https://bugzilla.mozilla.org/enter_bug.cgi?product=Firefox&component=PDF%20Viewer&groups=firefox-core-security) and make sure that the checkbox in the "Security" section is checked so the required access controls are automatically configured:
|
||||
|
||||

|
||||
|
||||
The Mozilla security team will process the bug as described in [Mozilla's security bugs policy](https://www.mozilla.org/en-US/about/governance/policies/security-group/bugs).
|
||||
8
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "github-actions"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
labels:
|
||||
- "dependencies"
|
||||
29
.github/fluent_linter_config.yml
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
# This Source Code Form is subject to the terms of the Mozilla Public
|
||||
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
# See https://github.com/mozilla-l10n/moz-fluent-linter/blob/main/src/fluent_linter/config.yml
|
||||
# for details
|
||||
|
||||
---
|
||||
ID01:
|
||||
enabled: true
|
||||
exclusions:
|
||||
messages: []
|
||||
files: []
|
||||
ID02:
|
||||
enabled: true
|
||||
min_length: 9
|
||||
exclusions:
|
||||
messages: []
|
||||
files: []
|
||||
CO01:
|
||||
enabled: true
|
||||
brands:
|
||||
- Firefox
|
||||
- Mozilla
|
||||
exclusions:
|
||||
files: []
|
||||
messages: []
|
||||
VC:
|
||||
disabled: false
|
||||
1
.github/requirements.txt
vendored
Normal file
@@ -0,0 +1 @@
|
||||
moz-fluent-linter==0.4.*
|
||||
BIN
.github/security.png
vendored
Normal file
|
After Width: | Height: | Size: 11 KiB |
34
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
name: CI
|
||||
on: [push, pull_request]
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: Test
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
node-version: [20, 22, 24]
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v5
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm ci
|
||||
|
||||
- name: Run external tests
|
||||
run: npx gulp externaltest
|
||||
|
||||
- name: Run CLI unit tests
|
||||
run: npx gulp unittestcli
|
||||
35
.github/workflows/codeql.yml
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
name: CodeQL
|
||||
on: [push, pull_request]
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
analyze:
|
||||
name: Analyze
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
permissions:
|
||||
security-events: write
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
language: [javascript]
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v3
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
queries: security-and-quality
|
||||
|
||||
- name: Autobuild CodeQL
|
||||
uses: github/codeql-action/autobuild@v3
|
||||
|
||||
- name: Perform CodeQL analysis
|
||||
uses: github/codeql-action/analyze@v3
|
||||
42
.github/workflows/fluent_linter.yml
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
name: Lint Fluent Reference Files
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- 'l10n/en-US/**.ftl'
|
||||
- '.github/fluent_linter_config.yml'
|
||||
- '.github/workflows/fluent_linter.yml'
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
paths:
|
||||
- 'l10n/en-US/**.ftl'
|
||||
- '.github/fluent_linter_config.yml'
|
||||
- '.github/workflows/fluent_linter.yml'
|
||||
branches:
|
||||
- master
|
||||
workflow_dispatch:
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
name: Lint
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v5
|
||||
|
||||
- name: Use Python 3.13
|
||||
uses: actions/setup-python@v6
|
||||
with:
|
||||
python-version: '3.13'
|
||||
cache: 'pip'
|
||||
|
||||
- name: Install Fluent dependencies
|
||||
run: |
|
||||
pip install -r .github/requirements.txt
|
||||
|
||||
- name: Lint Fluent reference files
|
||||
run: |
|
||||
moz-fluent-lint ./l10n/en-US --config .github/fluent_linter_config.yml
|
||||
61
.github/workflows/font_tests.yml
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
name: Font tests
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- 'gulpfile.mjs'
|
||||
- 'src/**'
|
||||
- 'test/test.mjs'
|
||||
- 'test/font/**'
|
||||
- '.github/workflows/font_tests.yml'
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
paths:
|
||||
- 'gulpfile.mjs'
|
||||
- 'src/**'
|
||||
- 'test/test.mjs'
|
||||
- 'test/font/**'
|
||||
- '.github/workflows/font_tests.yml'
|
||||
branches:
|
||||
- master
|
||||
workflow_dispatch:
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: Test
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
node-version: [lts/*]
|
||||
os: [windows-latest, ubuntu-latest]
|
||||
|
||||
runs-on: ${{ matrix.os }}
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v5
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm ci
|
||||
|
||||
- name: Use Python 3.13
|
||||
uses: actions/setup-python@v6
|
||||
with:
|
||||
python-version: '3.13'
|
||||
cache: 'pip'
|
||||
|
||||
- name: Install Fonttools
|
||||
run: pip install fonttools
|
||||
|
||||
- name: Run font tests
|
||||
run: npx gulp fonttest --headless
|
||||
34
.github/workflows/lint.yml
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
name: Lint
|
||||
on: [push, pull_request]
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
name: Lint
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
node-version: [lts/*]
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v5
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm ci
|
||||
|
||||
- name: Run lint
|
||||
run: npx gulp lint
|
||||
|
||||
- name: Run lint-chromium
|
||||
run: npx gulp lint-chromium
|
||||
39
.github/workflows/publish_release.yml
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
name: Publish release
|
||||
on:
|
||||
release:
|
||||
types: [published]
|
||||
permissions:
|
||||
contents: read
|
||||
id-token: write
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
name: Publish
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [lts/*]
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v5
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
registry-url: 'https://registry.npmjs.org'
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm ci
|
||||
|
||||
- name: Build the `pdfjs-dist` library
|
||||
run: npx gulp dist
|
||||
|
||||
- name: Publish the `pdfjs-dist` library to NPM
|
||||
run: npm publish ./build/dist --provenance
|
||||
env:
|
||||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
||||
69
.github/workflows/publish_website.yml
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
name: Publish website
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Build
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [lts/*]
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v5
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm ci
|
||||
|
||||
- name: Build the website
|
||||
run: npx gulp web
|
||||
|
||||
- name: Archive the website
|
||||
shell: sh
|
||||
run: |
|
||||
chmod -c -R +rX "$INPUT_PATH" | while read line; do
|
||||
echo "::warning title=Invalid file permissions automatically fixed::$line"
|
||||
done
|
||||
tar \
|
||||
--dereference --hard-dereference \
|
||||
--directory "$INPUT_PATH" \
|
||||
-cvf "$RUNNER_TEMP/website.tar" \
|
||||
--exclude=.git \
|
||||
--exclude=.github \
|
||||
.
|
||||
env:
|
||||
INPUT_PATH: build/gh-pages
|
||||
|
||||
- name: Upload the website
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: github-pages
|
||||
path: ${{ runner.temp }}/website.tar
|
||||
retention-days: 1
|
||||
if-no-files-found: error
|
||||
|
||||
deploy:
|
||||
name: Deploy
|
||||
runs-on: ubuntu-latest
|
||||
needs: build
|
||||
permissions:
|
||||
pages: write # Required to deploy to GitHub Pages.
|
||||
id-token: write # Required to verify that the deployment originates from this workflow.
|
||||
|
||||
steps:
|
||||
- name: Deploy the website
|
||||
uses: actions/deploy-pages@v4
|
||||
31
.github/workflows/types_tests.yml
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
name: Types tests
|
||||
on: [push, pull_request]
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: Test
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
node-version: [lts/*]
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v5
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm ci
|
||||
|
||||
- name: Run types tests
|
||||
run: npx gulp typestest
|
||||
9
.gitignore
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
*~
|
||||
local.mk
|
||||
build/
|
||||
tags
|
||||
.DS_Store
|
||||
Makefile
|
||||
node_modules/
|
||||
examples/node/svgdump/
|
||||
examples/node/pdf2png/*.png
|
||||
15
.prettierignore
Normal file
@@ -0,0 +1,15 @@
|
||||
build/
|
||||
l10n/
|
||||
docs/
|
||||
node_modules/
|
||||
external/bcmaps/
|
||||
external/builder/fixtures/
|
||||
external/builder/fixtures_babel/
|
||||
external/openjpeg/
|
||||
external/qcms/
|
||||
external/quickjs/
|
||||
test/stats/results/
|
||||
test/tmp/
|
||||
test/pdfs/
|
||||
web/locale/
|
||||
*~/
|
||||
18
.prettierrc
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"arrowParens": "avoid",
|
||||
"endOfLine": "lf",
|
||||
"printWidth": 80,
|
||||
"semi": true,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5",
|
||||
"useTabs": false,
|
||||
|
||||
"overrides": [
|
||||
{
|
||||
files: ["tsconfig.json"],
|
||||
options: {
|
||||
parser: "json",
|
||||
},
|
||||
},
|
||||
]
|
||||
}
|
||||
9
.puppeteerrc
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"chrome": {
|
||||
"skipDownload": false
|
||||
},
|
||||
"firefox": {
|
||||
"skipDownload": false,
|
||||
"version": "nightly"
|
||||
}
|
||||
}
|
||||
13
.stylelintignore
Normal file
@@ -0,0 +1,13 @@
|
||||
build/
|
||||
l10n/
|
||||
docs/
|
||||
node_modules/
|
||||
external/bcmaps/
|
||||
external/builder/fixtures/
|
||||
external/builder/fixtures_babel/
|
||||
external/quickjs/
|
||||
test/stats/results/
|
||||
test/tmp/
|
||||
test/pdfs/
|
||||
web/locale/
|
||||
*~/
|
||||
25
.stylelintrc
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"plugins": [
|
||||
"stylelint-prettier"
|
||||
],
|
||||
|
||||
"rules": {
|
||||
"prettier/prettier": true,
|
||||
|
||||
"alpha-value-notation": "number",
|
||||
"block-no-empty": true,
|
||||
"color-function-notation": "modern",
|
||||
"color-hex-length": "short",
|
||||
"color-no-invalid-hex": true,
|
||||
"declaration-block-no-duplicate-properties": true,
|
||||
"declaration-block-no-redundant-longhand-properties": true,
|
||||
"declaration-property-value-disallowed-list": {
|
||||
"float": ["inline-start", "inline-end"]
|
||||
},
|
||||
"length-zero-no-unit": [true, {
|
||||
ignore: ["custom-properties"]
|
||||
}],
|
||||
"selector-pseudo-element-colon-notation": "double",
|
||||
"shorthand-property-no-redundant-values": true,
|
||||
},
|
||||
}
|
||||
35
.svglintrc.js
Normal file
@@ -0,0 +1,35 @@
|
||||
export default {
|
||||
rules: {
|
||||
valid: true,
|
||||
|
||||
custom: [
|
||||
(reporter, $, ast, { filename }) => {
|
||||
reporter.name = "no-svg-fill-context-fill";
|
||||
|
||||
const svg = $.find("svg");
|
||||
const fill = svg.attr("fill");
|
||||
if (fill === "context-fill") {
|
||||
reporter.error(
|
||||
"Fill attribute on svg element must not be set to 'context-fill'",
|
||||
svg[0],
|
||||
ast
|
||||
);
|
||||
}
|
||||
},
|
||||
],
|
||||
},
|
||||
ignore: [
|
||||
"build/**",
|
||||
"l10n/**",
|
||||
"docs/**",
|
||||
"node_modules/**",
|
||||
"external/bcmaps/**",
|
||||
"external/builder/fixtures/**",
|
||||
"external/builder/fixtures_babel/**",
|
||||
"external/quickjs/**",
|
||||
"test/tmp/**",
|
||||
"test/pdfs/**",
|
||||
"web/locale/**",
|
||||
"*~/**",
|
||||
],
|
||||
};
|
||||
33
AUTHORS
Normal file
@@ -0,0 +1,33 @@
|
||||
This is an (incomplete) list of people who have contributed to the
|
||||
codebase which lives in this repository. If you make a contribution
|
||||
here, you may add your name and, optionally, email address in the
|
||||
appropriate place.
|
||||
|
||||
Adil Allawi <@ironymark>
|
||||
Andreas Gal <gal@mozilla.com>
|
||||
Artur Adib <aadib@mozilla.com>
|
||||
Brendan Dahl <bdahl@mozilla.com>
|
||||
Bill Walker <bwalker@mozilla.com>
|
||||
Calixte Denizet <calixte@mozilla.com>
|
||||
Chris G Jones <cjones@mozilla.com>
|
||||
David Quintana <gigaherz@gmail.com>
|
||||
Emily Wachowiak <ewachowiak@mozilla.com>
|
||||
Felix Kälberer <@fkaelberer>
|
||||
Jakob Miland <saebekassebil@gmail.com>
|
||||
Jonas Jenwald <jonas.jenwald@gmail.com>
|
||||
Julian Viereck
|
||||
Justin D'Arcangelo <justindarc@gmail.com>
|
||||
Kalervo Kujala
|
||||
Marco Castelluccio <mcastelluccio@mozilla.com>
|
||||
Marie-Lilas Onanga Ozavino <monangaozavino@mozilla.com>
|
||||
Michał Gołębiowski-Owczarek <m.goleb@gmail.com>
|
||||
Ophir Lojkine <@lovasoa>
|
||||
Ryan Casey <rcasey@mozilla.com>
|
||||
Rob Wu <rob@robwu.nl>
|
||||
Shaon Barman <shaon.barman@gmail.com>
|
||||
Sehyun Park <premed055515@gmail.com>
|
||||
Tim van der Meij <info@timvandermeij.nl>
|
||||
Vivin Paliath <vivin.paliath@gmail.com>
|
||||
Vivien Nicolas <21@vingtetun.org>
|
||||
Yury Delendik <ydelendik@mozilla.com>
|
||||
waddlesplash <@waddlesplash>
|
||||
15
CODE_OF_CONDUCT.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# Community Participation Guidelines
|
||||
|
||||
This repository is governed by Mozilla's code of conduct and etiquette guidelines.
|
||||
For more details, please read the
|
||||
[Mozilla Community Participation Guidelines](https://www.mozilla.org/about/governance/policies/participation/).
|
||||
|
||||
## How to Report
|
||||
For more information on how to report violations of the Community Participation Guidelines, please read our '[How to Report](https://www.mozilla.org/about/governance/policies/participation/reporting/)' page.
|
||||
|
||||
<!--
|
||||
## Project Specific Etiquette
|
||||
|
||||
In some cases, there will be additional project etiquette i.e.: (https://bugzilla.mozilla.org/page.cgi?id=etiquette.html).
|
||||
Please update for your project.
|
||||
-->
|
||||
5
EXPORT
Normal file
@@ -0,0 +1,5 @@
|
||||
PDF.js is publicly available software not subject to the Export Administration
|
||||
Regulations (EAR) per EAR 734.3(b) and 734.7. Because PDF.js is not subject
|
||||
to the EAR it does not have an Export Control Classification Number (ECCN).
|
||||
Mozilla has completed the notification for PDF.js publicly available encryption
|
||||
source code per EAR 742.15(b).
|
||||
177
LICENSE
Normal file
@@ -0,0 +1,177 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
137
README.md
Normal file
@@ -0,0 +1,137 @@
|
||||
# PDF.js [](https://github.com/mozilla/pdf.js/actions/workflows/ci.yml?query=branch%3Amaster)
|
||||
|
||||
[PDF.js](https://mozilla.github.io/pdf.js/) is a Portable Document Format (PDF) viewer that is built with HTML5.
|
||||
|
||||
PDF.js is community-driven and supported by Mozilla. Our goal is to
|
||||
create a general-purpose, web standards-based platform for parsing and
|
||||
rendering PDFs.
|
||||
|
||||
## Contributing
|
||||
|
||||
PDF.js is an open source project and always looking for more contributors. To
|
||||
get involved, visit:
|
||||
|
||||
+ [Issue Reporting Guide](https://github.com/mozilla/pdf.js/blob/master/.github/CONTRIBUTING.md)
|
||||
+ [Code Contribution Guide](https://github.com/mozilla/pdf.js/wiki/Contributing)
|
||||
+ [Frequently Asked Questions](https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions)
|
||||
+ [Good Beginner Bugs](https://github.com/mozilla/pdf.js/issues?q=is%3Aissue%20state%3Aopen%20label%3Agood-beginner-bug)
|
||||
+ [Projects](https://github.com/mozilla/pdf.js/projects)
|
||||
|
||||
Feel free to stop by our [Matrix room](https://chat.mozilla.org/#/room/#pdfjs:mozilla.org) for questions or guidance.
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Online demo
|
||||
|
||||
Please note that the "Modern browsers" version assumes native support for the
|
||||
latest JavaScript features; please also see [this wiki page](https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-support).
|
||||
|
||||
+ Modern browsers: https://mozilla.github.io/pdf.js/web/viewer.html
|
||||
|
||||
+ Older browsers: https://mozilla.github.io/pdf.js/legacy/web/viewer.html
|
||||
|
||||
### Browser Extensions
|
||||
|
||||
#### Firefox
|
||||
|
||||
PDF.js is built into version 19+ of Firefox.
|
||||
|
||||
#### Chrome
|
||||
|
||||
+ The official extension for Chrome can be installed from the [Chrome Web Store](https://chrome.google.com/webstore/detail/pdf-viewer/oemmndcbldboiebfnladdacbdfmadadm).
|
||||
*This extension is maintained by [@Rob--W](https://github.com/Rob--W).*
|
||||
+ Build Your Own - Get the code as explained below and issue `npx gulp chromium`. Then open
|
||||
Chrome, go to `Tools > Extension` and load the (unpackaged) extension from the
|
||||
directory `build/chromium`.
|
||||
|
||||
## Getting the Code
|
||||
|
||||
To get a local copy of the current code, clone it using git:
|
||||
|
||||
$ git clone https://github.com/mozilla/pdf.js.git
|
||||
$ cd pdf.js
|
||||
|
||||
Next, install Node.js via the [official package](https://nodejs.org) or via
|
||||
[nvm](https://github.com/creationix/nvm). If everything worked out, install
|
||||
all dependencies for PDF.js:
|
||||
|
||||
$ npm install
|
||||
|
||||
Finally, you need to start a local web server as some browsers do not allow opening
|
||||
PDF files using a `file://` URL. Run:
|
||||
|
||||
$ npx gulp server
|
||||
|
||||
and then you can open:
|
||||
|
||||
+ http://localhost:8888/web/viewer.html
|
||||
|
||||
Please keep in mind that this assumes the latest version of Mozilla Firefox; refer to [Building PDF.js](https://github.com/mozilla/pdf.js/blob/master/README.md#building-pdfjs) for non-development usage of the PDF.js library.
|
||||
|
||||
It is also possible to view all test PDF files on the right side by opening:
|
||||
|
||||
+ http://localhost:8888/test/pdfs/?frame
|
||||
|
||||
## Building PDF.js
|
||||
|
||||
In order to bundle all `src/` files into two production scripts and build the generic
|
||||
viewer, run:
|
||||
|
||||
$ npx gulp generic
|
||||
|
||||
If you need to support older browsers, run:
|
||||
|
||||
$ npx gulp generic-legacy
|
||||
|
||||
This will generate `pdf.js` and `pdf.worker.js` in the `build/generic/build/` directory (respectively `build/generic-legacy/build/`).
|
||||
Both scripts are needed but only `pdf.js` needs to be included since `pdf.worker.js` will
|
||||
be loaded by `pdf.js`. The PDF.js files are large and should be minified for production.
|
||||
|
||||
## Using PDF.js in a web application
|
||||
|
||||
To use PDF.js in a web application you can choose to use a pre-built version of the library
|
||||
or to build it from source. We supply pre-built versions for usage with NPM under
|
||||
the `pdfjs-dist` name. For more information and examples please refer to the
|
||||
[wiki page](https://github.com/mozilla/pdf.js/wiki/Setup-pdf.js-in-a-website) on this subject.
|
||||
|
||||
## Including via a CDN
|
||||
|
||||
PDF.js is hosted on several free CDNs:
|
||||
- https://www.jsdelivr.com/package/npm/pdfjs-dist
|
||||
- https://cdnjs.com/libraries/pdf.js
|
||||
- https://unpkg.com/pdfjs-dist/
|
||||
|
||||
## Learning
|
||||
|
||||
You can play with the PDF.js API directly from your browser using the live demos below:
|
||||
|
||||
+ [Interactive examples](https://mozilla.github.io/pdf.js/examples/index.html#interactive-examples)
|
||||
|
||||
More examples can be found in the [examples folder](https://github.com/mozilla/pdf.js/tree/master/examples/). Some of them are using the pdfjs-dist package, which can be built and installed in this repo directory via `npx gulp dist-install` command.
|
||||
|
||||
For an introduction to the PDF.js code, check out the presentation by our
|
||||
contributor Julian Viereck:
|
||||
|
||||
+ https://www.youtube.com/watch?v=Iv15UY-4Fg8
|
||||
|
||||
More learning resources can be found at:
|
||||
|
||||
+ https://github.com/mozilla/pdf.js/wiki/Additional-Learning-Resources
|
||||
|
||||
The API documentation can be found at:
|
||||
|
||||
+ https://mozilla.github.io/pdf.js/api/
|
||||
|
||||
## Questions
|
||||
|
||||
Check out our FAQs and get answers to common questions:
|
||||
|
||||
+ https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions
|
||||
|
||||
Talk to us on Matrix:
|
||||
|
||||
+ https://chat.mozilla.org/#/room/#pdfjs:mozilla.org
|
||||
|
||||
File an issue:
|
||||
|
||||
+ https://github.com/mozilla/pdf.js/issues/new/choose
|
||||
11
docs/contents/api/index.md
Normal file
@@ -0,0 +1,11 @@
|
||||
---
|
||||
title: API
|
||||
layout: layout.njk
|
||||
slug: api
|
||||
---
|
||||
|
||||
# API
|
||||
|
||||
The generated API documentation, from the inline comments in [api.js](https://github.com/mozilla/pdf.js/blob/master/src/display/api.js), is available below.
|
||||
|
||||
<iframe src="draft/index.html" title="PDF.js API documentation"></iframe>
|
||||
100
docs/contents/css/a11y-light.css
Normal file
@@ -0,0 +1,100 @@
|
||||
/*!
|
||||
Theme: a11y-light
|
||||
Author: @ericwbailey
|
||||
Maintainer: @ericwbailey
|
||||
|
||||
Based on the Tomorrow Night Eighties theme: https://github.com/isagalaev/highlight.js/blob/master/src/styles/tomorrow-night-eighties.css
|
||||
|
||||
Original source: https://github.com/highlightjs/highlight.js/blob/main/src/styles/a11y-light.css
|
||||
*/
|
||||
|
||||
.hljs {
|
||||
background: #fefefe;
|
||||
color: #545454;
|
||||
}
|
||||
|
||||
/* Comment */
|
||||
.hljs-comment,
|
||||
.hljs-quote {
|
||||
color: #696969;
|
||||
}
|
||||
|
||||
/* Red */
|
||||
.hljs-variable,
|
||||
.hljs-template-variable,
|
||||
.hljs-tag,
|
||||
.hljs-name,
|
||||
.hljs-selector-id,
|
||||
.hljs-selector-class,
|
||||
.hljs-regexp,
|
||||
.hljs-deletion {
|
||||
color: #d91e18;
|
||||
}
|
||||
|
||||
/* Orange */
|
||||
.hljs-number,
|
||||
.hljs-built_in,
|
||||
.hljs-literal,
|
||||
.hljs-type,
|
||||
.hljs-params,
|
||||
.hljs-meta,
|
||||
.hljs-link {
|
||||
color: #aa5d00;
|
||||
}
|
||||
|
||||
/* Yellow */
|
||||
.hljs-attribute {
|
||||
color: #aa5d00;
|
||||
}
|
||||
|
||||
/* Green */
|
||||
.hljs-string,
|
||||
.hljs-symbol,
|
||||
.hljs-bullet,
|
||||
.hljs-addition {
|
||||
color: #008000;
|
||||
}
|
||||
|
||||
/* Blue */
|
||||
.hljs-title,
|
||||
.hljs-section {
|
||||
color: #007faa;
|
||||
}
|
||||
|
||||
/* Purple */
|
||||
.hljs-keyword,
|
||||
.hljs-selector-tag {
|
||||
color: #7928a1;
|
||||
}
|
||||
|
||||
.hljs-emphasis {
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.hljs-strong {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
@media screen and (-ms-high-contrast: active) {
|
||||
.hljs-addition,
|
||||
.hljs-attribute,
|
||||
.hljs-built_in,
|
||||
.hljs-bullet,
|
||||
.hljs-comment,
|
||||
.hljs-link,
|
||||
.hljs-literal,
|
||||
.hljs-meta,
|
||||
.hljs-number,
|
||||
.hljs-params,
|
||||
.hljs-string,
|
||||
.hljs-symbol,
|
||||
.hljs-type,
|
||||
.hljs-quote {
|
||||
color: highlight;
|
||||
}
|
||||
|
||||
.hljs-keyword,
|
||||
.hljs-selector-tag {
|
||||
font-weight: bold;
|
||||
}
|
||||
}
|
||||
7
docs/contents/css/bootstrap.min.css
vendored
Normal file
40
docs/contents/css/main.css
Normal file
@@ -0,0 +1,40 @@
|
||||
header {
|
||||
background-color: #f8f8f8;
|
||||
border-bottom: 1px solid #e5e7e8;
|
||||
|
||||
.navbar-brand {
|
||||
padding: 0;
|
||||
|
||||
img {
|
||||
height: 42px;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main {
|
||||
margin: 50px 0;
|
||||
|
||||
.description {
|
||||
font-size: 20px;
|
||||
}
|
||||
|
||||
pre {
|
||||
background-color: #f5f5f5;
|
||||
border: 1px solid #cccccc;
|
||||
border-radius: 4px;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
iframe {
|
||||
border: none;
|
||||
height: calc(0.55 * 100vh);
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
footer {
|
||||
border-top: 1px solid #e5e5e5;
|
||||
color: #777777;
|
||||
padding: 40px 0;
|
||||
text-align: center;
|
||||
}
|
||||
100
docs/contents/examples/index.md
Normal file
@@ -0,0 +1,100 @@
|
||||
---
|
||||
title: Examples
|
||||
layout: layout.njk
|
||||
slug: examples
|
||||
---
|
||||
|
||||
## Hello World Walkthrough
|
||||
|
||||
[Full source](https://github.com/mozilla/pdf.js/blob/master/examples/learning/helloworld.html)
|
||||
|
||||
PDF.js heavily relies on the use of [Promises](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise). If promises are new to you, it's recommended you become familiar with them before continuing on.
|
||||
|
||||
This tutorial shows how PDF.js can be used as a library in a web browser.
|
||||
[examples/](https://github.com/mozilla/pdf.js/tree/master/examples) provides more examples, including usage in Node.js (at [examples/node/](https://github.com/mozilla/pdf.js/tree/master/examples/node)).
|
||||
|
||||
### Document
|
||||
|
||||
The object structure of PDF.js loosely follows the structure of an actual PDF. At the top level there is a document object. From the document, more information and individual pages can be fetched. To get the document:
|
||||
|
||||
```js
|
||||
pdfjsLib.getDocument('helloworld.pdf')
|
||||
```
|
||||
|
||||
Remember though that PDF.js uses promises, and the above will return a `PDFDocumentLoadingTask` instance that has a `promise` property which is resolved with the document object.
|
||||
|
||||
```js
|
||||
var loadingTask = pdfjsLib.getDocument('helloworld.pdf');
|
||||
loadingTask.promise.then(function(pdf) {
|
||||
// you can now use *pdf* here
|
||||
});
|
||||
```
|
||||
|
||||
### Page
|
||||
Now that we have the document, we can get a page. Again, this uses promises.
|
||||
|
||||
```js
|
||||
pdf.getPage(1).then(function(page) {
|
||||
// you can now use *page* here
|
||||
});
|
||||
```
|
||||
|
||||
### Rendering the Page
|
||||
Each PDF page has its own viewport which defines the size in pixels(72DPI) and initial rotation. By default the viewport is scaled to the original size of the PDF, but this can be changed by modifying the viewport. When the viewport is created, an initial transformation matrix will also be created that takes into account the desired scale, rotation, and it transforms the coordinate system (the 0,0 point in PDF documents the bottom-left whereas canvas 0,0 is top-left).
|
||||
|
||||
```js
|
||||
var scale = 1.5;
|
||||
var viewport = page.getViewport({ scale: scale, });
|
||||
// Support HiDPI-screens.
|
||||
var outputScale = window.devicePixelRatio || 1;
|
||||
|
||||
var canvas = document.getElementById('the-canvas');
|
||||
var context = canvas.getContext('2d');
|
||||
|
||||
canvas.width = Math.floor(viewport.width * outputScale);
|
||||
canvas.height = Math.floor(viewport.height * outputScale);
|
||||
canvas.style.width = Math.floor(viewport.width) + "px";
|
||||
canvas.style.height = Math.floor(viewport.height) + "px";
|
||||
|
||||
var transform = outputScale !== 1
|
||||
? [outputScale, 0, 0, outputScale, 0, 0]
|
||||
: null;
|
||||
|
||||
var renderContext = {
|
||||
canvasContext: context,
|
||||
transform: transform,
|
||||
viewport: viewport
|
||||
};
|
||||
page.render(renderContext);
|
||||
```
|
||||
|
||||
Alternatively, if you want the canvas to render to a certain pixel size you could do the following:
|
||||
|
||||
```js
|
||||
var desiredWidth = 100;
|
||||
var viewport = page.getViewport({ scale: 1, });
|
||||
var scale = desiredWidth / viewport.width;
|
||||
var scaledViewport = page.getViewport({ scale: scale, });
|
||||
```
|
||||
|
||||
## Interactive examples
|
||||
|
||||
### Hello World with document load error handling
|
||||
|
||||
The example demonstrates how promises can be used to handle errors during loading.
|
||||
It also demonstrates how to wait until a page is loaded and rendered.
|
||||
|
||||
<script async src="https://jsfiddle.net/pdfjs/9engc9mw/embed/html,css,result/"></script>
|
||||
|
||||
### Hello World using base64 encoded PDF
|
||||
|
||||
The PDF.js can accept any decoded base64 data as an array.
|
||||
|
||||
<script async src="https://jsfiddle.net/pdfjs/cq0asLqz/embed/html,css,result/"></script>
|
||||
|
||||
### Previous/Next example
|
||||
|
||||
The same canvas cannot be used to perform to draw two pages at the same time --
|
||||
the example demonstrates how to wait on previous operation to be complete.
|
||||
|
||||
<script async src="https://jsfiddle.net/pdfjs/wagvs9Lf/embed/html,css,result/"></script>
|
||||
127
docs/contents/getting_started/index.md
Normal file
@@ -0,0 +1,127 @@
|
||||
---
|
||||
title: Getting Started
|
||||
layout: layout.njk
|
||||
slug: getting_started
|
||||
---
|
||||
|
||||
# Getting Started
|
||||
|
||||
An introduction to PDF.js with examples.
|
||||
|
||||
## Introduction
|
||||
|
||||
Before downloading PDF.js please take a moment to understand the different layers of the PDF.js project.
|
||||
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Layer</th>
|
||||
<th>About</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>Core</td>
|
||||
<td>The core layer is where a binary PDF is parsed and interpreted. This layer is the foundation for all subsequent layers. It is not documented here because using it directly is considered an advanced usage and the API is likely to change. For an example of using the core layer see the <a href="https://github.com/brendandahl/pdf.js.utils/tree/master/browser">PDF Object Browser</a>.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Display</td>
|
||||
<td>The display layer takes the core layer and exposes an easier to use API to render PDFs and get other information out of a document. This API is what the version number is based on.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Viewer</td>
|
||||
<td>The viewer is built on the display layer and is the UI for PDF viewer in Firefox and the other browser extensions within the project. It can be a good starting point for building your own viewer. <em>However, we do ask if you plan to embed the viewer in your own site, that it not just be an unmodified version. Please re-skin it or build upon it.</em></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
## Download
|
||||
|
||||
Please refer to [this wiki page](https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-support) for information about supported browsers.
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
<h3>Prebuilt (modern browsers)</h3>
|
||||
<p>
|
||||
Includes the generic build of PDF.js and the viewer.
|
||||
</p>
|
||||
<a type="button" class="btn btn-primary" href="https://github.com/mozilla/pdf.js/releases/download/vSTABLE_VERSION/pdfjs-STABLE_VERSION-dist.zip">Stable (vSTABLE_VERSION)</a>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<h3>Prebuilt (older browsers)</h3>
|
||||
<p>
|
||||
Includes the generic build of PDF.js and the viewer.
|
||||
</p>
|
||||
<a type="button" class="btn btn-primary" href="https://github.com/mozilla/pdf.js/releases/download/vSTABLE_VERSION/pdfjs-STABLE_VERSION-legacy-dist.zip">Stable (vSTABLE_VERSION)</a>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<h3>Source</h3>
|
||||
To get a local copy of the current code, clone it using git:
|
||||
<pre><code>$ git clone https://github.com/mozilla/pdf.js.git
|
||||
$ cd pdf.js
|
||||
</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
## Including via a CDN
|
||||
|
||||
PDF.js is hosted on several free CDNs:
|
||||
- https://www.jsdelivr.com/package/npm/pdfjs-dist
|
||||
- https://cdnjs.com/libraries/pdf.js
|
||||
- https://unpkg.com/pdfjs-dist/
|
||||
|
||||
## File Layout Overview
|
||||
|
||||
Note that we only mention the most relevant files and folders.
|
||||
|
||||
### Prebuilt
|
||||
|
||||
```plaintext
|
||||
├── build/
|
||||
│ ├── pdf.mjs - display layer
|
||||
│ ├── pdf.mjs.map - display layer's source map
|
||||
│ ├── pdf.worker.mjs - core layer
|
||||
│ └── pdf.worker.mjs.map - core layer's source map
|
||||
├── web/
|
||||
│ ├── cmaps/ - character maps (required by core)
|
||||
│ ├── compressed.tracemonkey-pldi-09.pdf - PDF file for testing purposes
|
||||
│ ├── images/ - images for the viewer and annotation icons
|
||||
│ ├── locale/ - translation files
|
||||
│ ├── viewer.css - viewer style sheet
|
||||
│ ├── viewer.html - viewer layout
|
||||
│ ├── viewer.mjs - viewer layer
|
||||
│ └── viewer.mjs.map - viewer layer's source map
|
||||
└── LICENSE
|
||||
```
|
||||
|
||||
### Source
|
||||
|
||||
```plaintext
|
||||
├── docs/ - website source code
|
||||
├── examples/ - simple usage examples
|
||||
├── extensions/ - browser extension source code
|
||||
├── external/ - third party code
|
||||
├── l10n/ - translation files
|
||||
├── src/
|
||||
│ ├── core/ - core layer
|
||||
│ ├── display/ - display layer
|
||||
│ ├── shared/ - shared code between the core and display layers
|
||||
│ ├── interfaces.js - interface definitions for the core/display layers
|
||||
│ └── pdf.*.js - wrapper files for bundling
|
||||
├── test/ - unit, font, reference, and integration tests
|
||||
├── web/ - viewer layer
|
||||
├── LICENSE
|
||||
├── README.md
|
||||
├── gulpfile.mjs - build scripts/logic
|
||||
├── package-lock.json - pinned dependency versions
|
||||
└── package.json - package definition and dependencies
|
||||
```
|
||||
|
||||
## Trying the Viewer
|
||||
|
||||
With the prebuilt or source version, open `web/viewer.html` in a browser and the test pdf should load. Note: the worker is not enabled for file:// urls, so use a server. If you're using the source build and have node, you can run `npx gulp server`.
|
||||
|
||||
## More Information
|
||||
|
||||
For a further walkthrough of a minimal viewer, see the hello world example. More documentation can be found in our [wiki](https://github.com/mozilla/pdf.js/wiki) too.
|
||||
BIN
docs/contents/images/favicon.ico
Normal file
|
After Width: | Height: | Size: 32 KiB |
41
docs/contents/images/logo.svg
Normal file
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="64"
|
||||
height="64">
|
||||
<path
|
||||
d="M 4.8364028,0.4891005 32.096378,4.5726641 59.163597,0.4891005 54.680408,57.805097 32.096378,63.510899 8.3116209,57.805097 z"
|
||||
style="fill:#e5e7e8;fill-opacity:1;fill-rule:nonzero;stroke:#cccccc" />
|
||||
<path
|
||||
d="M 32.096378,10.745857 53.925414,6.8301117 51.016574,53.81906 32.096378,58.517955 z"
|
||||
style="fill:#ffffff;fill-opacity:1;fill-rule:nonzero;stroke:none" />
|
||||
<rect
|
||||
width="34.027256"
|
||||
height="19.136194"
|
||||
x="3.7557135"
|
||||
y="22.431904"
|
||||
style="fill:#ff2600;fill-opacity:1;fill-rule:nonzero;stroke:none" />
|
||||
<rect
|
||||
width="23.480518"
|
||||
height="19.136194"
|
||||
x="36.763767"
|
||||
y="22.431904"
|
||||
style="fill:#ff501a;fill-opacity:1;fill-rule:nonzero;stroke:none" />
|
||||
<g transform="matrix(0.42778543,0,0,0.42778543,58.617711,9.6737064)">
|
||||
<path
|
||||
d="m -120.53125,34.59375 0,35.1875 6.53125,0 0,-5.9375 0,-5.96875 8.875,0 4.15625,-3.71875 0,-7.71875 0,-7.71875 -4.21875,-4.125 -15.34375,0 z m 6.53125,6.8125 6.21875,0 0,10.21875 -6.21875,0 0,-10.21875 z"
|
||||
style="fill:#ffffff;fill-opacity:1" />
|
||||
<path
|
||||
d="m -98.125,34.59375 0,35.1875 16.125,0 3.75,-3.625 0,-27.96875 -3.96875,-3.59375 -15.90625,0 z m 6.8125,6.8125 6.8125,0 0,21.5625 -6.8125,0 0,-21.5625 z" id="path3056"
|
||||
style="fill:#ffffff;fill-opacity:1" />
|
||||
<path
|
||||
d="m -74.856072,34.602929 c 5.485697,0 10.971394,0 16.457091,0 0,2.269943 0,4.539887 0,6.80983 -3.404915,0 -6.809831,0 -10.214746,0 0,2.472069 0,4.944138 0,7.416206 2.93201,0.110496 5.864021,-0.110494 8.796031,0 l 0,3.366025 0,3.375914 c -2.93201,0.110495 -5.864021,-0.110495 -8.796031,0 l 0,7.146446 0,7.069702 c -2.080782,0 -4.161563,0 -6.242345,0 0,-11.728041 0,-23.456082 0,-35.184123 z"
|
||||
style="fill:#ffffff;fill-opacity:1" />
|
||||
<path
|
||||
d="m -42.8813,67.942723 -2.181762,-1.844329 c 0,-1.986871 0,-3.973742 0,-5.960613 2.175363,6.7e-4 4.350725,0.0013 6.526088,0.002 0,0.94581 0,1.891619 0,2.837429 1.891619,0 3.783239,0 5.674858,0 0,-9.458098 0,-18.916195 0,-28.374293 2.175363,-1.26e-4 4.350725,-2.52e-4 6.526088,-3.78e-4 0,10.498614 0,20.997229 0,31.495843 -1.454508,1.229554 -2.909019,2.459106 -4.363529,3.688658 -3.333325,0 -6.666651,0 -9.999976,0 -0.727237,-0.614768 -1.454563,-1.229595 -2.181767,-1.844308 z"
|
||||
style="fill:#ffffff;fill-opacity:1" />
|
||||
<path
|
||||
d="m -21.316836,67.942723 -2.109122,-1.844329 0,-5.978379 c 2.061011,0 4.392437,0.01978 6.453448,0.01978 0,0.94581 0,1.891619 0,2.837429 2.269943,0 4.539887,0 6.80983,0 0,-2.459105 0,-4.918211 0,-7.377316 -3.196954,0 -6.393909,0 -9.590863,0 l -3.745055,-3.688659 0,-6.80983 0,-6.80983 3.745055,-3.688658 c 3.863156,0 7.726313,0 11.5894692,0 l 4.2511106,3.688658 c -0.00204,1.797039 -0.00472,3.594077 -0.00737,5.391115 -2.0807816,0 -4.1615632,0 -6.2423448,0 0,-0.756648 0,-1.513295 0,-2.269943 -2.269943,0 -4.539887,0 -6.80983,0 0,2.459105 0,4.918211 0,7.377316 2.990126,0 5.980253,0 8.9703792,0 1.3605964,1.204585 4.0817956,3.613749 4.0817956,3.613749 0,4.555246 0,9.110492 0,13.665738 l -2.0892845,1.858745 -2.0892901,1.858745 -5.5180792,0 -5.51808,0 z"
|
||||
style="fill:#ffffff;fill-opacity:1" />
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.2 KiB |
13
docs/contents/index.md
Normal file
@@ -0,0 +1,13 @@
|
||||
---
|
||||
title: Home
|
||||
layout: layout.njk
|
||||
slug: home
|
||||
---
|
||||
|
||||
<h1 class="text-center">PDF.js</h1>
|
||||
<p class="text-center description">A general-purpose, web standards-based platform for parsing and rendering PDFs.</p>
|
||||
<p class="text-center">
|
||||
<a type="button" class="btn btn-outline-dark" href="/getting_started/#download">Download</a>
|
||||
<a type="button" class="btn btn-outline-dark" href="https://github.com/mozilla/pdf.js#online-demo">Demo</a>
|
||||
<a type="button" class="btn btn-outline-dark" href="https://github.com/mozilla/pdf.js">GitHub Project</a>
|
||||
</p>
|
||||
7
docs/contents/js/bootstrap.min.js
vendored
Normal file
2
docs/contents/js/jquery-3.7.1.min.js
vendored
Normal file
56
docs/templates/layout.njk
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>{{ sitename }} - {{ title }}</title>
|
||||
<meta charset="utf-8">
|
||||
<meta name="description" content="{{ description }}">
|
||||
<meta name="viewport" content="device-width, initial-scale=1.0">
|
||||
<script src="/js/jquery-3.7.1.min.js"></script>
|
||||
<script src="/js/bootstrap.min.js"></script>
|
||||
<link rel="shortcut icon" href="/images/favicon.ico">
|
||||
<link rel="stylesheet" href="/css/bootstrap.min.css">
|
||||
<link rel="stylesheet" href="/css/a11y-light.css">
|
||||
<link rel="stylesheet" href="/css/main.css">
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<div class="container">
|
||||
<div class="navbar-brand"><img src="/images/logo.svg"></div>
|
||||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav mr-auto">
|
||||
<li class="nav-item {{ 'active' if slug == 'home' else '' }}">
|
||||
<a class="nav-link" href="/">Home</a>
|
||||
</li>
|
||||
<li class="nav-item {{ 'active' if slug == 'getting_started' else '' }}">
|
||||
<a class="nav-link" href="/getting_started">Getting started</a>
|
||||
</li>
|
||||
<li class="nav-item {{ 'active' if slug == 'examples' else '' }}">
|
||||
<a class="nav-link" href="/examples">Examples</a>
|
||||
</li>
|
||||
<li class="nav-item {{ 'active' if slug == 'api' else '' }}">
|
||||
<a class="nav-link" href="/api">API</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions">FAQ</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
</header>
|
||||
<main class="container">
|
||||
{{ contents | safe }}
|
||||
</main>
|
||||
<footer>
|
||||
<p>© Mozilla and individual contributors</p>
|
||||
<p>
|
||||
PDF.js is licensed under <a href="https://github.com/mozilla/pdf.js/blob/master/LICENSE">Apache 2.0</a>,
|
||||
documentation is licensed under <a href="https://creativecommons.org/licenses/by-sa/2.5">CC BY-SA 2.5</a>.
|
||||
</p>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
473
eslint.config.mjs
Normal file
@@ -0,0 +1,473 @@
|
||||
import globals from "globals";
|
||||
|
||||
import import_ from "eslint-plugin-import";
|
||||
import jasmine from "eslint-plugin-jasmine";
|
||||
import json from "eslint-plugin-json";
|
||||
import noUnsanitized from "eslint-plugin-no-unsanitized";
|
||||
import perfectionist from "eslint-plugin-perfectionist";
|
||||
import prettierRecommended from "eslint-plugin-prettier/recommended";
|
||||
import unicorn from "eslint-plugin-unicorn";
|
||||
|
||||
const jsFiles = folder => {
|
||||
const prefix = folder === "." ? "" : folder + "/";
|
||||
return [prefix + "**/*.js", prefix + "**/*.jsm", prefix + "**/*.mjs"];
|
||||
};
|
||||
|
||||
// Include all files referenced in extensions/chromium/background.js
|
||||
const chromiumExtensionServiceWorkerFiles = [
|
||||
"extensions/chromium/extension-router.js",
|
||||
"extensions/chromium/options/migration.js",
|
||||
"extensions/chromium/pdfHandler.js",
|
||||
"extensions/chromium/preserve-referer.js",
|
||||
"extensions/chromium/suppress-update.js",
|
||||
"extensions/chromium/telemetry.js",
|
||||
];
|
||||
|
||||
export default [
|
||||
{
|
||||
ignores: [
|
||||
"**/build/",
|
||||
"**/l10n/",
|
||||
"**/docs/",
|
||||
"**/node_modules/",
|
||||
"external/bcmaps/",
|
||||
"external/builder/fixtures/",
|
||||
"external/builder/fixtures_babel/",
|
||||
"external/openjpeg/",
|
||||
"external/qcms/",
|
||||
"external/quickjs/",
|
||||
"test/stats/results/",
|
||||
"test/tmp/",
|
||||
"test/pdfs/",
|
||||
"web/locale/",
|
||||
"web/wasm/",
|
||||
"**/*~/",
|
||||
],
|
||||
},
|
||||
|
||||
/* ======================================================================== *\
|
||||
Base configuration
|
||||
\* ======================================================================== */
|
||||
|
||||
prettierRecommended,
|
||||
{
|
||||
files: ["**/*.json"],
|
||||
...json.configs.recommended,
|
||||
},
|
||||
{
|
||||
files: jsFiles("."),
|
||||
ignores: chromiumExtensionServiceWorkerFiles,
|
||||
languageOptions: {
|
||||
globals: globals.browser,
|
||||
},
|
||||
},
|
||||
{
|
||||
files: jsFiles("."),
|
||||
|
||||
plugins: {
|
||||
import: import_.flatConfigs.recommended.plugins.import,
|
||||
json,
|
||||
"no-unsanitized": noUnsanitized,
|
||||
perfectionist,
|
||||
unicorn,
|
||||
},
|
||||
|
||||
languageOptions: {
|
||||
globals: {
|
||||
...globals.worker,
|
||||
PDFJSDev: "readonly",
|
||||
__raw_import__: "readonly",
|
||||
},
|
||||
|
||||
ecmaVersion: 2025,
|
||||
sourceType: "module",
|
||||
},
|
||||
|
||||
rules: {
|
||||
"import/export": "error",
|
||||
"import/exports-last": "error",
|
||||
"import/extensions": ["error", "always", { ignorePackages: true }],
|
||||
"import/first": "error",
|
||||
"import/named": "error",
|
||||
"import/no-cycle": "error",
|
||||
"import/no-empty-named-blocks": "error",
|
||||
"import/no-commonjs": "error",
|
||||
"import/no-mutable-exports": "error",
|
||||
"import/no-restricted-paths": [
|
||||
"error",
|
||||
{
|
||||
zones: [
|
||||
{
|
||||
target: "./web",
|
||||
from: "./src",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
"import/no-self-import": "error",
|
||||
"import/no-unresolved": [
|
||||
"error",
|
||||
{
|
||||
ignore: [
|
||||
"display",
|
||||
"pdfjs",
|
||||
"pdfjs-lib",
|
||||
"pdfjs-web",
|
||||
"web",
|
||||
"fluent-bundle",
|
||||
"fluent-dom",
|
||||
// See https://github.com/firebase/firebase-admin-node/discussions/1359.
|
||||
"eslint-plugin-perfectionist",
|
||||
],
|
||||
},
|
||||
],
|
||||
"no-unsanitized/method": "error",
|
||||
"no-unsanitized/property": "error",
|
||||
"perfectionist/sort-exports": "error",
|
||||
"perfectionist/sort-named-exports": "error",
|
||||
"unicorn/no-abusive-eslint-disable": "error",
|
||||
"unicorn/no-array-reduce": ["error", { allowSimpleOperations: true }],
|
||||
"unicorn/no-console-spaces": "error",
|
||||
"unicorn/no-instanceof-builtins": "error",
|
||||
"unicorn/no-invalid-remove-event-listener": "error",
|
||||
"unicorn/no-new-buffer": "error",
|
||||
"unicorn/no-single-promise-in-promise-methods": "error",
|
||||
"unicorn/no-typeof-undefined": ["error", { checkGlobalVariables: false }],
|
||||
"unicorn/no-unnecessary-array-flat-depth": "error",
|
||||
"unicorn/no-unnecessary-array-splice-count": "error",
|
||||
"unicorn/no-unnecessary-slice-end": "error",
|
||||
"unicorn/no-useless-promise-resolve-reject": "error",
|
||||
"unicorn/no-useless-spread": "error",
|
||||
"unicorn/prefer-array-find": "error",
|
||||
"unicorn/prefer-array-flat": "error",
|
||||
"unicorn/prefer-array-flat-map": "error",
|
||||
"unicorn/prefer-array-index-of": "error",
|
||||
"unicorn/prefer-array-some": "error",
|
||||
"unicorn/prefer-at": "error",
|
||||
"unicorn/prefer-date-now": "error",
|
||||
"unicorn/prefer-dom-node-append": "error",
|
||||
"unicorn/prefer-dom-node-remove": "error",
|
||||
"unicorn/prefer-import-meta-properties": "error",
|
||||
"unicorn/prefer-includes": "error",
|
||||
"unicorn/prefer-logical-operator-over-ternary": "error",
|
||||
"unicorn/prefer-modern-dom-apis": "error",
|
||||
"unicorn/prefer-modern-math-apis": "error",
|
||||
"unicorn/prefer-negative-index": "error",
|
||||
"unicorn/prefer-optional-catch-binding": "error",
|
||||
"unicorn/prefer-regexp-test": "error",
|
||||
"unicorn/prefer-single-call": "error",
|
||||
"unicorn/prefer-string-replace-all": "error",
|
||||
"unicorn/prefer-string-starts-ends-with": "error",
|
||||
"unicorn/prefer-ternary": ["error", "only-single-line"],
|
||||
"unicorn/throw-new-error": "error",
|
||||
|
||||
// Possible errors
|
||||
"for-direction": "error",
|
||||
"getter-return": "error",
|
||||
"no-async-promise-executor": "error",
|
||||
"no-cond-assign": ["error", "except-parens"],
|
||||
"no-constant-condition": ["error", { checkLoops: false }],
|
||||
"no-debugger": "error",
|
||||
"no-dupe-args": "error",
|
||||
"no-dupe-else-if": "error",
|
||||
"no-dupe-keys": "error",
|
||||
"no-duplicate-case": "error",
|
||||
"no-empty": ["error", { allowEmptyCatch: true }],
|
||||
"no-empty-character-class": "error",
|
||||
"no-ex-assign": "error",
|
||||
"no-extra-boolean-cast": "error",
|
||||
"no-func-assign": "error",
|
||||
"no-inner-declarations": ["error", "functions"],
|
||||
"no-invalid-regexp": "error",
|
||||
"no-irregular-whitespace": "error",
|
||||
"no-loss-of-precision": "error",
|
||||
"no-obj-calls": "error",
|
||||
"no-promise-executor-return": "error",
|
||||
"no-regex-spaces": "error",
|
||||
"no-setter-return": "error",
|
||||
"no-sparse-arrays": "error",
|
||||
"no-template-curly-in-string": "error",
|
||||
"no-unexpected-multiline": "error",
|
||||
"no-unreachable": "error",
|
||||
"no-unsafe-finally": "error",
|
||||
"no-unsafe-negation": "error",
|
||||
"no-unsafe-optional-chaining": [
|
||||
"error",
|
||||
{ disallowArithmeticOperators: true },
|
||||
],
|
||||
"no-unused-private-class-members": "error",
|
||||
"use-isnan": ["error", { enforceForIndexOf: true }],
|
||||
"valid-typeof": ["error", { requireStringLiterals: true }],
|
||||
|
||||
// Best Practices
|
||||
"accessor-pairs": [
|
||||
"error",
|
||||
{ setWithoutGet: true, enforceForClassMembers: true },
|
||||
],
|
||||
"consistent-return": "error",
|
||||
curly: ["error", "all"],
|
||||
"default-case-last": "error",
|
||||
"dot-notation": "error",
|
||||
eqeqeq: ["error", "always"],
|
||||
"grouped-accessor-pairs": ["error", "getBeforeSet"],
|
||||
"no-alert": "error",
|
||||
"no-caller": "error",
|
||||
"no-else-return": "error",
|
||||
"no-empty-pattern": "error",
|
||||
"no-eval": "error",
|
||||
"no-extend-native": "error",
|
||||
"no-extra-bind": "error",
|
||||
"no-extra-label": "error",
|
||||
"no-fallthrough": "error",
|
||||
"no-floating-decimal": "error",
|
||||
"no-global-assign": "error",
|
||||
"no-implied-eval": "error",
|
||||
"no-iterator": "error",
|
||||
"no-lone-blocks": "error",
|
||||
"no-lonely-if": "error",
|
||||
"no-multi-str": "error",
|
||||
"no-new": "error",
|
||||
"no-new-func": "error",
|
||||
"no-new-symbol": "error",
|
||||
"no-new-wrappers": "error",
|
||||
"no-octal-escape": "error",
|
||||
"no-octal": "error",
|
||||
"no-redeclare": "error",
|
||||
"no-return-await": "error",
|
||||
"no-self-assign": "error",
|
||||
"no-self-compare": "error",
|
||||
"no-throw-literal": "error",
|
||||
"no-unused-expressions": "error",
|
||||
"no-unused-labels": "error",
|
||||
"no-useless-call": "error",
|
||||
"no-useless-catch": "error",
|
||||
"no-useless-concat": "error",
|
||||
"no-useless-escape": "error",
|
||||
"no-useless-return": "error",
|
||||
"prefer-promise-reject-errors": "error",
|
||||
"prefer-spread": "error",
|
||||
"wrap-iife": ["error", "any"],
|
||||
yoda: ["error", "never", { exceptRange: true }],
|
||||
|
||||
// Strict Mode
|
||||
strict: ["off", "global"],
|
||||
|
||||
// Variables
|
||||
"no-delete-var": "error",
|
||||
"no-label-var": "error",
|
||||
"no-shadow": "error",
|
||||
"no-shadow-restricted-names": "error",
|
||||
"no-undef-init": "error",
|
||||
"no-undef": ["error", { typeof: true }],
|
||||
"no-unused-vars": ["error", { vars: "all", args: "none" }],
|
||||
"no-use-before-define": [
|
||||
"error",
|
||||
{ functions: false, classes: false, variables: false },
|
||||
],
|
||||
|
||||
// Stylistic Issues
|
||||
"lines-between-class-members": ["error", "always"],
|
||||
"max-len": ["error", { code: 1000, comments: 80, ignoreUrls: true }],
|
||||
"new-cap": ["error", { newIsCap: true, capIsNew: false }],
|
||||
"no-array-constructor": "error",
|
||||
"no-multiple-empty-lines": ["error", { max: 1, maxEOF: 0, maxBOF: 1 }],
|
||||
"no-nested-ternary": "error",
|
||||
"no-new-object": "error",
|
||||
"no-restricted-syntax": [
|
||||
"error",
|
||||
{
|
||||
selector:
|
||||
"BinaryExpression[operator='instanceof'][right.name='Object']",
|
||||
message: "Use `typeof` rather than `instanceof Object`.",
|
||||
},
|
||||
{
|
||||
selector: "CallExpression[callee.name='assert'][arguments.length!=2]",
|
||||
message: "`assert()` must always be invoked with two arguments.",
|
||||
},
|
||||
{
|
||||
selector: "CallExpression[callee.name='isCmd'][arguments.length<2]",
|
||||
message:
|
||||
"Use `instanceof Cmd` rather than `isCmd()` with one argument.",
|
||||
},
|
||||
{
|
||||
selector: "CallExpression[callee.name='isDict'][arguments.length<2]",
|
||||
message:
|
||||
"Use `instanceof Dict` rather than `isDict()` with one argument.",
|
||||
},
|
||||
{
|
||||
selector: "CallExpression[callee.name='isName'][arguments.length<2]",
|
||||
message:
|
||||
"Use `instanceof Name` rather than `isName()` with one argument.",
|
||||
},
|
||||
{
|
||||
selector: "NewExpression[callee.name='Cmd']",
|
||||
message: "Use `Cmd.get()` rather than `new Cmd()`.",
|
||||
},
|
||||
{
|
||||
selector: "NewExpression[callee.name='Name']",
|
||||
message: "Use `Name.get()` rather than `new Name()`.",
|
||||
},
|
||||
{
|
||||
selector: "NewExpression[callee.name='ObjectLoader']",
|
||||
message:
|
||||
"Use `ObjectLoader.load()` rather than `new ObjectLoader()`.",
|
||||
},
|
||||
{
|
||||
selector: "NewExpression[callee.name='Ref']",
|
||||
message: "Use `Ref.get()` rather than `new Ref()`.",
|
||||
},
|
||||
{
|
||||
selector: "ExportNamedDeclaration[declaration]",
|
||||
message:
|
||||
"Separate the declaration and the export statement, using `export { ... }`.",
|
||||
},
|
||||
{
|
||||
selector: "ExportDefaultDeclaration:has(> :declaration)",
|
||||
message:
|
||||
"Separate the declaration and the export statement, using `export default <variable name>`.",
|
||||
},
|
||||
],
|
||||
"no-unneeded-ternary": "error",
|
||||
"operator-assignment": "error",
|
||||
"prefer-exponentiation-operator": "error",
|
||||
"spaced-comment": ["error", "always", { block: { balanced: true } }],
|
||||
|
||||
// ECMAScript 6
|
||||
"arrow-body-style": ["error", "as-needed"],
|
||||
"constructor-super": "error",
|
||||
"no-class-assign": "error",
|
||||
"no-const-assign": "error",
|
||||
"no-dupe-class-members": "error",
|
||||
"no-duplicate-imports": "error",
|
||||
"no-this-before-super": "error",
|
||||
"no-useless-computed-key": "error",
|
||||
"no-useless-constructor": "error",
|
||||
"no-useless-rename": "error",
|
||||
"no-var": "error",
|
||||
"object-shorthand": ["error", "always", { avoidQuotes: true }],
|
||||
"prefer-const": "error",
|
||||
"require-yield": "error",
|
||||
"sort-imports": ["error", { ignoreCase: true }],
|
||||
"template-curly-spacing": ["error", "never"],
|
||||
},
|
||||
},
|
||||
{
|
||||
files: jsFiles("src"),
|
||||
rules: {
|
||||
"no-console": "error",
|
||||
},
|
||||
},
|
||||
|
||||
/* ======================================================================== *\
|
||||
Test-specific rules
|
||||
\* ======================================================================== */
|
||||
|
||||
{
|
||||
files: jsFiles("test"),
|
||||
|
||||
plugins: { jasmine },
|
||||
|
||||
languageOptions: {
|
||||
globals: {
|
||||
...globals.node,
|
||||
...globals.jasmine,
|
||||
},
|
||||
},
|
||||
rules: {
|
||||
...jasmine.configs.recommended.rules,
|
||||
"jasmine/new-line-before-expect": "off",
|
||||
"jasmine/new-line-between-declarations": "off",
|
||||
"jasmine/no-focused-tests": "error",
|
||||
"jasmine/no-pending-tests": "off",
|
||||
"jasmine/no-spec-dupes": ["error", "branch"],
|
||||
"jasmine/no-suite-dupes": ["error", "branch"],
|
||||
"jasmine/prefer-jasmine-matcher": "off",
|
||||
"jasmine/prefer-toHaveBeenCalledWith": "off",
|
||||
},
|
||||
},
|
||||
{
|
||||
files: jsFiles("test/unit"),
|
||||
rules: {
|
||||
"import/no-unresolved": ["error", { ignore: ["pdfjs/"] }],
|
||||
"no-console": ["error", { allow: ["warn", "error"] }],
|
||||
},
|
||||
},
|
||||
{
|
||||
files: jsFiles("test/integration"),
|
||||
rules: {
|
||||
"no-console": ["error", { allow: ["warn", "error"] }],
|
||||
"no-restricted-syntax": [
|
||||
"error",
|
||||
{
|
||||
selector: "CallExpression[callee.name='waitForTimeout']",
|
||||
message:
|
||||
"`waitForTimeout` can cause intermittent failures and should not be used (see issue #17656 for replacements).",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
/* ======================================================================== *\
|
||||
External libraries
|
||||
\* ======================================================================== */
|
||||
|
||||
{
|
||||
files: jsFiles("external"),
|
||||
|
||||
languageOptions: { globals: globals.node },
|
||||
},
|
||||
|
||||
/* ======================================================================== *\
|
||||
Examples
|
||||
\* ======================================================================== */
|
||||
|
||||
{
|
||||
files: jsFiles("examples"),
|
||||
|
||||
languageOptions: {
|
||||
globals: {
|
||||
pdfjsImageDecoders: false,
|
||||
pdfjsLib: false,
|
||||
pdfjsViewer: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
files: [...jsFiles("examples/node"), ...jsFiles("examples/webpack")],
|
||||
|
||||
languageOptions: { globals: globals.node },
|
||||
},
|
||||
|
||||
/* ======================================================================== *\
|
||||
Chromium extension
|
||||
\* ======================================================================== */
|
||||
|
||||
{
|
||||
files: jsFiles("extensions/chromium"),
|
||||
|
||||
languageOptions: {
|
||||
globals: globals.webextensions,
|
||||
sourceType: "script",
|
||||
},
|
||||
|
||||
rules: {
|
||||
"no-var": "off",
|
||||
},
|
||||
},
|
||||
{
|
||||
files: chromiumExtensionServiceWorkerFiles,
|
||||
|
||||
languageOptions: {
|
||||
globals: globals.serviceworker,
|
||||
sourceType: "script",
|
||||
},
|
||||
},
|
||||
|
||||
/* ======================================================================== *\
|
||||
Other
|
||||
\* ======================================================================== */
|
||||
{
|
||||
files: ["gulpfile.mjs"],
|
||||
languageOptions: { globals: globals.node },
|
||||
},
|
||||
];
|
||||
43
examples/components/pageviewer.html
Normal file
@@ -0,0 +1,43 @@
|
||||
<!DOCTYPE html>
|
||||
<!--
|
||||
Copyright 2014 Mozilla Foundation
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<html dir="ltr" mozdisallowselectionprint>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
|
||||
<meta name="google" content="notranslate">
|
||||
<title>PDF.js page viewer using built components</title>
|
||||
|
||||
<style>
|
||||
body {
|
||||
background-color: #808080;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
</style>
|
||||
|
||||
<link rel="stylesheet" href="../../node_modules/pdfjs-dist/web/pdf_viewer.css">
|
||||
|
||||
<script src="../../node_modules/pdfjs-dist/build/pdf.mjs" type="module"></script>
|
||||
<script src="../../node_modules/pdfjs-dist/web/pdf_viewer.mjs" type="module"></script>
|
||||
</head>
|
||||
|
||||
<body tabindex="1">
|
||||
<div id="pageContainer" class="pdfViewer singlePageView"></div>
|
||||
|
||||
<script src="pageviewer.mjs" type="module"></script>
|
||||
</body>
|
||||
</html>
|
||||
63
examples/components/pageviewer.mjs
Normal file
@@ -0,0 +1,63 @@
|
||||
/* Copyright 2014 Mozilla Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
if (!pdfjsLib.getDocument || !pdfjsViewer.PDFPageView) {
|
||||
// eslint-disable-next-line no-alert
|
||||
alert("Please build the pdfjs-dist library using\n `gulp dist-install`");
|
||||
}
|
||||
|
||||
// The workerSrc property shall be specified.
|
||||
//
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc =
|
||||
"../../node_modules/pdfjs-dist/build/pdf.worker.mjs";
|
||||
|
||||
// Some PDFs need external cmaps.
|
||||
//
|
||||
const CMAP_URL = "../../node_modules/pdfjs-dist/cmaps/";
|
||||
const CMAP_PACKED = true;
|
||||
|
||||
const DEFAULT_URL = "../../web/compressed.tracemonkey-pldi-09.pdf";
|
||||
const PAGE_TO_VIEW = 1;
|
||||
const SCALE = 1.0;
|
||||
|
||||
const ENABLE_XFA = true;
|
||||
|
||||
const container = document.getElementById("pageContainer");
|
||||
|
||||
const eventBus = new pdfjsViewer.EventBus();
|
||||
|
||||
// Loading document.
|
||||
const loadingTask = pdfjsLib.getDocument({
|
||||
url: DEFAULT_URL,
|
||||
cMapUrl: CMAP_URL,
|
||||
cMapPacked: CMAP_PACKED,
|
||||
enableXfa: ENABLE_XFA,
|
||||
});
|
||||
|
||||
const pdfDocument = await loadingTask.promise;
|
||||
// Document loaded, retrieving the page.
|
||||
const pdfPage = await pdfDocument.getPage(PAGE_TO_VIEW);
|
||||
|
||||
// Creating the page view with default parameters.
|
||||
const pdfPageView = new pdfjsViewer.PDFPageView({
|
||||
container,
|
||||
id: PAGE_TO_VIEW,
|
||||
scale: SCALE,
|
||||
defaultViewport: pdfPage.getViewport({ scale: SCALE }),
|
||||
eventBus,
|
||||
});
|
||||
// Associate the actual page with the view, and draw it.
|
||||
pdfPageView.setPdfPage(pdfPage);
|
||||
pdfPageView.draw();
|
||||
51
examples/components/simpleviewer.html
Normal file
@@ -0,0 +1,51 @@
|
||||
<!DOCTYPE html>
|
||||
<!--
|
||||
Copyright 2014 Mozilla Foundation
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<html dir="ltr" mozdisallowselectionprint>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
|
||||
<meta name="google" content="notranslate">
|
||||
<title>PDF.js viewer using built components</title>
|
||||
|
||||
<style>
|
||||
body {
|
||||
background-color: #808080;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
#viewerContainer {
|
||||
overflow: auto;
|
||||
position: absolute;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
</style>
|
||||
|
||||
<link rel="stylesheet" href="../../node_modules/pdfjs-dist/web/pdf_viewer.css">
|
||||
|
||||
<script src="../../node_modules/pdfjs-dist/build/pdf.mjs" type="module"></script>
|
||||
<script src="../../node_modules/pdfjs-dist/web/pdf_viewer.mjs" type="module"></script>
|
||||
</head>
|
||||
|
||||
<body tabindex="1">
|
||||
<div id="viewerContainer">
|
||||
<div id="viewer" class="pdfViewer"></div>
|
||||
</div>
|
||||
|
||||
<script src="simpleviewer.mjs" type="module"></script>
|
||||
</body>
|
||||
</html>
|
||||
97
examples/components/simpleviewer.mjs
Normal file
@@ -0,0 +1,97 @@
|
||||
/* Copyright 2014 Mozilla Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
if (!pdfjsLib.getDocument || !pdfjsViewer.PDFViewer) {
|
||||
// eslint-disable-next-line no-alert
|
||||
alert("Please build the pdfjs-dist library using\n `gulp dist-install`");
|
||||
}
|
||||
|
||||
// The workerSrc property shall be specified.
|
||||
//
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc =
|
||||
"../../node_modules/pdfjs-dist/build/pdf.worker.mjs";
|
||||
|
||||
// Some PDFs need external cmaps.
|
||||
//
|
||||
const CMAP_URL = "../../node_modules/pdfjs-dist/cmaps/";
|
||||
const CMAP_PACKED = true;
|
||||
|
||||
const DEFAULT_URL = "../../web/compressed.tracemonkey-pldi-09.pdf";
|
||||
// To test the AcroForm and/or scripting functionality, try e.g. this file:
|
||||
// "../../test/pdfs/160F-2019.pdf"
|
||||
|
||||
const ENABLE_XFA = true;
|
||||
const SEARCH_FOR = ""; // try "Mozilla";
|
||||
|
||||
const SANDBOX_BUNDLE_SRC = new URL(
|
||||
"../../node_modules/pdfjs-dist/build/pdf.sandbox.mjs",
|
||||
window.location
|
||||
);
|
||||
|
||||
const container = document.getElementById("viewerContainer");
|
||||
|
||||
const eventBus = new pdfjsViewer.EventBus();
|
||||
|
||||
// (Optionally) enable hyperlinks within PDF files.
|
||||
const pdfLinkService = new pdfjsViewer.PDFLinkService({
|
||||
eventBus,
|
||||
});
|
||||
|
||||
// (Optionally) enable find controller.
|
||||
const pdfFindController = new pdfjsViewer.PDFFindController({
|
||||
eventBus,
|
||||
linkService: pdfLinkService,
|
||||
});
|
||||
|
||||
// (Optionally) enable scripting support.
|
||||
const pdfScriptingManager = new pdfjsViewer.PDFScriptingManager({
|
||||
eventBus,
|
||||
sandboxBundleSrc: SANDBOX_BUNDLE_SRC,
|
||||
});
|
||||
|
||||
const pdfViewer = new pdfjsViewer.PDFViewer({
|
||||
container,
|
||||
eventBus,
|
||||
linkService: pdfLinkService,
|
||||
findController: pdfFindController,
|
||||
scriptingManager: pdfScriptingManager,
|
||||
});
|
||||
pdfLinkService.setViewer(pdfViewer);
|
||||
pdfScriptingManager.setViewer(pdfViewer);
|
||||
|
||||
eventBus.on("pagesinit", function () {
|
||||
// We can use pdfViewer now, e.g. let's change default scale.
|
||||
pdfViewer.currentScaleValue = "page-width";
|
||||
|
||||
// We can try searching for things.
|
||||
if (SEARCH_FOR) {
|
||||
eventBus.dispatch("find", { type: "", query: SEARCH_FOR });
|
||||
}
|
||||
});
|
||||
|
||||
// Loading document.
|
||||
const loadingTask = pdfjsLib.getDocument({
|
||||
url: DEFAULT_URL,
|
||||
cMapUrl: CMAP_URL,
|
||||
cMapPacked: CMAP_PACKED,
|
||||
enableXfa: ENABLE_XFA,
|
||||
});
|
||||
|
||||
const pdfDocument = await loadingTask.promise;
|
||||
// Document loaded, specifying document for the viewer and
|
||||
// the (optional) linkService.
|
||||
pdfViewer.setDocument(pdfDocument);
|
||||
|
||||
pdfLinkService.setDocument(pdfDocument, null);
|
||||
51
examples/components/singlepageviewer.html
Normal file
@@ -0,0 +1,51 @@
|
||||
<!DOCTYPE html>
|
||||
<!--
|
||||
Copyright 2014 Mozilla Foundation
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<html dir="ltr" mozdisallowselectionprint>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
|
||||
<meta name="google" content="notranslate">
|
||||
<title>PDF.js Single Page Viewer using built components</title>
|
||||
|
||||
<style>
|
||||
body {
|
||||
background-color: #808080;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
#viewerContainer {
|
||||
overflow: auto;
|
||||
position: absolute;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
</style>
|
||||
|
||||
<link rel="stylesheet" href="../../node_modules/pdfjs-dist/web/pdf_viewer.css">
|
||||
|
||||
<script src="../../node_modules/pdfjs-dist/build/pdf.mjs" type="module"></script>
|
||||
<script src="../../node_modules/pdfjs-dist/web/pdf_viewer.mjs" type="module"></script>
|
||||
</head>
|
||||
|
||||
<body tabindex="1">
|
||||
<div id="viewerContainer">
|
||||
<div id="viewer" class="pdfViewer"></div>
|
||||
</div>
|
||||
|
||||
<script src="singlepageviewer.mjs" type="module"></script>
|
||||
</body>
|
||||
</html>
|
||||
97
examples/components/singlepageviewer.mjs
Normal file
@@ -0,0 +1,97 @@
|
||||
/* Copyright 2014 Mozilla Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
if (!pdfjsLib.getDocument || !pdfjsViewer.PDFSinglePageViewer) {
|
||||
// eslint-disable-next-line no-alert
|
||||
alert("Please build the pdfjs-dist library using\n `gulp dist-install`");
|
||||
}
|
||||
|
||||
// The workerSrc property shall be specified.
|
||||
//
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc =
|
||||
"../../node_modules/pdfjs-dist/build/pdf.worker.mjs";
|
||||
|
||||
// Some PDFs need external cmaps.
|
||||
//
|
||||
const CMAP_URL = "../../node_modules/pdfjs-dist/cmaps/";
|
||||
const CMAP_PACKED = true;
|
||||
|
||||
const DEFAULT_URL = "../../web/compressed.tracemonkey-pldi-09.pdf";
|
||||
// To test the AcroForm and/or scripting functionality, try e.g. this file:
|
||||
// "../../test/pdfs/160F-2019.pdf"
|
||||
|
||||
const ENABLE_XFA = true;
|
||||
const SEARCH_FOR = ""; // try "Mozilla";
|
||||
|
||||
const SANDBOX_BUNDLE_SRC = new URL(
|
||||
"../../node_modules/pdfjs-dist/build/pdf.sandbox.mjs",
|
||||
window.location
|
||||
);
|
||||
|
||||
const container = document.getElementById("viewerContainer");
|
||||
|
||||
const eventBus = new pdfjsViewer.EventBus();
|
||||
|
||||
// (Optionally) enable hyperlinks within PDF files.
|
||||
const pdfLinkService = new pdfjsViewer.PDFLinkService({
|
||||
eventBus,
|
||||
});
|
||||
|
||||
// (Optionally) enable find controller.
|
||||
const pdfFindController = new pdfjsViewer.PDFFindController({
|
||||
eventBus,
|
||||
linkService: pdfLinkService,
|
||||
});
|
||||
|
||||
// (Optionally) enable scripting support.
|
||||
const pdfScriptingManager = new pdfjsViewer.PDFScriptingManager({
|
||||
eventBus,
|
||||
sandboxBundleSrc: SANDBOX_BUNDLE_SRC,
|
||||
});
|
||||
|
||||
const pdfSinglePageViewer = new pdfjsViewer.PDFSinglePageViewer({
|
||||
container,
|
||||
eventBus,
|
||||
linkService: pdfLinkService,
|
||||
findController: pdfFindController,
|
||||
scriptingManager: pdfScriptingManager,
|
||||
});
|
||||
pdfLinkService.setViewer(pdfSinglePageViewer);
|
||||
pdfScriptingManager.setViewer(pdfSinglePageViewer);
|
||||
|
||||
eventBus.on("pagesinit", function () {
|
||||
// We can use pdfSinglePageViewer now, e.g. let's change default scale.
|
||||
pdfSinglePageViewer.currentScaleValue = "page-width";
|
||||
|
||||
// We can try searching for things.
|
||||
if (SEARCH_FOR) {
|
||||
eventBus.dispatch("find", { type: "", query: SEARCH_FOR });
|
||||
}
|
||||
});
|
||||
|
||||
// Loading document.
|
||||
const loadingTask = pdfjsLib.getDocument({
|
||||
url: DEFAULT_URL,
|
||||
cMapUrl: CMAP_URL,
|
||||
cMapPacked: CMAP_PACKED,
|
||||
enableXfa: ENABLE_XFA,
|
||||
});
|
||||
|
||||
const pdfDocument = await loadingTask.promise;
|
||||
// Document loaded, specifying document for the viewer and
|
||||
// the (optional) linkService.
|
||||
pdfSinglePageViewer.setDocument(pdfDocument);
|
||||
|
||||
pdfLinkService.setDocument(pdfDocument, null);
|
||||
BIN
examples/image_decoders/fish.jpg
Normal file
|
After Width: | Height: | Size: 22 KiB |
40
examples/image_decoders/jpeg_viewer.html
Normal file
@@ -0,0 +1,40 @@
|
||||
<!DOCTYPE html>
|
||||
<!--
|
||||
Copyright 2018 Mozilla Foundation
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<html dir="ltr" mozdisallowselectionprint>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
|
||||
<meta name="google" content="notranslate">
|
||||
<title>PDF.js standalone JpegImage parser</title>
|
||||
|
||||
<style>
|
||||
body {
|
||||
background-color: #808080;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
</style>
|
||||
|
||||
<script src="../../node_modules/pdfjs-dist/image_decoders/pdf.image_decoders.mjs" type="module"></script>
|
||||
</head>
|
||||
|
||||
<body tabindex="1">
|
||||
<canvas id="jpegCanvas" width="0" height="0"></canvas>
|
||||
|
||||
<script src="jpeg_viewer.mjs" type="module"></script>
|
||||
</body>
|
||||
</html>
|
||||
59
examples/image_decoders/jpeg_viewer.mjs
Normal file
@@ -0,0 +1,59 @@
|
||||
/* Copyright 2018 Mozilla Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
if (!pdfjsImageDecoders.JpegImage) {
|
||||
// eslint-disable-next-line no-alert
|
||||
alert("Please build the pdfjs-dist library using `gulp dist-install`");
|
||||
}
|
||||
|
||||
const JPEG_IMAGE = "fish.jpg";
|
||||
|
||||
const jpegCanvas = document.getElementById("jpegCanvas");
|
||||
const jpegCtx = jpegCanvas.getContext("2d");
|
||||
|
||||
// Load the image data, and convert it to a Uint8Array.
|
||||
//
|
||||
const response = await fetch(JPEG_IMAGE);
|
||||
if (!response.ok) {
|
||||
throw new Error(response.statusText);
|
||||
}
|
||||
const typedArrayImage = new Uint8Array(await response.arrayBuffer());
|
||||
|
||||
// Parse the image data using `JpegImage`.
|
||||
//
|
||||
const jpegImage = new pdfjsImageDecoders.JpegImage();
|
||||
jpegImage.parse(typedArrayImage);
|
||||
|
||||
const width = jpegImage.width,
|
||||
height = jpegImage.height;
|
||||
const jpegData = jpegImage.getData({
|
||||
width,
|
||||
height,
|
||||
forceRGB: true,
|
||||
});
|
||||
|
||||
// Render the JPEG image on a <canvas>.
|
||||
//
|
||||
const imageData = jpegCtx.createImageData(width, height);
|
||||
const imageBytes = imageData.data;
|
||||
for (let j = 0, k = 0, jj = width * height * 4; j < jj; ) {
|
||||
imageBytes[j++] = jpegData[k++];
|
||||
imageBytes[j++] = jpegData[k++];
|
||||
imageBytes[j++] = jpegData[k++];
|
||||
imageBytes[j++] = 255;
|
||||
}
|
||||
jpegCanvas.width = width;
|
||||
jpegCanvas.height = height;
|
||||
jpegCtx.putImageData(imageData, 0, 0);
|
||||
76
examples/learning/helloworld.html
Normal file
@@ -0,0 +1,76 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>'Hello, world!' example</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>'Hello, world!' example</h1>
|
||||
|
||||
<canvas id="the-canvas" style="border: 1px solid black; direction: ltr;"></canvas>
|
||||
|
||||
<script src="../../node_modules/pdfjs-dist/build/pdf.mjs" type="module"></script>
|
||||
|
||||
<script id="script" type="module">
|
||||
//
|
||||
// If absolute URL from the remote server is provided, configure the CORS
|
||||
// header on that server.
|
||||
//
|
||||
const url = './helloworld.pdf';
|
||||
|
||||
//
|
||||
// The workerSrc property shall be specified.
|
||||
//
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc =
|
||||
'../../node_modules/pdfjs-dist/build/pdf.worker.mjs';
|
||||
|
||||
//
|
||||
// Asynchronous download PDF
|
||||
//
|
||||
const loadingTask = pdfjsLib.getDocument(url);
|
||||
const pdf = await loadingTask.promise;
|
||||
//
|
||||
// Fetch the first page
|
||||
//
|
||||
const page = await pdf.getPage(1);
|
||||
const scale = 1.5;
|
||||
const viewport = page.getViewport({ scale });
|
||||
// Support HiDPI-screens.
|
||||
const outputScale = window.devicePixelRatio || 1;
|
||||
|
||||
//
|
||||
// Prepare canvas using PDF page dimensions
|
||||
//
|
||||
const canvas = document.getElementById("the-canvas");
|
||||
const context = canvas.getContext("2d");
|
||||
|
||||
canvas.width = Math.floor(viewport.width * outputScale);
|
||||
canvas.height = Math.floor(viewport.height * outputScale);
|
||||
canvas.style.width = Math.floor(viewport.width) + "px";
|
||||
canvas.style.height = Math.floor(viewport.height) + "px";
|
||||
|
||||
const transform = outputScale !== 1
|
||||
? [outputScale, 0, 0, outputScale, 0, 0]
|
||||
: null;
|
||||
|
||||
//
|
||||
// Render PDF page into canvas context
|
||||
//
|
||||
const renderContext = {
|
||||
canvasContext: context,
|
||||
transform,
|
||||
viewport,
|
||||
};
|
||||
page.render(renderContext);
|
||||
</script>
|
||||
|
||||
<hr>
|
||||
<h2>JavaScript code:</h2>
|
||||
<pre id="code"></pre>
|
||||
<script>
|
||||
document.getElementById('code').textContent =
|
||||
document.getElementById('script').text;
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
68
examples/learning/helloworld.pdf
Normal file
@@ -0,0 +1,68 @@
|
||||
%PDF-1.7
|
||||
|
||||
1 0 obj % entry point
|
||||
<<
|
||||
/Type /Catalog
|
||||
/Pages 2 0 R
|
||||
>>
|
||||
endobj
|
||||
|
||||
2 0 obj
|
||||
<<
|
||||
/Type /Pages
|
||||
/MediaBox [ 0 0 200 200 ]
|
||||
/Count 1
|
||||
/Kids [ 3 0 R ]
|
||||
>>
|
||||
endobj
|
||||
|
||||
3 0 obj
|
||||
<<
|
||||
/Type /Page
|
||||
/Parent 2 0 R
|
||||
/Resources <<
|
||||
/Font <<
|
||||
/F1 4 0 R
|
||||
>>
|
||||
>>
|
||||
/Contents 5 0 R
|
||||
>>
|
||||
endobj
|
||||
|
||||
4 0 obj
|
||||
<<
|
||||
/Type /Font
|
||||
/Subtype /Type1
|
||||
/BaseFont /Times-Roman
|
||||
>>
|
||||
endobj
|
||||
|
||||
5 0 obj % page content
|
||||
<<
|
||||
/Length 44
|
||||
>>
|
||||
stream
|
||||
BT
|
||||
70 50 TD
|
||||
/F1 12 Tf
|
||||
(Hello, world!) Tj
|
||||
ET
|
||||
endstream
|
||||
endobj
|
||||
|
||||
xref
|
||||
0 6
|
||||
0000000000 65535 f
|
||||
0000000010 00000 n
|
||||
0000000079 00000 n
|
||||
0000000173 00000 n
|
||||
0000000301 00000 n
|
||||
0000000380 00000 n
|
||||
trailer
|
||||
<<
|
||||
/Size 6
|
||||
/Root 1 0 R
|
||||
>>
|
||||
startxref
|
||||
492
|
||||
%%EOF
|
||||
81
examples/learning/helloworld64.html
Normal file
@@ -0,0 +1,81 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>'Hello, world!' base64 example</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>'Hello, world!' example</h1>
|
||||
|
||||
<canvas id="the-canvas" style="border: 1px solid black; direction: ltr;"></canvas>
|
||||
|
||||
<script src="../../node_modules/pdfjs-dist/build/pdf.mjs" type="module"></script>
|
||||
|
||||
<script id="script" type="module">
|
||||
// atob() is used to convert base64 encoded PDF to binary-like data.
|
||||
// (See also https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/
|
||||
// Base64_encoding_and_decoding.)
|
||||
var pdfData = atob(
|
||||
'JVBERi0xLjcKCjEgMCBvYmogICUgZW50cnkgcG9pbnQKPDwKICAvVHlwZSAvQ2F0YWxvZwog' +
|
||||
'IC9QYWdlcyAyIDAgUgo+PgplbmRvYmoKCjIgMCBvYmoKPDwKICAvVHlwZSAvUGFnZXMKICAv' +
|
||||
'TWVkaWFCb3ggWyAwIDAgMjAwIDIwMCBdCiAgL0NvdW50IDEKICAvS2lkcyBbIDMgMCBSIF0K' +
|
||||
'Pj4KZW5kb2JqCgozIDAgb2JqCjw8CiAgL1R5cGUgL1BhZ2UKICAvUGFyZW50IDIgMCBSCiAg' +
|
||||
'L1Jlc291cmNlcyA8PAogICAgL0ZvbnQgPDwKICAgICAgL0YxIDQgMCBSIAogICAgPj4KICA+' +
|
||||
'PgogIC9Db250ZW50cyA1IDAgUgo+PgplbmRvYmoKCjQgMCBvYmoKPDwKICAvVHlwZSAvRm9u' +
|
||||
'dAogIC9TdWJ0eXBlIC9UeXBlMQogIC9CYXNlRm9udCAvVGltZXMtUm9tYW4KPj4KZW5kb2Jq' +
|
||||
'Cgo1IDAgb2JqICAlIHBhZ2UgY29udGVudAo8PAogIC9MZW5ndGggNDQKPj4Kc3RyZWFtCkJU' +
|
||||
'CjcwIDUwIFRECi9GMSAxMiBUZgooSGVsbG8sIHdvcmxkISkgVGoKRVQKZW5kc3RyZWFtCmVu' +
|
||||
'ZG9iagoKeHJlZgowIDYKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDEwIDAwMDAwIG4g' +
|
||||
'CjAwMDAwMDAwNzkgMDAwMDAgbiAKMDAwMDAwMDE3MyAwMDAwMCBuIAowMDAwMDAwMzAxIDAw' +
|
||||
'MDAwIG4gCjAwMDAwMDAzODAgMDAwMDAgbiAKdHJhaWxlcgo8PAogIC9TaXplIDYKICAvUm9v' +
|
||||
'dCAxIDAgUgo+PgpzdGFydHhyZWYKNDkyCiUlRU9G');
|
||||
|
||||
//
|
||||
// The workerSrc property shall be specified.
|
||||
//
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc =
|
||||
'../../node_modules/pdfjs-dist/build/pdf.worker.mjs';
|
||||
|
||||
// Opening PDF by passing its binary data as a string. It is still preferable
|
||||
// to use Uint8Array, but string or array-like structure will work too.
|
||||
var loadingTask = pdfjsLib.getDocument({ data: pdfData, });
|
||||
var pdf = await loadingTask.promise;
|
||||
// Fetch the first page.
|
||||
var page = await pdf.getPage(1);
|
||||
var scale = 1.5;
|
||||
var viewport = page.getViewport({ scale: scale, });
|
||||
// Support HiDPI-screens.
|
||||
var outputScale = window.devicePixelRatio || 1;
|
||||
|
||||
// Prepare canvas using PDF page dimensions.
|
||||
var canvas = document.getElementById('the-canvas');
|
||||
var context = canvas.getContext('2d');
|
||||
|
||||
canvas.width = Math.floor(viewport.width * outputScale);
|
||||
canvas.height = Math.floor(viewport.height * outputScale);
|
||||
canvas.style.width = Math.floor(viewport.width) + "px";
|
||||
canvas.style.height = Math.floor(viewport.height) + "px";
|
||||
|
||||
var transform = outputScale !== 1
|
||||
? [outputScale, 0, 0, outputScale, 0, 0]
|
||||
: null;
|
||||
|
||||
// Render PDF page into canvas context.
|
||||
var renderContext = {
|
||||
canvasContext: context,
|
||||
transform,
|
||||
viewport,
|
||||
};
|
||||
page.render(renderContext);
|
||||
</script>
|
||||
|
||||
<hr>
|
||||
<h2>JavaScript code:</h2>
|
||||
<pre id="code"></pre>
|
||||
<script>
|
||||
document.getElementById('code').textContent =
|
||||
document.getElementById('script').text;
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
139
examples/learning/prevnext.html
Normal file
@@ -0,0 +1,139 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Previous/Next example</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>'Previous/Next' example</h1>
|
||||
|
||||
<div>
|
||||
<button id="prev" type="button">Previous</button>
|
||||
<button id="next" type="button">Next</button>
|
||||
|
||||
<span>Page: <span id="page_num"></span> / <span id="page_count"></span></span>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<canvas id="the-canvas" style="border: 1px solid black; direction: ltr;"></canvas>
|
||||
</div>
|
||||
|
||||
<script src="../../node_modules/pdfjs-dist/build/pdf.mjs" type="module"></script>
|
||||
|
||||
<script id="script" type="module">
|
||||
//
|
||||
// If absolute URL from the remote server is provided, configure the CORS
|
||||
// header on that server.
|
||||
//
|
||||
var url = '../../web/compressed.tracemonkey-pldi-09.pdf';
|
||||
|
||||
//
|
||||
// In cases when the pdf.worker.js is located at the different folder than the
|
||||
// PDF.js's one, or the PDF.js is executed via eval(), the workerSrc property
|
||||
// shall be specified.
|
||||
//
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc =
|
||||
'../../node_modules/pdfjs-dist/build/pdf.worker.mjs';
|
||||
|
||||
var pdfDoc = null,
|
||||
pageNum = 1,
|
||||
pageRendering = false,
|
||||
pageNumPending = null,
|
||||
scale = 0.8,
|
||||
canvas = document.getElementById('the-canvas'),
|
||||
ctx = canvas.getContext('2d');
|
||||
|
||||
/**
|
||||
* Get page info from document, resize canvas accordingly, and render page.
|
||||
* @param num Page number.
|
||||
*/
|
||||
function renderPage(num) {
|
||||
pageRendering = true;
|
||||
// Using promise to fetch the page
|
||||
pdfDoc.getPage(num).then(function(page) {
|
||||
var viewport = page.getViewport({ scale: scale, });
|
||||
// Support HiDPI-screens.
|
||||
var outputScale = window.devicePixelRatio || 1;
|
||||
|
||||
canvas.width = Math.floor(viewport.width * outputScale);
|
||||
canvas.height = Math.floor(viewport.height * outputScale);
|
||||
canvas.style.width = Math.floor(viewport.width) + "px";
|
||||
canvas.style.height = Math.floor(viewport.height) + "px";
|
||||
|
||||
var transform = outputScale !== 1
|
||||
? [outputScale, 0, 0, outputScale, 0, 0]
|
||||
: null;
|
||||
|
||||
// Render PDF page into canvas context
|
||||
var renderContext = {
|
||||
canvasContext: ctx,
|
||||
transform: transform,
|
||||
viewport: viewport,
|
||||
};
|
||||
var renderTask = page.render(renderContext);
|
||||
|
||||
// Wait for rendering to finish
|
||||
renderTask.promise.then(function () {
|
||||
pageRendering = false;
|
||||
if (pageNumPending !== null) {
|
||||
// New page rendering is pending
|
||||
renderPage(pageNumPending);
|
||||
pageNumPending = null;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Update page counters
|
||||
document.getElementById('page_num').textContent = num;
|
||||
}
|
||||
|
||||
/**
|
||||
* If another page rendering in progress, waits until the rendering is
|
||||
* finished. Otherwise, executes rendering immediately.
|
||||
*/
|
||||
function queueRenderPage(num) {
|
||||
if (pageRendering) {
|
||||
pageNumPending = num;
|
||||
} else {
|
||||
renderPage(num);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays previous page.
|
||||
*/
|
||||
function onPrevPage() {
|
||||
if (pageNum <= 1) {
|
||||
return;
|
||||
}
|
||||
pageNum--;
|
||||
queueRenderPage(pageNum);
|
||||
}
|
||||
document.getElementById('prev').addEventListener('click', onPrevPage);
|
||||
|
||||
/**
|
||||
* Displays next page.
|
||||
*/
|
||||
function onNextPage() {
|
||||
if (pageNum >= pdfDoc.numPages) {
|
||||
return;
|
||||
}
|
||||
pageNum++;
|
||||
queueRenderPage(pageNum);
|
||||
}
|
||||
document.getElementById('next').addEventListener('click', onNextPage);
|
||||
|
||||
/**
|
||||
* Asynchronously downloads PDF.
|
||||
*/
|
||||
var loadingTask = pdfjsLib.getDocument(url);
|
||||
pdfDoc = await loadingTask.promise;
|
||||
document.getElementById('page_count').textContent = pdfDoc.numPages;
|
||||
|
||||
// Initial/first page rendering
|
||||
renderPage(pageNum);
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
11
examples/mobile-viewer/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
## Overview
|
||||
|
||||
Example to demonstrate PDF.js library usage with a viewer optimized for mobile usage.
|
||||
|
||||
## Getting started
|
||||
|
||||
Build PDF.js using `gulp dist-install` and run `gulp server` to start a web server.
|
||||
You can then work with the mobile viewer at
|
||||
http://localhost:8888/examples/mobile-viewer/viewer.html.
|
||||
|
||||
Refer to `viewer.js` for the source code of the mobile viewer.
|
||||
BIN
examples/mobile-viewer/images/div_line_left.png
Normal file
|
After Width: | Height: | Size: 169 B |
BIN
examples/mobile-viewer/images/div_line_left@1.5x.png
Normal file
|
After Width: | Height: | Size: 185 B |
BIN
examples/mobile-viewer/images/div_line_left@2x.png
Normal file
|
After Width: | Height: | Size: 295 B |
BIN
examples/mobile-viewer/images/div_line_right.png
Normal file
|
After Width: | Height: | Size: 166 B |
BIN
examples/mobile-viewer/images/div_line_right@1.5x.png
Normal file
|
After Width: | Height: | Size: 184 B |
BIN
examples/mobile-viewer/images/div_line_right@2x.png
Normal file
|
After Width: | Height: | Size: 295 B |
BIN
examples/mobile-viewer/images/document_bg.png
Normal file
|
After Width: | Height: | Size: 560 B |
BIN
examples/mobile-viewer/images/icon_next_page.png
Normal file
|
After Width: | Height: | Size: 310 B |
BIN
examples/mobile-viewer/images/icon_next_page@1.5x.png
Normal file
|
After Width: | Height: | Size: 338 B |
BIN
examples/mobile-viewer/images/icon_previous_page.png
Normal file
|
After Width: | Height: | Size: 372 B |
BIN
examples/mobile-viewer/images/icon_previous_page@1.5x.png
Normal file
|
After Width: | Height: | Size: 395 B |
BIN
examples/mobile-viewer/images/icon_zoom_in.png
Normal file
|
After Width: | Height: | Size: 640 B |
BIN
examples/mobile-viewer/images/icon_zoom_in@1.5x.png
Normal file
|
After Width: | Height: | Size: 1.1 KiB |
BIN
examples/mobile-viewer/images/icon_zoom_out.png
Normal file
|
After Width: | Height: | Size: 564 B |
BIN
examples/mobile-viewer/images/icon_zoom_out@1.5x.png
Normal file
|
After Width: | Height: | Size: 1.0 KiB |
BIN
examples/mobile-viewer/images/spinner.png
Normal file
|
After Width: | Height: | Size: 2.7 KiB |
BIN
examples/mobile-viewer/images/toolbar_background.png
Normal file
|
After Width: | Height: | Size: 4.5 KiB |
242
examples/mobile-viewer/viewer.css
Normal file
@@ -0,0 +1,242 @@
|
||||
/* Copyright 2016 Mozilla Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
* {
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
html {
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
overflow: hidden;
|
||||
font-size: 10px;
|
||||
}
|
||||
|
||||
header {
|
||||
background-color: rgb(244 244 244 / 1);
|
||||
}
|
||||
|
||||
header h1 {
|
||||
border-bottom: 1px solid rgb(216 216 216 / 1);
|
||||
color: rgb(133 133 133 / 1);
|
||||
font-size: 23px;
|
||||
font-style: italic;
|
||||
font-weight: normal;
|
||||
overflow: hidden;
|
||||
padding: 10px;
|
||||
text-align: center;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
body {
|
||||
background: url(images/document_bg.png);
|
||||
color: rgb(255 255 255 / 1);
|
||||
font-family: sans-serif;
|
||||
font-size: 10px;
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
overflow: hidden;
|
||||
padding-bottom: 5rem;
|
||||
}
|
||||
|
||||
section {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
overflow: hidden;
|
||||
font-size: 2rem;
|
||||
}
|
||||
|
||||
footer {
|
||||
background-image: url(images/toolbar_background.png);
|
||||
height: 4rem;
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
z-index: 1;
|
||||
box-shadow: 0 -0.2rem 0.5rem rgb(50 50 50 / 0.75);
|
||||
}
|
||||
|
||||
.toolbarButton {
|
||||
display: block;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
border-width: 0;
|
||||
background-position: center center;
|
||||
background-repeat: no-repeat;
|
||||
background-color: rgb(0 0 0 / 0);
|
||||
}
|
||||
|
||||
.toolbarButton.pageUp {
|
||||
position: absolute;
|
||||
width: 18%;
|
||||
height: 100%;
|
||||
left: 0;
|
||||
background-image: url(images/icon_previous_page.png);
|
||||
background-size: 2rem;
|
||||
}
|
||||
|
||||
.toolbarButton.pageDown {
|
||||
position: absolute;
|
||||
width: 18%;
|
||||
height: 100%;
|
||||
left: 18%;
|
||||
background-image: url(images/icon_next_page.png);
|
||||
background-size: 2rem;
|
||||
}
|
||||
|
||||
#pageNumber {
|
||||
-moz-appearance: textfield; /* hides the spinner in moz */
|
||||
position: absolute;
|
||||
width: 28%;
|
||||
height: 100%;
|
||||
left: 36%;
|
||||
text-align: center;
|
||||
border: 0;
|
||||
background-color: rgb(0 0 0 / 0);
|
||||
font-size: 1.2rem;
|
||||
color: rgb(255 255 255 / 1);
|
||||
background-image:
|
||||
url(images/div_line_left.png), url(images/div_line_right.png);
|
||||
background-repeat: no-repeat;
|
||||
background-position: left, right;
|
||||
background-size: 0.2rem, 0.2rem;
|
||||
}
|
||||
|
||||
.toolbarButton.zoomOut {
|
||||
position: absolute;
|
||||
width: 18%;
|
||||
height: 100%;
|
||||
left: 64%;
|
||||
background-image: url(images/icon_zoom_out.png);
|
||||
background-size: 2.4rem;
|
||||
}
|
||||
|
||||
.toolbarButton.zoomIn {
|
||||
position: absolute;
|
||||
width: 18%;
|
||||
height: 100%;
|
||||
left: 82%;
|
||||
background-image: url(images/icon_zoom_in.png);
|
||||
background-size: 2.4rem;
|
||||
}
|
||||
|
||||
.toolbarButton[disabled] {
|
||||
opacity: 0.3;
|
||||
}
|
||||
|
||||
.hidden {
|
||||
display: none;
|
||||
}
|
||||
[hidden] {
|
||||
display: none !important;
|
||||
}
|
||||
|
||||
#viewerContainer {
|
||||
position: absolute;
|
||||
overflow: auto;
|
||||
width: 100%;
|
||||
inset: 5rem 0 4rem;
|
||||
}
|
||||
|
||||
canvas {
|
||||
margin: auto;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.pdfViewer .page .loadingIcon {
|
||||
width: 2.9rem;
|
||||
height: 2.9rem;
|
||||
background: url("images/spinner.png") no-repeat left top / 38rem;
|
||||
border: medium none;
|
||||
animation: 1s steps(10, end) 0s normal none infinite moveDefault;
|
||||
display: block;
|
||||
position: absolute;
|
||||
top: calc((100% - 2.9rem) / 2);
|
||||
left: calc((100% - 2.9rem) / 2);
|
||||
}
|
||||
|
||||
@keyframes moveDefault {
|
||||
from {
|
||||
background-position: 0 top;
|
||||
}
|
||||
|
||||
to {
|
||||
background-position: -39rem top;
|
||||
}
|
||||
}
|
||||
|
||||
#loadingBar {
|
||||
/* Define this variable here, and not in :root, to avoid reflowing the
|
||||
entire viewer when updating progress (see issue 15958). */
|
||||
--progressBar-percent: 0%;
|
||||
|
||||
position: relative;
|
||||
height: 0.6rem;
|
||||
background-color: rgb(51 51 51 / 1);
|
||||
border-bottom: 1px solid rgb(51 51 51 / 1);
|
||||
}
|
||||
|
||||
#loadingBar .progress {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
transform: scaleX(var(--progressBar-percent));
|
||||
transform-origin: 0 0;
|
||||
height: 100%;
|
||||
background-color: rgb(221 221 221 / 1);
|
||||
overflow: hidden;
|
||||
transition: transform 200ms;
|
||||
}
|
||||
|
||||
@keyframes progressIndeterminate {
|
||||
0% {
|
||||
transform: translateX(0%);
|
||||
}
|
||||
50% {
|
||||
transform: translateX(100%);
|
||||
}
|
||||
100% {
|
||||
transform: translateX(100%);
|
||||
}
|
||||
}
|
||||
|
||||
#loadingBar.indeterminate .progress {
|
||||
transform: none;
|
||||
background-color: rgb(153 153 153 / 1);
|
||||
transition: none;
|
||||
}
|
||||
|
||||
#loadingBar.indeterminate .progress .glimmer {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
height: 100%;
|
||||
width: 5rem;
|
||||
background-image: linear-gradient(
|
||||
to right,
|
||||
rgb(153 153 153 / 1) 0%,
|
||||
rgb(255 255 255 / 1) 50%,
|
||||
rgb(153 153 153 / 1) 100%
|
||||
);
|
||||
background-size: 100% 100%;
|
||||
background-repeat: no-repeat;
|
||||
animation: progressIndeterminate 2s linear infinite;
|
||||
}
|
||||
57
examples/mobile-viewer/viewer.html
Normal file
@@ -0,0 +1,57 @@
|
||||
<!DOCTYPE html>
|
||||
<!--
|
||||
Copyright 2016 Mozilla Foundation
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<html dir="ltr">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
|
||||
|
||||
<title>PDF.js viewer</title>
|
||||
|
||||
<link rel="stylesheet" href="../../node_modules/pdfjs-dist/web/pdf_viewer.css">
|
||||
<link rel="stylesheet" type="text/css" href="viewer.css">
|
||||
|
||||
<script src="../../node_modules/pdfjs-dist/build/pdf.mjs" type="module"></script>
|
||||
<script src="../../node_modules/pdfjs-dist/web/pdf_viewer.mjs" type="module"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<h1 id="title"></h1>
|
||||
</header>
|
||||
|
||||
<div id="viewerContainer">
|
||||
<div id="viewer" class="pdfViewer"></div>
|
||||
</div>
|
||||
|
||||
<div id="loadingBar">
|
||||
<div class="progress"></div>
|
||||
<div class="glimmer"></div>
|
||||
</div>
|
||||
|
||||
<footer>
|
||||
<button class="toolbarButton pageUp" title="Previous Page" id="previous" type="button"></button>
|
||||
<button class="toolbarButton pageDown" title="Next Page" id="next" type="button"></button>
|
||||
|
||||
<input type="number" id="pageNumber" class="toolbarField pageNumber" value="1" size="4" min="1">
|
||||
|
||||
<button class="toolbarButton zoomOut" title="Zoom Out" id="zoomOut" type="button"></button>
|
||||
<button class="toolbarButton zoomIn" title="Zoom In" id="zoomIn" type="button"></button>
|
||||
</footer>
|
||||
|
||||
<script src="viewer.mjs" type="module"></script>
|
||||
</body>
|
||||
</html>
|
||||
371
examples/mobile-viewer/viewer.mjs
Normal file
@@ -0,0 +1,371 @@
|
||||
/* Copyright 2016 Mozilla Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
if (!pdfjsLib.getDocument || !pdfjsViewer.PDFViewer) {
|
||||
// eslint-disable-next-line no-alert
|
||||
alert("Please build the pdfjs-dist library using\n `gulp dist-install`");
|
||||
}
|
||||
|
||||
const MAX_CANVAS_PIXELS = 0; // CSS-only zooming.
|
||||
const TEXT_LAYER_MODE = 0; // DISABLE
|
||||
const MAX_IMAGE_SIZE = 1024 * 1024;
|
||||
const CMAP_URL = "../../node_modules/pdfjs-dist/cmaps/";
|
||||
const CMAP_PACKED = true;
|
||||
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc =
|
||||
"../../node_modules/pdfjs-dist/build/pdf.worker.mjs";
|
||||
|
||||
const DEFAULT_URL = "../../web/compressed.tracemonkey-pldi-09.pdf";
|
||||
const DEFAULT_SCALE_DELTA = 1.1;
|
||||
const MIN_SCALE = 0.25;
|
||||
const MAX_SCALE = 10.0;
|
||||
const DEFAULT_SCALE_VALUE = "auto";
|
||||
|
||||
const PDFViewerApplication = {
|
||||
pdfLoadingTask: null,
|
||||
pdfDocument: null,
|
||||
pdfViewer: null,
|
||||
pdfHistory: null,
|
||||
pdfLinkService: null,
|
||||
eventBus: null,
|
||||
|
||||
/**
|
||||
* Opens PDF document specified by URL.
|
||||
* @returns {Promise} - Returns the promise, which is resolved when document
|
||||
* is opened.
|
||||
*/
|
||||
open(params) {
|
||||
if (this.pdfLoadingTask) {
|
||||
// We need to destroy already opened document
|
||||
return this.close().then(
|
||||
function () {
|
||||
// ... and repeat the open() call.
|
||||
return this.open(params);
|
||||
}.bind(this)
|
||||
);
|
||||
}
|
||||
|
||||
const url = params.url;
|
||||
const self = this;
|
||||
this.setTitleUsingUrl(url);
|
||||
|
||||
// Loading document.
|
||||
const loadingTask = pdfjsLib.getDocument({
|
||||
url,
|
||||
maxImageSize: MAX_IMAGE_SIZE,
|
||||
cMapUrl: CMAP_URL,
|
||||
cMapPacked: CMAP_PACKED,
|
||||
});
|
||||
this.pdfLoadingTask = loadingTask;
|
||||
|
||||
loadingTask.onProgress = function (progressData) {
|
||||
self.progress(progressData.loaded / progressData.total);
|
||||
};
|
||||
|
||||
return loadingTask.promise.then(
|
||||
function (pdfDocument) {
|
||||
// Document loaded, specifying document for the viewer.
|
||||
self.pdfDocument = pdfDocument;
|
||||
self.pdfViewer.setDocument(pdfDocument);
|
||||
self.pdfLinkService.setDocument(pdfDocument);
|
||||
self.pdfHistory.initialize({
|
||||
fingerprint: pdfDocument.fingerprints[0],
|
||||
});
|
||||
|
||||
self.loadingBar.hide();
|
||||
self.setTitleUsingMetadata(pdfDocument);
|
||||
},
|
||||
function (reason) {
|
||||
let key = "pdfjs-loading-error";
|
||||
if (reason instanceof pdfjsLib.InvalidPDFException) {
|
||||
key = "pdfjs-invalid-file-error";
|
||||
} else if (reason instanceof pdfjsLib.ResponseException) {
|
||||
key = reason.missing
|
||||
? "pdfjs-missing-file-error"
|
||||
: "pdfjs-unexpected-response-error";
|
||||
}
|
||||
self.l10n.get(key).then(msg => {
|
||||
self.error(msg, { message: reason?.message });
|
||||
});
|
||||
self.loadingBar.hide();
|
||||
}
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* Closes opened PDF document.
|
||||
* @returns {Promise} - Returns the promise, which is resolved when all
|
||||
* destruction is completed.
|
||||
*/
|
||||
close() {
|
||||
if (!this.pdfLoadingTask) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
const promise = this.pdfLoadingTask.destroy();
|
||||
this.pdfLoadingTask = null;
|
||||
|
||||
if (this.pdfDocument) {
|
||||
this.pdfDocument = null;
|
||||
|
||||
this.pdfViewer.setDocument(null);
|
||||
this.pdfLinkService.setDocument(null, null);
|
||||
|
||||
if (this.pdfHistory) {
|
||||
this.pdfHistory.reset();
|
||||
}
|
||||
}
|
||||
|
||||
return promise;
|
||||
},
|
||||
|
||||
get loadingBar() {
|
||||
const bar = document.getElementById("loadingBar");
|
||||
return pdfjsLib.shadow(
|
||||
this,
|
||||
"loadingBar",
|
||||
new pdfjsViewer.ProgressBar(bar)
|
||||
);
|
||||
},
|
||||
|
||||
setTitleUsingUrl: function pdfViewSetTitleUsingUrl(url) {
|
||||
this.url = url;
|
||||
let title = pdfjsLib.getFilenameFromUrl(url) || url;
|
||||
try {
|
||||
title = decodeURIComponent(title);
|
||||
} catch {
|
||||
// decodeURIComponent may throw URIError,
|
||||
// fall back to using the unprocessed url in that case
|
||||
}
|
||||
this.setTitle(title);
|
||||
},
|
||||
|
||||
setTitleUsingMetadata(pdfDocument) {
|
||||
const self = this;
|
||||
pdfDocument.getMetadata().then(function (data) {
|
||||
const info = data.info,
|
||||
metadata = data.metadata;
|
||||
self.documentInfo = info;
|
||||
self.metadata = metadata;
|
||||
|
||||
// Provides some basic debug information
|
||||
console.log(
|
||||
"PDF " +
|
||||
pdfDocument.fingerprints[0] +
|
||||
" [" +
|
||||
info.PDFFormatVersion +
|
||||
" " +
|
||||
(info.Producer || "-").trim() +
|
||||
" / " +
|
||||
(info.Creator || "-").trim() +
|
||||
"]" +
|
||||
" (PDF.js: " +
|
||||
(pdfjsLib.version || "-") +
|
||||
")"
|
||||
);
|
||||
|
||||
let pdfTitle;
|
||||
if (metadata && metadata.has("dc:title")) {
|
||||
const title = metadata.get("dc:title");
|
||||
// Ghostscript sometimes returns 'Untitled', so prevent setting the
|
||||
// title to 'Untitled.
|
||||
if (title !== "Untitled") {
|
||||
pdfTitle = title;
|
||||
}
|
||||
}
|
||||
|
||||
if (!pdfTitle && info && info.Title) {
|
||||
pdfTitle = info.Title;
|
||||
}
|
||||
|
||||
if (pdfTitle) {
|
||||
self.setTitle(pdfTitle + " - " + document.title);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
setTitle: function pdfViewSetTitle(title) {
|
||||
document.title = title;
|
||||
document.getElementById("title").textContent = title;
|
||||
},
|
||||
|
||||
error: function pdfViewError(message, moreInfo) {
|
||||
const moreInfoText = [
|
||||
`PDF.js v${pdfjsLib.version || "?"} (build: ${pdfjsLib.build || "?"})`,
|
||||
];
|
||||
if (moreInfo) {
|
||||
moreInfoText.push(`Message: ${moreInfo.message}`);
|
||||
|
||||
if (moreInfo.stack) {
|
||||
moreInfoText.push(`Stack: ${moreInfo.stack}`);
|
||||
} else {
|
||||
if (moreInfo.filename) {
|
||||
moreInfoText.push(`File: ${moreInfo.filename}`);
|
||||
}
|
||||
if (moreInfo.lineNumber) {
|
||||
moreInfoText.push(`Line: ${moreInfo.lineNumber}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.error(`${message}\n\n${moreInfoText.join("\n")}`);
|
||||
},
|
||||
|
||||
progress: function pdfViewProgress(level) {
|
||||
const percent = Math.round(level * 100);
|
||||
// Updating the bar if value increases.
|
||||
if (percent > this.loadingBar.percent || isNaN(percent)) {
|
||||
this.loadingBar.percent = percent;
|
||||
}
|
||||
},
|
||||
|
||||
get pagesCount() {
|
||||
return this.pdfDocument.numPages;
|
||||
},
|
||||
|
||||
get page() {
|
||||
return this.pdfViewer.currentPageNumber;
|
||||
},
|
||||
|
||||
set page(val) {
|
||||
this.pdfViewer.currentPageNumber = val;
|
||||
},
|
||||
|
||||
zoomIn: function pdfViewZoomIn(ticks) {
|
||||
let newScale = this.pdfViewer.currentScale;
|
||||
do {
|
||||
newScale = (newScale * DEFAULT_SCALE_DELTA).toFixed(2);
|
||||
newScale = Math.ceil(newScale * 10) / 10;
|
||||
newScale = Math.min(MAX_SCALE, newScale);
|
||||
} while (--ticks && newScale < MAX_SCALE);
|
||||
this.pdfViewer.currentScaleValue = newScale;
|
||||
},
|
||||
|
||||
zoomOut: function pdfViewZoomOut(ticks) {
|
||||
let newScale = this.pdfViewer.currentScale;
|
||||
do {
|
||||
newScale = (newScale / DEFAULT_SCALE_DELTA).toFixed(2);
|
||||
newScale = Math.floor(newScale * 10) / 10;
|
||||
newScale = Math.max(MIN_SCALE, newScale);
|
||||
} while (--ticks && newScale > MIN_SCALE);
|
||||
this.pdfViewer.currentScaleValue = newScale;
|
||||
},
|
||||
|
||||
initUI: function pdfViewInitUI() {
|
||||
const eventBus = new pdfjsViewer.EventBus();
|
||||
this.eventBus = eventBus;
|
||||
|
||||
const linkService = new pdfjsViewer.PDFLinkService({
|
||||
eventBus,
|
||||
});
|
||||
this.pdfLinkService = linkService;
|
||||
|
||||
this.l10n = new pdfjsViewer.GenericL10n();
|
||||
|
||||
const container = document.getElementById("viewerContainer");
|
||||
const pdfViewer = new pdfjsViewer.PDFViewer({
|
||||
container,
|
||||
eventBus,
|
||||
linkService,
|
||||
l10n: this.l10n,
|
||||
maxCanvasPixels: MAX_CANVAS_PIXELS,
|
||||
textLayerMode: TEXT_LAYER_MODE,
|
||||
});
|
||||
this.pdfViewer = pdfViewer;
|
||||
linkService.setViewer(pdfViewer);
|
||||
|
||||
this.pdfHistory = new pdfjsViewer.PDFHistory({
|
||||
eventBus,
|
||||
linkService,
|
||||
});
|
||||
linkService.setHistory(this.pdfHistory);
|
||||
|
||||
document.getElementById("previous").addEventListener("click", function () {
|
||||
PDFViewerApplication.page--;
|
||||
});
|
||||
|
||||
document.getElementById("next").addEventListener("click", function () {
|
||||
PDFViewerApplication.page++;
|
||||
});
|
||||
|
||||
document.getElementById("zoomIn").addEventListener("click", function () {
|
||||
PDFViewerApplication.zoomIn();
|
||||
});
|
||||
|
||||
document.getElementById("zoomOut").addEventListener("click", function () {
|
||||
PDFViewerApplication.zoomOut();
|
||||
});
|
||||
|
||||
document
|
||||
.getElementById("pageNumber")
|
||||
.addEventListener("click", function () {
|
||||
this.select();
|
||||
});
|
||||
|
||||
document
|
||||
.getElementById("pageNumber")
|
||||
.addEventListener("change", function () {
|
||||
PDFViewerApplication.page = this.value | 0;
|
||||
|
||||
// Ensure that the page number input displays the correct value,
|
||||
// even if the value entered by the user was invalid
|
||||
// (e.g. a floating point number).
|
||||
if (this.value !== PDFViewerApplication.page.toString()) {
|
||||
this.value = PDFViewerApplication.page;
|
||||
}
|
||||
});
|
||||
|
||||
eventBus.on("pagesinit", function () {
|
||||
// We can use pdfViewer now, e.g. let's change default scale.
|
||||
pdfViewer.currentScaleValue = DEFAULT_SCALE_VALUE;
|
||||
});
|
||||
|
||||
eventBus.on(
|
||||
"pagechanging",
|
||||
function (evt) {
|
||||
const page = evt.pageNumber;
|
||||
const numPages = PDFViewerApplication.pagesCount;
|
||||
|
||||
document.getElementById("pageNumber").value = page;
|
||||
document.getElementById("previous").disabled = page <= 1;
|
||||
document.getElementById("next").disabled = page >= numPages;
|
||||
},
|
||||
true
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
window.PDFViewerApplication = PDFViewerApplication;
|
||||
|
||||
document.addEventListener(
|
||||
"DOMContentLoaded",
|
||||
function () {
|
||||
PDFViewerApplication.initUI();
|
||||
},
|
||||
true
|
||||
);
|
||||
|
||||
// The offsetParent is not set until the PDF.js iframe or object is visible;
|
||||
// waiting for first animation.
|
||||
const animationStarted = new Promise(function (resolve) {
|
||||
window.requestAnimationFrame(resolve);
|
||||
});
|
||||
|
||||
// We need to delay opening until all HTML is loaded.
|
||||
animationStarted.then(function () {
|
||||
PDFViewerApplication.open({
|
||||
url: DEFAULT_URL,
|
||||
});
|
||||
});
|
||||
76
examples/node/getinfo.mjs
Normal file
@@ -0,0 +1,76 @@
|
||||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
//
|
||||
// Basic node example that prints document metadata and text content.
|
||||
//
|
||||
|
||||
// Run `gulp dist-install` to generate 'pdfjs-dist' npm package files.
|
||||
import { getDocument } from "pdfjs-dist/legacy/build/pdf.mjs";
|
||||
|
||||
// Loading file from file system into typed array
|
||||
const pdfPath =
|
||||
process.argv[2] || "../../web/compressed.tracemonkey-pldi-09.pdf";
|
||||
|
||||
// Will be using promises to load document, pages and misc data instead of
|
||||
// callback.
|
||||
const loadingTask = getDocument(pdfPath);
|
||||
loadingTask.promise
|
||||
.then(function (doc) {
|
||||
const numPages = doc.numPages;
|
||||
console.log("# Document Loaded");
|
||||
console.log("Number of Pages: " + numPages);
|
||||
console.log();
|
||||
|
||||
let lastPromise; // will be used to chain promises
|
||||
lastPromise = doc.getMetadata().then(function (data) {
|
||||
console.log("# Metadata Is Loaded");
|
||||
console.log("## Info");
|
||||
console.log(JSON.stringify(data.info, null, 2));
|
||||
console.log();
|
||||
if (data.metadata) {
|
||||
console.log("## Metadata");
|
||||
console.log(JSON.stringify(data.metadata.getAll(), null, 2));
|
||||
console.log();
|
||||
}
|
||||
});
|
||||
|
||||
const loadPage = function (pageNum) {
|
||||
return doc.getPage(pageNum).then(function (page) {
|
||||
console.log("# Page " + pageNum);
|
||||
const viewport = page.getViewport({ scale: 1.0 });
|
||||
console.log("Size: " + viewport.width + "x" + viewport.height);
|
||||
console.log();
|
||||
return page
|
||||
.getTextContent()
|
||||
.then(function (content) {
|
||||
// Content contains lots of information about the text layout and
|
||||
// styles, but we need only strings at the moment
|
||||
const strings = content.items.map(function (item) {
|
||||
return item.str;
|
||||
});
|
||||
console.log("## Text Content");
|
||||
console.log(strings.join(" "));
|
||||
// Release page resources.
|
||||
page.cleanup();
|
||||
})
|
||||
.then(function () {
|
||||
console.log();
|
||||
});
|
||||
});
|
||||
};
|
||||
// Loading of the first page will wait on metadata and subsequent loadings
|
||||
// will wait on the previous pages.
|
||||
for (let i = 1; i <= numPages; i++) {
|
||||
lastPromise = lastPromise.then(loadPage.bind(null, i));
|
||||
}
|
||||
return lastPromise;
|
||||
})
|
||||
.then(
|
||||
function () {
|
||||
console.log("# End of Document");
|
||||
},
|
||||
function (err) {
|
||||
console.error("Error: " + err);
|
||||
}
|
||||
);
|
||||
15
examples/node/pdf2png/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
## Overview
|
||||
|
||||
Example to demonstrate converting a PDF file to a PNG image using the PDF.js library.
|
||||
|
||||
## Getting started
|
||||
|
||||
Install the dependencies and build the PDF.js library:
|
||||
|
||||
$ npm install
|
||||
$ gulp dist-install
|
||||
|
||||
Run the example to convert the first page of a PDF file to a PNG image:
|
||||
|
||||
$ cd examples/node/pdf2png
|
||||
$ node pdf2png.mjs
|
||||
72
examples/node/pdf2png/pdf2png.mjs
Normal file
@@ -0,0 +1,72 @@
|
||||
/* Copyright 2017 Mozilla Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import fs from "fs";
|
||||
import { getDocument } from "pdfjs-dist/legacy/build/pdf.mjs";
|
||||
|
||||
// Some PDFs need external cmaps.
|
||||
const CMAP_URL = "../../../node_modules/pdfjs-dist/cmaps/";
|
||||
const CMAP_PACKED = true;
|
||||
|
||||
// Where the standard fonts are located.
|
||||
const STANDARD_FONT_DATA_URL =
|
||||
"../../../node_modules/pdfjs-dist/standard_fonts/";
|
||||
|
||||
// Loading file from file system into typed array.
|
||||
const pdfPath =
|
||||
process.argv[2] || "../../../web/compressed.tracemonkey-pldi-09.pdf";
|
||||
const data = new Uint8Array(fs.readFileSync(pdfPath));
|
||||
|
||||
// Load the PDF file.
|
||||
const loadingTask = getDocument({
|
||||
data,
|
||||
cMapUrl: CMAP_URL,
|
||||
cMapPacked: CMAP_PACKED,
|
||||
standardFontDataUrl: STANDARD_FONT_DATA_URL,
|
||||
});
|
||||
|
||||
try {
|
||||
const pdfDocument = await loadingTask.promise;
|
||||
console.log("# PDF document loaded.");
|
||||
// Get the first page.
|
||||
const page = await pdfDocument.getPage(1);
|
||||
// Render the page on a Node canvas with 100% scale.
|
||||
const canvasFactory = pdfDocument.canvasFactory;
|
||||
const viewport = page.getViewport({ scale: 1.0 });
|
||||
const canvasAndContext = canvasFactory.create(
|
||||
viewport.width,
|
||||
viewport.height
|
||||
);
|
||||
const renderContext = {
|
||||
canvasContext: canvasAndContext.context,
|
||||
viewport,
|
||||
};
|
||||
|
||||
const renderTask = page.render(renderContext);
|
||||
await renderTask.promise;
|
||||
// Convert the canvas to an image buffer.
|
||||
const image = canvasAndContext.canvas.toBuffer("image/png");
|
||||
fs.writeFile("output.png", image, function (error) {
|
||||
if (error) {
|
||||
console.error("Error: " + error);
|
||||
} else {
|
||||
console.log("Finished converting first page of PDF file to a PNG image.");
|
||||
}
|
||||
});
|
||||
// Release page resources.
|
||||
page.cleanup();
|
||||
} catch (reason) {
|
||||
console.log(reason);
|
||||
}
|
||||
14
examples/text-only/index.html
Normal file
@@ -0,0 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Text-only PDF.js example</title>
|
||||
<script src="../../node_modules/pdfjs-dist/build/pdf.mjs" type="module"></script>
|
||||
<script src="pdf2svg.mjs" type="module"></script>
|
||||
</head>
|
||||
<body>
|
||||
<p>Text-only PDF.js example</p>
|
||||
<div id="pageContainer" style="display: inline-block; border: solid 1px black;">
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
72
examples/text-only/pdf2svg.mjs
Normal file
@@ -0,0 +1,72 @@
|
||||
/* Copyright 2014 Mozilla Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
const PDF_PATH = "../../web/compressed.tracemonkey-pldi-09.pdf";
|
||||
const PAGE_NUMBER = 1;
|
||||
const PAGE_SCALE = 1.5;
|
||||
const SVG_NS = "http://www.w3.org/2000/svg";
|
||||
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc =
|
||||
"../../node_modules/pdfjs-dist/build/pdf.worker.mjs";
|
||||
|
||||
function buildSVG(viewport, textContent) {
|
||||
// Building SVG with size of the viewport (for simplicity)
|
||||
const svg = document.createElementNS(SVG_NS, "svg:svg");
|
||||
svg.setAttribute("width", viewport.width + "px");
|
||||
svg.setAttribute("height", viewport.height + "px");
|
||||
// items are transformed to have 1px font size
|
||||
svg.setAttribute("font-size", 1);
|
||||
|
||||
// processing all items
|
||||
textContent.items.forEach(function (textItem) {
|
||||
// we have to take in account viewport transform, which includes scale,
|
||||
// rotation and Y-axis flip, and not forgetting to flip text.
|
||||
const tx = pdfjsLib.Util.transform(
|
||||
pdfjsLib.Util.transform(viewport.transform, textItem.transform),
|
||||
[1, 0, 0, -1, 0, 0]
|
||||
);
|
||||
const style = textContent.styles[textItem.fontName];
|
||||
// adding text element
|
||||
const text = document.createElementNS(SVG_NS, "svg:text");
|
||||
text.setAttribute("transform", "matrix(" + tx.join(" ") + ")");
|
||||
text.setAttribute("font-family", style.fontFamily);
|
||||
text.textContent = textItem.str;
|
||||
svg.append(text);
|
||||
});
|
||||
return svg;
|
||||
}
|
||||
|
||||
async function pageLoaded() {
|
||||
// Loading document and page text content
|
||||
const loadingTask = pdfjsLib.getDocument({ url: PDF_PATH });
|
||||
const pdfDocument = await loadingTask.promise;
|
||||
const page = await pdfDocument.getPage(PAGE_NUMBER);
|
||||
const viewport = page.getViewport({ scale: PAGE_SCALE });
|
||||
const textContent = await page.getTextContent();
|
||||
// building SVG and adding that to the DOM
|
||||
const svg = buildSVG(viewport, textContent);
|
||||
document.getElementById("pageContainer").append(svg);
|
||||
// Release page resources.
|
||||
page.cleanup();
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
if (typeof pdfjsLib === "undefined") {
|
||||
// eslint-disable-next-line no-alert
|
||||
alert("Please build the pdfjs-dist library using\n `gulp dist-install`");
|
||||
return;
|
||||
}
|
||||
pageLoaded();
|
||||
});
|
||||
1
examples/webpack/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
node_modules/
|
||||
33
examples/webpack/README.md
Normal file
@@ -0,0 +1,33 @@
|
||||
## Overview
|
||||
|
||||
Example to demonstrate PDF.js library usage with Webpack.
|
||||
|
||||
## Getting started
|
||||
|
||||
Install the example dependencies and build the project:
|
||||
|
||||
$ gulp dist-install
|
||||
$ cd examples/webpack
|
||||
$ npm install
|
||||
$ ./node_modules/webpack/bin/webpack.js
|
||||
|
||||
You can observe the build results by running `gulp server` and navigating to
|
||||
http://localhost:8888/examples/webpack/index.html.
|
||||
|
||||
Refer to the `main.js` and `webpack.config.js` files for the source code.
|
||||
Note that PDF.js packaging requires packaging of the main application and
|
||||
the worker code, and the `workerSrc` path shall be set to the latter file.
|
||||
|
||||
### Minification
|
||||
|
||||
If you are configuring Webpack to output a minified build, please note that you
|
||||
*must* configure the minifier to keep original class/function names intact;
|
||||
otherwise the build is not guaranteed to work correctly.
|
||||
|
||||
## Worker loading
|
||||
|
||||
If you are getting the `Setting up fake worker` warning, make sure you are
|
||||
importing `pdfjs-dist/webpack.mjs` which is the zero-configuration method for
|
||||
Webpack users. Installing `worker-loader` is no longer necessary.
|
||||
|
||||
import * as pdfjsLib from 'pdfjs-dist/webpack.mjs';
|
||||
11
examples/webpack/index.html
Normal file
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>webpack example</title>
|
||||
<script src="../../build/webpack/main.bundle.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<canvas id="theCanvas"></canvas>
|
||||
</body>
|
||||
</html>
|
||||
29
examples/webpack/main.mjs
Normal file
@@ -0,0 +1,29 @@
|
||||
// Any copyright is dedicated to the Public Domain.
|
||||
// http://creativecommons.org/licenses/publicdomain/
|
||||
|
||||
// Hello world example for webpack.
|
||||
|
||||
import * as pdfjsLib from "pdfjs-dist";
|
||||
|
||||
const pdfPath = "../learning/helloworld.pdf";
|
||||
|
||||
// Setting worker path to worker bundle.
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc =
|
||||
"../../build/webpack/pdf.worker.bundle.js";
|
||||
|
||||
// Loading a document.
|
||||
const loadingTask = pdfjsLib.getDocument(pdfPath);
|
||||
const pdfDocument = await loadingTask.promise;
|
||||
// Request a first page
|
||||
const pdfPage = await pdfDocument.getPage(1);
|
||||
// Display page on the existing canvas with 100% scale.
|
||||
const viewport = pdfPage.getViewport({ scale: 1.0 });
|
||||
const canvas = document.getElementById("theCanvas");
|
||||
canvas.width = viewport.width;
|
||||
canvas.height = viewport.height;
|
||||
const ctx = canvas.getContext("2d");
|
||||
const renderTask = pdfPage.render({
|
||||
canvasContext: ctx,
|
||||
viewport,
|
||||
});
|
||||
await renderTask.promise;
|
||||
12
examples/webpack/package.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "webpack-pdf.js-example",
|
||||
"version": "0.2.0",
|
||||
"scripts": {
|
||||
"build": "webpack"
|
||||
},
|
||||
"devDependencies": {
|
||||
"webpack": "^5.89.0",
|
||||
"webpack-cli": "^5.1.4",
|
||||
"pdfjs-dist": "../../node_modules/pdfjs-dist"
|
||||
}
|
||||
}
|
||||
18
examples/webpack/webpack.config.js
Normal file
@@ -0,0 +1,18 @@
|
||||
/* eslint-disable import/no-commonjs */
|
||||
|
||||
const webpack = require("webpack"); // eslint-disable-line no-unused-vars
|
||||
const path = require("path");
|
||||
|
||||
module.exports = {
|
||||
context: __dirname,
|
||||
entry: {
|
||||
main: "./main.mjs",
|
||||
"pdf.worker": "pdfjs-dist/build/pdf.worker.mjs",
|
||||
},
|
||||
mode: "none",
|
||||
output: {
|
||||
path: path.join(__dirname, "../../build/webpack"),
|
||||
publicPath: "../../build/webpack/",
|
||||
filename: "[name].bundle.js",
|
||||
},
|
||||
};
|
||||
1
extensions/chromium/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
content/
|
||||
26
extensions/chromium/background.js
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
Copyright 2024 Mozilla Foundation
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
importScripts(
|
||||
"options/migration.js",
|
||||
"preserve-referer.js",
|
||||
"pdfHandler.js",
|
||||
"extension-router.js",
|
||||
"suppress-update.js",
|
||||
"telemetry.js"
|
||||
);
|
||||
261
extensions/chromium/contentscript.js
Normal file
@@ -0,0 +1,261 @@
|
||||
/*
|
||||
Copyright 2014 Mozilla Foundation
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
var VIEWER_URL = chrome.runtime.getURL("content/web/viewer.html");
|
||||
|
||||
function getViewerURL(pdf_url) {
|
||||
return VIEWER_URL + "?file=" + encodeURIComponent(pdf_url);
|
||||
}
|
||||
|
||||
document.addEventListener("animationstart", onAnimationStart, true);
|
||||
if (document.contentType === "application/pdf") {
|
||||
chrome.runtime.sendMessage({ action: "canRequestBody" }, maybeRenderPdfDoc);
|
||||
}
|
||||
|
||||
function onAnimationStart(event) {
|
||||
if (event.animationName === "pdfjs-detected-object-or-embed") {
|
||||
watchObjectOrEmbed(event.target);
|
||||
}
|
||||
}
|
||||
|
||||
// Called for every <object> or <embed> element in the page.
|
||||
// This may change the type, src/data attributes and/or the child nodes of the
|
||||
// element. This function only affects elements for the first call. Subsequent
|
||||
// invocations have no effect.
|
||||
function watchObjectOrEmbed(elem) {
|
||||
var mimeType = elem.type;
|
||||
if (mimeType && mimeType.toLowerCase() !== "application/pdf") {
|
||||
return;
|
||||
}
|
||||
// <embed src> <object data>
|
||||
var srcAttribute = "src" in elem ? "src" : "data";
|
||||
var path = elem[srcAttribute];
|
||||
if (!mimeType && !/\.pdf($|[?#])/i.test(path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
elem.tagName === "EMBED" &&
|
||||
elem.name === "plugin" &&
|
||||
elem.parentNode === document.body &&
|
||||
elem.parentNode.childElementCount === 1 &&
|
||||
elem.src === location.href
|
||||
) {
|
||||
// This page is most likely Chrome's default page that embeds a PDF file.
|
||||
// The fact that the extension's background page did not intercept and
|
||||
// redirect this PDF request means that this PDF cannot be opened by PDF.js,
|
||||
// e.g. because it is a response to a POST request (as in #6174).
|
||||
// A reduced test case to test PDF response to POST requests is available at
|
||||
// https://robwu.nl/pdfjs/issue6174/.
|
||||
// Until #4483 is fixed, POST requests should be ignored.
|
||||
return;
|
||||
}
|
||||
if (elem.tagName === "EMBED" && elem.src === "about:blank") {
|
||||
// Starting from Chrome 76, internal embeds do not have the original URL,
|
||||
// but "about:blank" instead.
|
||||
// See https://github.com/mozilla/pdf.js/issues/11137
|
||||
return;
|
||||
}
|
||||
|
||||
if (elem.__I_saw_this_element) {
|
||||
return;
|
||||
}
|
||||
elem.__I_saw_this_element = true;
|
||||
|
||||
var tagName = elem.tagName.toUpperCase();
|
||||
var updateEmbedOrObject;
|
||||
if (tagName === "EMBED") {
|
||||
updateEmbedOrObject = updateEmbedElement;
|
||||
} else if (tagName === "OBJECT") {
|
||||
updateEmbedOrObject = updateObjectElement;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
var lastSrc;
|
||||
var isUpdating = false;
|
||||
|
||||
function updateViewerFrame() {
|
||||
if (!isUpdating) {
|
||||
isUpdating = true;
|
||||
try {
|
||||
if (lastSrc !== elem[srcAttribute]) {
|
||||
updateEmbedOrObject(elem);
|
||||
lastSrc = elem[srcAttribute];
|
||||
}
|
||||
} finally {
|
||||
isUpdating = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
updateViewerFrame();
|
||||
|
||||
// Watch for page-initiated changes of the src/data attribute.
|
||||
var srcObserver = new MutationObserver(updateViewerFrame);
|
||||
srcObserver.observe(elem, {
|
||||
attributes: true,
|
||||
childList: false,
|
||||
characterData: false,
|
||||
attributeFilter: [srcAttribute],
|
||||
});
|
||||
}
|
||||
|
||||
// Display the PDF Viewer in an <embed>.
|
||||
function updateEmbedElement(elem) {
|
||||
if (elem.type === "text/html" && elem.src.lastIndexOf(VIEWER_URL, 0) === 0) {
|
||||
// The viewer is already shown.
|
||||
return;
|
||||
}
|
||||
// The <embed> tag needs to be removed and re-inserted before any src changes
|
||||
// are effective.
|
||||
var parentNode = elem.parentNode;
|
||||
var nextSibling = elem.nextSibling;
|
||||
if (parentNode) {
|
||||
elem.remove();
|
||||
}
|
||||
elem.type = "text/html";
|
||||
elem.src = getEmbeddedViewerURL(elem.src);
|
||||
|
||||
if (parentNode) {
|
||||
// Suppress linter warning: insertBefore is preferable to
|
||||
// nextSibling.before(elem) because nextSibling may be null.
|
||||
// eslint-disable-next-line unicorn/prefer-modern-dom-apis
|
||||
parentNode.insertBefore(elem, nextSibling);
|
||||
}
|
||||
}
|
||||
|
||||
// Display the PDF Viewer in an <object>.
|
||||
function updateObjectElement(elem) {
|
||||
// <object> elements are terrible. Experiments (in49.0.2623.75) show that the
|
||||
// following happens:
|
||||
// - When fallback content is shown (e.g. because the built-in PDF Viewer is
|
||||
// disabled), updating the "data" attribute has no effect. Not surprising
|
||||
// considering that HTMLObjectElement::m_useFallbackContent is not reset
|
||||
// once it is set to true. Source:
|
||||
// WebKit/Source/core/html/HTMLObjectElement.cpp#378 (rev 749fe30d676b6c14).
|
||||
// - When the built-in PDF Viewer plugin is enabled, updating the "data"
|
||||
// attribute reloads the content (provided that the type was correctly set).
|
||||
// - When <object type=text/html data="chrome-extension://..."> is used
|
||||
// (tested with a data-URL, data:text/html,<object...>, the extension's
|
||||
// origin allowlist is not set up, so the viewer can't load the PDF file.
|
||||
// - The content of the <object> tag may be affected by <param> tags.
|
||||
//
|
||||
// To make sure that our solution works for all cases, we will insert a frame
|
||||
// as fallback content and force the <object> tag to render its fallback
|
||||
// content.
|
||||
var iframe = elem.firstElementChild;
|
||||
if (!iframe || !iframe.__inserted_by_pdfjs) {
|
||||
iframe = createFullSizeIframe();
|
||||
elem.textContent = "";
|
||||
elem.append(iframe);
|
||||
iframe.__inserted_by_pdfjs = true;
|
||||
}
|
||||
iframe.src = getEmbeddedViewerURL(elem.data);
|
||||
|
||||
// Some bogus content type that is not handled by any plugin.
|
||||
elem.type = "application/not-a-pee-dee-eff-type";
|
||||
// Force the <object> to reload and render its fallback content.
|
||||
elem.data += "";
|
||||
|
||||
// Usually the browser renders plugin content in this tag, which is completely
|
||||
// oblivious of styles such as padding, but we insert and render child nodes,
|
||||
// so force padding to be zero to avoid undesired dimension changes.
|
||||
elem.style.padding = "0";
|
||||
|
||||
// <object> and <embed> elements have a "display:inline" style by default.
|
||||
// Despite this property, when a plugin is loaded in the tag, the tag is
|
||||
// treated like "display:inline-block". However, when the browser does not
|
||||
// render plugin content, the <object> tag does not behave like that, and as
|
||||
// a result the width and height is ignored.
|
||||
// Force "display:inline-block" to make sure that the width/height as set by
|
||||
// web pages is respected.
|
||||
// (<embed> behaves as expected with the default display value, but setting it
|
||||
// to display:inline-block doesn't hurt).
|
||||
elem.style.display = "inline-block";
|
||||
}
|
||||
|
||||
// Create an <iframe> element without borders that takes the full width and
|
||||
// height.
|
||||
function createFullSizeIframe() {
|
||||
var iframe = document.createElement("iframe");
|
||||
iframe.style.background = "none";
|
||||
iframe.style.border = "none";
|
||||
iframe.style.borderRadius = "none";
|
||||
iframe.style.boxShadow = "none";
|
||||
iframe.style.cssFloat = "none";
|
||||
iframe.style.display = "block";
|
||||
iframe.style.height = "100%";
|
||||
iframe.style.margin = "0";
|
||||
iframe.style.maxHeight = "none";
|
||||
iframe.style.maxWidth = "none";
|
||||
iframe.style.position = "static";
|
||||
iframe.style.transform = "none";
|
||||
iframe.style.visibility = "visible";
|
||||
iframe.style.width = "100%";
|
||||
return iframe;
|
||||
}
|
||||
|
||||
// Get the viewer URL, provided that the path is a valid URL.
|
||||
function getEmbeddedViewerURL(path) {
|
||||
var fragment = /^([^#]*)(#.*)?$/.exec(path);
|
||||
path = fragment[1];
|
||||
fragment = fragment[2] || "";
|
||||
|
||||
// Resolve relative path to document.
|
||||
var a = document.createElement("a");
|
||||
a.href = document.baseURI;
|
||||
a.href = path;
|
||||
path = a.href;
|
||||
return getViewerURL(path) + fragment;
|
||||
}
|
||||
|
||||
function maybeRenderPdfDoc(isNotPOST) {
|
||||
if (!isNotPOST) {
|
||||
// The document was loaded through a POST request, but we cannot access the
|
||||
// original response body, nor safely send a new request to fetch the PDF.
|
||||
// Until #4483 is fixed, POST requests should be ignored.
|
||||
return;
|
||||
}
|
||||
|
||||
// Detected PDF that was not redirected by the declarativeNetRequest rules.
|
||||
// Maybe because this was served without Content-Type and sniffed as PDF.
|
||||
// Or because this is Chrome 127-, which does not support responseHeaders
|
||||
// condition in declarativeNetRequest (DNR), and PDF requests are therefore
|
||||
// not redirected via DNR.
|
||||
|
||||
// In any case, load the viewer.
|
||||
console.log(`Detected PDF via document, opening viewer for ${document.URL}`);
|
||||
|
||||
// Ideally we would use logic consistent with the DNR logic, like this:
|
||||
// location.href = getEmbeddedViewerURL(document.URL);
|
||||
// ... unfortunately, this causes Chrome to crash until version 129, fixed by
|
||||
// https://chromium.googlesource.com/chromium/src/+/8c42358b2cc549553d939efe7d36515d80563da7%5E%21/
|
||||
// Work around this by replacing the body with an iframe of the viewer.
|
||||
// Interestingly, Chrome's built-in PDF viewer uses a similar technique.
|
||||
const shadowRoot = document.body.attachShadow({ mode: "closed" });
|
||||
const iframe = document.createElement("iframe");
|
||||
iframe.style.position = "absolute";
|
||||
iframe.style.top = "0";
|
||||
iframe.style.left = "0";
|
||||
iframe.style.width = "100%";
|
||||
iframe.style.height = "100%";
|
||||
iframe.style.border = "0 none";
|
||||
iframe.src = getEmbeddedViewerURL(document.URL);
|
||||
shadowRoot.append(iframe);
|
||||
}
|
||||
14
extensions/chromium/contentstyle.css
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Detect creation of <embed> and <object> tags.
|
||||
*/
|
||||
@keyframes pdfjs-detected-object-or-embed {
|
||||
from {
|
||||
/* empty */
|
||||
}
|
||||
}
|
||||
object,
|
||||
embed {
|
||||
animation-delay: 0s !important;
|
||||
animation-name: pdfjs-detected-object-or-embed !important;
|
||||
animation-play-state: running !important;
|
||||
}
|
||||
105
extensions/chromium/extension-router.js
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
Copyright 2013 Mozilla Foundation
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
(function ExtensionRouterClosure() {
|
||||
var VIEWER_URL = chrome.runtime.getURL("content/web/viewer.html");
|
||||
var CRX_BASE_URL = chrome.runtime.getURL("/");
|
||||
|
||||
var schemes = [
|
||||
"http",
|
||||
"https",
|
||||
"file",
|
||||
"chrome-extension",
|
||||
"blob",
|
||||
"data",
|
||||
// Chromium OS
|
||||
"filesystem",
|
||||
// Chromium OS, shorthand for filesystem:<origin>/external/
|
||||
"drive",
|
||||
];
|
||||
|
||||
/**
|
||||
* @param {string} url The URL prefixed with chrome-extension://.../
|
||||
* @returns {string|undefined} The percent-encoded URL of the (PDF) file.
|
||||
*/
|
||||
function parseExtensionURL(url) {
|
||||
url = url.substring(CRX_BASE_URL.length);
|
||||
// Find the (url-encoded) colon and verify that the scheme is allowed.
|
||||
var schemeIndex = url.search(/:|%3A/i);
|
||||
if (schemeIndex === -1) {
|
||||
return undefined;
|
||||
}
|
||||
var scheme = url.slice(0, schemeIndex).toLowerCase();
|
||||
if (schemes.includes(scheme)) {
|
||||
// NOTE: We cannot use the `updateUrlHash` function in this context.
|
||||
url = url.split("#", 1)[0];
|
||||
if (url.charAt(schemeIndex) === ":") {
|
||||
url = encodeURIComponent(url);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function resolveViewerURL(originalUrl) {
|
||||
if (originalUrl.startsWith(CRX_BASE_URL)) {
|
||||
// This listener converts chrome-extension://.../http://...pdf to
|
||||
// chrome-extension://.../content/web/viewer.html?file=http%3A%2F%2F...pdf
|
||||
var url = parseExtensionURL(originalUrl);
|
||||
if (url) {
|
||||
url = VIEWER_URL + "?file=" + url;
|
||||
var i = originalUrl.indexOf("#");
|
||||
if (i > 0) {
|
||||
url += originalUrl.slice(i);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
self.addEventListener("fetch", event => {
|
||||
const req = event.request;
|
||||
if (req.destination === "document") {
|
||||
var url = resolveViewerURL(req.url);
|
||||
if (url) {
|
||||
console.log("Redirecting " + req.url + " to " + url);
|
||||
event.respondWith(Response.redirect(url));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Ctrl + F5 bypasses service worker. the pretty extension URLs will fail to
|
||||
// resolve in that case. Catch this and redirect to destination.
|
||||
chrome.webNavigation.onErrorOccurred.addListener(
|
||||
details => {
|
||||
if (details.frameId !== 0) {
|
||||
// Not a top-level frame. Cannot easily navigate a specific child frame.
|
||||
return;
|
||||
}
|
||||
const url = resolveViewerURL(details.url);
|
||||
if (url) {
|
||||
console.log(`Redirecting ${details.url} to ${url} (fallback)`);
|
||||
chrome.tabs.update(details.tabId, { url });
|
||||
}
|
||||
},
|
||||
{ url: [{ urlPrefix: CRX_BASE_URL }] }
|
||||
);
|
||||
|
||||
console.log("Set up extension URL router.");
|
||||
})();
|
||||
BIN
extensions/chromium/icon128.png
Normal file
|
After Width: | Height: | Size: 2.9 KiB |
BIN
extensions/chromium/icon16.png
Normal file
|
After Width: | Height: | Size: 594 B |
BIN
extensions/chromium/icon48.png
Normal file
|
After Width: | Height: | Size: 1.6 KiB |