使用基于PDF.js的PDF预览查看器到CakePHP 2.x.

问题描述 投票:0回答:2

我是一种新的cakephp和我试图使用这个非常好的PDF预览我在网上找到https://gist.github.com/ichord/9808444

<script src="http://cdnjs.cloudflare.com/ajax/libs/processing.js/1.4.1/processing-api.min.js"></script><html>
<!--
  Created using jsbin.com
  Source can be edited via http://jsbin.com/pdfjs-helloworld-v2/8598/edit
-->
<body>
  <canvas id="the-canvas" style="border:1px solid black"></canvas>
  <input id='pdf' type='file'/>

  <!-- Use latest PDF.js build from Github -->
  <script type="text/javascript" src="https://rawgithub.com/mozilla/pdf.js/gh-pages/build/pdf.js"></script>
  
  <script type="text/javascript">
    //
    // Disable workers to avoid yet another cross-origin issue (workers need the URL of
    // the script to be loaded, and dynamically loading a cross-origin script does
    // not work)
    //
    PDFJS.disableWorker = true;
    //
    // Asynchronous download PDF as an ArrayBuffer
    //
    var pdf = document.getElementById('pdf');
    pdf.onchange = function(ev) {
      if (file = document.getElementById('pdf').files[0]) {
        fileReader = new FileReader();
        fileReader.onload = function(ev) {
          console.log(ev);
          PDFJS.getDocument(fileReader.result).then(function getPdfHelloWorld(pdf) {
            //
            // Fetch the first page
            //
            console.log(pdf)
            pdf.getPage(1).then(function getPageHelloWorld(page) {
              var scale = 0.8;
              var viewport = page.getViewport(scale);
              //
              // Prepare canvas using PDF page dimensions
              //
              var canvas = document.getElementById('the-canvas');
              var context = canvas.getContext('2d');
              canvas.height = viewport.height;
              canvas.width = viewport.width;
              //
              // Render PDF page into canvas context
              //
              var task = page.render({canvasContext: context, viewport: viewport})
              task.promise.then(function(){
                console.log(canvas.toDataURL('image/jpeg'));
              });
            });
          }, function(error){
            console.log(error);
          });
        };
        fileReader.readAsArrayBuffer(file);
      }
    }
            alert(file.name)
  </script>
  

<style id="jsbin-css">
</style>
<script>
</script>
</body>
</html>​

但我不能让它与cakephp一起工作。

它适用于我在cakephp环境之外的服务器,我想我必须使用它作为插件,但我无法弄明白。它使用pdf.js和processing-api.js库,我认为问题是加载这些库。

我想要的是用户在他/她上传之前预览他想要上传的PDF

先感谢您。

问候

javascript jquery cakephp pdf pdfjs
2个回答
0
投票

我最终找到了解决方案,我补充说:PDFJS.workerSrc ='/ js / pdf.worker.js';在PDFJS.disableWorker = true之前;

现在它的作品!


0
投票

我希望测试你的代码,问题是尽管上面做了更改,它仍然无效。我在webroot的js目录中上传了“pdf.worker.js”文件但没有任何结果。我真的很感激,如果你能指出我的错误,以便我可以在我的cakephp中工作,2.8提前谢谢你

© www.soinside.com 2019 - 2024. All rights reserved.