12
9

More than 1 year has passed since last update.

pdf.jsを使って日本語ファイルを表示する

Posted at

CDNのpdf.jsで日本語を含むPDFファイルを表示させるのに苦戦したのでメモ.
Prebuildのzipを展開して設置する場合も多分ほぼ同じ.

検索すると viewer.html を使うやり方はある程度出てくるけれど,pdf.js 自体を読み込んでスクリプトを書く場合の情報は見つけづらい印象.

使用バージョン: pdf.js 2.16.105

公式のExample

基本設定

コンソールに Deprecated API usage: No "GlobalWorkerOptions.workerSrc" specified. と表示されるときは

pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://unpkg.com/pdfjs-dist@2.16.105/build/pdf.worker.js';

と書けばよい.(公式の例にもこの記述がある)

日本語表示

コンソールに
Warning: loadFont - translateFont failed: "UnknownErrorException: CMapReaderFactory not initialized, see the `useWorkerFetch` parameter.".
Warning: Error during font loading: CMapReaderFactory not initialized, see the `useWorkerFetch` parameter.
と表示されて日本語が描画されないときは

const loadingTask = pdfjsLib.getDocument({
    url: 'sample.pdf',
    cMapUrl: "https://unpkg.com/pdfjs-dist@2.16.105/cmaps/",
    cMapPacked: true,
});

のように cMap を明示的に指定する.

その他参考記事

12
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
9