1:乱码问题。最近遇到一个问题,一个pdf 文件使用了冷门的字体,导致出现乱码,字体空白,如下图所示
2:问题排查,初步排查,治标。
修改改pdf 文件,把字体都修改成宋体。再次预览,显示正常 。
3:从问题上根治这个问题,治本
总不能把每个pdf 都修改一下字体,这样太麻烦。经过测试,找到治本的方法。
下载 http://download.****.net/download/amu0521/10107388?web=web 后,
web目录有一个文件夹名字叫cmap,拷贝出来,放到网站根目录,如下图
然后修改网页代码,代码模块增加
PDFJS.cMapUrl = 'cmaps/'; PDFJS.cMapPacked = true;
然后运行后,问题解决
完成代码如下:
<html> <head> <title>pdf.js展示1,上一页,下一页</title> <meta charset="UTF-8"> </head> <h1>PDF.js Previous/Next example</h1> <div> <button id="prev">Previous</button> <button id="next">Next</button> <span>Page: <span id="page_num"></span> / <span id="page_count"></span></span> </div> <canvas id="the-canvas"></canvas> <script src="js/pdf.js"></script> <script src="js/pdf.worker.js"></script> <script> var url = 'doc/demo2.pdf'; var pdfDoc = null, pageNum = 1, pageRendering = false, pageNumPending = null, scale = 1,//显示比例,0到1之间 canvas = document.getElementById('the-canvas'), ctx = canvas.getContext('2d'); PDFJS.cMapUrl = 'cmaps/'; PDFJS.cMapPacked = true; /** * Get page info from document, resize canvas accordingly, and render page. * @param num Page number. */ function renderPage(num) { pageRendering = true; // Using promise to fetch the page pdfDoc.getPage(num).then(function(page) { var viewport = page.getViewport(scale); canvas.height = viewport.height; canvas.width = viewport.width; // Render PDF page into canvas context var renderContext = { canvasContext: ctx, viewport: viewport }; var renderTask = page.render(renderContext); // Wait for rendering to finish renderTask.promise.then(function() { pageRendering = false; if (pageNumPending !== null) { // New page rendering is pending renderPage(pageNumPending); pageNumPending = null; } }); }); // Update page counters document.getElementById('page_num').textContent = num; } /** * If another page rendering in progress, waits until the rendering is * finised. Otherwise, executes rendering immediately. */ function queueRenderPage(num) { if (pageRendering) { pageNumPending = num; } else { renderPage(num); } } /** * Displays previous page. */ function onPrevPage() { if (pageNum <= 1) { return; } pageNum--; queueRenderPage(pageNum); } document.getElementById('prev').addEventListener('click', onPrevPage); /** * Displays next page. */ function onNextPage() { if (pageNum >= pdfDoc.numPages) { return; } pageNum++; queueRenderPage(pageNum); } document.getElementById('next').addEventListener('click', onNextPage); /** * Asynchronously downloads PDF. */ PDFJS.getDocument(url).then(function(pdfDoc_) { pdfDoc = pdfDoc_; document.getElementById('page_count').textContent = pdfDoc.numPages; // Initial/first page rendering renderPage(pageNum); }); </script> </html>
最终的目录布局,如下图