LoginSignup
4
1

More than 3 years have passed since last update.

Pdf.jsで"The cMap url must be specified"のエラーが出たときの解決法

Last updated at Posted at 2020-07-28

散々探しても中々情報が出てきませんでしたが、
以下のサイトに解決法がありました。
PDF.jsで日本語を表示させるのに苦労した話

普通にエラーメッセージでググると
pdfjsLib.cMapUrl = "~/cmaps/";
pdfjsLib.cMapPacked = true;

みたいなワンライナーが出てくるかと思いますが、
私の場合はこれでは解決しませんでした。

解決法は以下

main.js
pdfjsLib.getDocument({  
  url: '<PDFへのurlやpath>',  
  // モジュール内にあるcmapsディレクトリーへのpathを指定してください  
  // 末尾のスラッシュ'/'まで必須です  
  cMapUrl: './node_modules/pdfjs-dist/cmaps/',  
  cMapPacked: true,  
})  

*htmlのscriptダグからpdf.jsを読み込んで使用する場合、変数名「PDJFS」は既にDeprecatedで動きません。
代わりに「pdfjsLib」を使いましょう。

【追記】
pdf.jsのサンプルはこちらにまとまっています。
https://github.com/mozilla/pdf.js/tree/master/examples

4
1
2

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
4
1