pdf.js ----- rear return utf-8 data stream, the data show front end processing pdf

Requirements: When do the project FBI interfaces found Knowledge pdf failed to show, through communication with the back-end staff and found the following problems:

           1. Interface returns utf-8 data stream, but the front end of the call is base64 analytical method;

              Leading to function incorrectly;

Scheme One: the utf-8 data stream returned to the rear end of the parsed data flow through the base64. ======== new idea: try to slow to load, but failed to achieve.

            Test results: Andrews system can be used normally, but ios problematic flash back then, and base64 if handled file a 2M after parsing volume nearly as 4M, for small files or with large files easily cause the system to run out of memory, out of App.

Option Two: iframe static display with base64 file stream.

            Test results: ios can be used normally, but Andrews system iframe compatibility issues.

<iframe  ng-src="{{pdfPath}}"  style="width:100%;height:800px;"> </iframe>

Scheme Three: the rear end of the data stream returned to utf-8, the backend returns a data stream modify the parsing method. ------Solve the problem.

1     <div style="width: 100%;"  ng-style="{height: screenHeight-160 + 'px'}">
2           <div id="pdf-containerTF"  style="width: 100%; height: 100%;border-radius: 10px; box-shadow: 0 0 10px #3268d2; background: #ffffff;padding: 0 5px;margin-top:5px;">
3                   <ion-scroll delegate-handle="pdfScroll" zooming="true" direction="xy" style="width: 100%; height: 100%;" scrollbar-y="false" scrollbar-x="false" min-zoom="1">
4                         <div id="pdf-popTF"  style="width: 100%; height: 100%;border-radius: 10px; background: #ffffff;position: absolute;padding: 0px;">
5                         </div>
6                     </ion-scroll>
7               </div>
8      </div>            
 $scope.getFileData=function () {
           var params={
                fileId:$scope.fileId
                }
         $ionicLoading.show({});
          yxxPdfHttp.post(QUERY_SERVICE_FILE_DATA,params, function (data) {
                    if(data){
                        $ionicLoading.hide({});
                        $scope.fileDataObj.fileTitle=$scope.fileTitle;
                        showPdfFileM(data);
                        $rootScope.isRefresh = true;
                    }else{
                        $ionicLoading.hide({});
                        alert({type: '', title: '温馨提示', msg: "请求发生异常"});
                    }
                }, Function (Data) { 
                    $ ionicLoading.hide ({}); 
                    Alert ({type: '', title: 'Error', msg: '! Server is busy, please try again later'}); 
                }); 
    }

  

Package ajax request specific data stream to a request 
// package ajax pdf of received 
    .factory ( 'yxxPdfHttp', [ " $ http", "$ rootScope", "$ timeout", function ($ http, $ rootScope, $ timeout) { 
        var URL = ""; 
        return { 
            POST: function (Method, the params, Success, error, config) { 
                // the console.log (the params) 
                var = {head 
                    the accepts: '* / *', 
                    the Authorization: 'Bearer' + rootScope.Authorization $ 
                }; 
                IF (Method == QUERY_LOGIN_TOKEN) { 
                    head = { 
                        the accepts: '* / *' 
                    };
                } 
                Was asflag = true;
                if (config) {
                    if ('async' in config) {
                        asflag = config.async;
                    }
                }
                $.ajax({
                    type: "post",
                    url: serverPath + method,
                    contentType: 'application/json',
                    // responseType:'multipart/form-data',
                    mimeType: 'text/plain; charset=x-user-defined',  
                    dataType: 'text',
                    headers: head,
                    timeout: 180000,
                    async: asflag,
                    data: JSON.stringify(params),
                    success: function (data, status, xhr) {
                        // success(JSON.parse(data));
                        success(data);
                        //    console.log(data);
                    },
                    complete: function (request, textStatus) {
                        if (textStatus == 'timeout') {
                            alert({ type: 'error', title: '温馨提示', msg: "请求超时" });
                            $timeout(function () { $rootScope.$apply() })
                        }
                    },
                    error: function (request, data, exception) {
                        error(request.responseJSON);
                        the console.log (Request); 
                                        the params: {
                        the console.log (Data); 
                        IF (== 0 && request.readyState request.status == 0) { 
                            the console.log (request.readyState); 
                            the console.log (request.status); 
                        } the else IF (&& Request Request. Status! = 200 is) { 
                            // the token invalid login page non-native modulation method returns to the login page 
                            IF (request.status == 401) { 
                                IF (window.webkit) { 
                                    window.webkit.messageHandlers.authfailed.postMessage ({ 
                                            code : to false 
                                        } 
                                    })
                                } else {
                                    nativeObj.backLogin("88");
                                }
                            } else {
                                var message = '';
                                if (request.responseJSON) {
                                    message = request.responseJSON.message;
                                } else if (request.responseText) {
                                    message = JSON.parse(request.responseText).message;
                                } else {
                                    message = '请求异常';
                                } 
                                Alert ({type: '', title: request.status + ':' + + Method ':' + request.getResponseHeader ( 'the request_id'), MSG: Message}); 
                            } 
                        } the else { 
                            Alert ({type: ' error ', title:' error ', msg: "Please contact the administrator system abnormalities."}); 
                        } 
                        $ timeout (function () {$ $ rootScope Apply ()}). 
                    } 

                }); 
            } 
        }; 
    }])

  

Modified package parsing UTF- . 8 method
 var showPdfFileM = function (PDF) {
     // var DEFAULT_URL = ""; // Note that the variable is deleted here redefining   
    // var PDFData = ""; 
    var rawLength = pdf.length;
     // converted Uint8Array type pdf.js directly parsed, 4068-see pdf.js   
    var Array = new new Uint8Array ( new new an ArrayBuffer (rawLength));
     for (I = 0; I <rawLength; I ++ ) { 
        Array [I] pdf.charCodeAt = (I) & 0xFF ; 
    } 
    // DEFAULT_URL = Array; 

    // var = char2buf Data (window.atob (PDF)); 
    var Data =Array; 
    pdfjsLib.getDocument ({ 
        Data: Data, 
        cMapUrl: 'lib / pdf.js / Web / CMaps /' , 
        cMapPacked: to true 
    }). the then ( function (pdfDoc_) { 
        pdfDoc = pdfDoc_; 
        COUNT = pdfDoc.numPages;
         / / draw all pages 
        renderAllTF (); 
    });

 

Guess you like

Origin www.cnblogs.com/linm/p/12320689.html