在使用vue-pdf-embed過程中, 突然發現有些pdf文件可以正常打開, 有些文件只顯示了一些數字, 并且控制臺報出如下警告:
Warning: loadFont - translateFont failed: “UnknownErrorException: Ensure that the cMapUrl
and cMapPacked
API parameters are provided.”.
Warning: Cannot substitute the font because of its name:
Warning: Error during font loading: Ensure that the cMapUrl
and cMapPacked
API parameters are provided.
經過搜索, 發現是在加載包含中文字符的pdf文件時, 缺少了映射文件導致的. 這時, 我們要修改vue-pdf-embed的source內容.
<vue-pdf-embed :source="pdfSource" />const pdfSource = computed(() => ({url: 文件的url路徑,cMapUrl: '/cmaps/', //public目錄下的cmaps文件夾cMapPacked: true,standardFontDataUrl: '/standard_fonts/' //public目錄下的standard_fonts文件夾
}));
那么, 這兩個cmaps和standard_fonts文件又要到哪里下載呢?
進入https://github.com/mozilla/pdfjs-dist PDF.js官方倉庫
進入官方倉庫,我們會看到這個目錄列表, 把我們需要的文件下載下來, 放到你的vue項目的public目錄即可.