在web上加載pdf文件,並禁止用戶下載和保存。起初考慮使用embed加載pdf,配置pdf連接尾部增長 #toolbar=0 ,但此方案僅在chrome下有效。所以考慮使用pdf.js作自定義pdf渲染。node
pdfjsLib.getDocument({
url: this.url,
withCredentials: true, // 容許攜帶cookie
});
複製代碼
pdfjsLib.getDocument({
cMapUrl: 'https://unpkg.com/pdfjs-dist@2.0.943/cmaps/', // 使用cdn加載pdf.js提供的字體文件。
cMapPacked: true, // 此參數須要設爲true
});
複製代碼
pdfjsLib.GlobalWorkerOptions.workerSrc = 'static/js/bundle.js';
複製代碼
// webpack配置
entry: {
main: 'xxxxx',
'pdf.worker': path.join(process.cwd(), 'node_moudules/pdfjs-dist/build/pdf.worker.entry')
},
output: {
filename: 'static/js/bundle.js',
publicPath: '/',
},
複製代碼
document.oncontextmenu = () => {
event.returnValue = false;
// 兼容ie
return false;
};
複製代碼