Open3
【PWA】PDF.jsをPDFファイルを開く既定のアプリにしてみた
目標
- PDF.jsをPWA化する
- File Handling APIでPDFファイルを開かせる
ダウンロード
PDF.js Getting StartedからPrebuild (modern browsers)をダウンロードして展開
index.htmlの設定、manifest.webmanifestの記述
-
pdfjs-3.4.120-dist/web/viewer.html
が本体なので、ファイル名をindex.html
に改名する -
index.html
にmanifest.webmanifest
を追加
index.html
...
<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</title>
<link rel="stylesheet" href="viewer.css">
<!-- This snippet is used in production (included from viewer.html) -->
<link rel="resource" type="application/l10n" href="locale/locale.properties">
+ <link rel="manifest" href="manifest.webmanifest">
<script src="../build/pdf.js"></script>
<script src="viewer.js"></script>
</head>
...
- 同フォルダ内に
manifest.webmanifest
を記述
manifest.webmanifest
{
"$schema": "https://json.schemastore.org/web-manifest-combined.json",
"name": "PDF.js",
"short_name": "PDF.js",
"start_url": ".",
"display": "standalone",
"background_color": "#333",
"description": "pdf viewer",
"icons": [{
"src": "images/icons/icon192.png",
"sizes": "192x192",
"type": "image/png"
}],
"file_handlers": [
{
"action": "/open-pdf",
"accept": {
"application/pdf": [".pdf"]
},
"icons": [
{
"src": "images/icons/icon192.png",
"sizes": "192x192",
"type": "image/png"
}
],
"launch_type": "multiple-clients"
}
]
}