PDF.js无法在IE中正确呈现pdf

时间:2015-07-21 15:04:16

标签: javascript canvas pdf.js pdf-parsing pdf-rendering

我正在使用PDF.js框架来呈现PDF。我正在使用base64数据来呈现PDF。但在IE 11 pdf看起来模糊。

请参阅IE 11的以下屏幕

enter image description here

见下面的代码:

var renderPDF = function(url, canvasContainer,data) {
        var scale= 0.9;  //"zoom" factor for the PDF

        function renderPage(page) {
            var canvas = document.createElement('canvas');
            var viewport = page.getViewport(scale);

            var ctx = canvas.getContext('2d');
            var renderContext = {
                canvasContext: ctx,
                viewport: viewport
            };

            canvas.height = viewport.height;
            canvas.width = viewport.width;

            canvasContainer.appendChild(canvas);

            page.render(renderContext);
        }

        function renderPages(pdfDoc) {
            for(var num = 1; num <= pdfDoc.numPages; num++)
                pdfDoc.getPage(num).then(renderPage);
        }

        PDFJS.disableWorker = false;

        var pdfAsDataUri = "data:application/pdf;base64,"+data; // shortened
        var pdfAsArray = convertDataURIToBinary(pdfAsDataUri);
        PDFJS.getDocument(pdfAsArray).then(renderPages);

    };

    var BASE64_MARKER = ';base64,';

    var convertDataURIToBinary = function(dataURI) {
      var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
      var base64 = dataURI.substring(base64Index);
      var raw = atob(base64);
      var rawLength = raw.length;
      var array = new Uint8Array(new ArrayBuffer(rawLength));

      for(var i = 0; i < rawLength; i++) {
        array[i] = raw.charCodeAt(i);
      }
      return array;
    };

请帮帮我。

2 个答案:

答案 0 :(得分:2)

如果您的PDF.js无法在IE11中运行,则需要compatibility.js。 您可以在标记之后(在任何其他<script>之前)将以下行插入代码:

<script type="text/javascript" src="../../web/compatibility.js"></script>

您还可以在下面添加文件:

希望有所帮助。

答案 1 :(得分:1)

Thats the code for PDF.js framework... i would do is to check if IE10 works OK... if not!!! then your code have some stuff than cannot create from the right way... PDF.js that you can validate. (validate on IE11) then is not framework issue.

Other issue could be that some characters are not OK!! could you post your var "data"??? Regards!!!