dongsu3664 2016-07-13 08:07
浏览 726
已采纳

如何在javascript中打开和读取二进制流中的PDF

I'm having some difficulties in reading a binary data stream in JavaScript.

Currently, a window pops open and says Failed to load PDF document. I'll post my response and code below, any help would be greatly appreciated.

Here's my back-end PHP:

function getPDF() {
        $file = __DIR__.'\..\pdf\FAS_HeadedPaper.pdf';
        header("Content-Length: " . filesize ( $file ) );
        header("Content-type: application/octet-stream");
        header("Content-disposition: attachment; filename=".basename($file));
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        ob_clean();
        flush();

        readfile($file);
    }

Here's my front end JavaScript controller:

$scope.sendRef = function(ref){

        $http({
            method: 'GET',
            url: $scope.endpoint + 'attachments/PDF',
            async: true,
            headers: {
                'Authorization': 'MyToken'
            },
            params: {
                ref: ref
            },
            dataType:'blob'
        })
            .success(function (data) {
                console.log(data);


                var file = new Blob([(data['response'])], {type: 'application/pdf'});
                //var file = data['response'];
                var fileURL = URL.createObjectURL(file);
                window.open(fileURL);
                $scope.content = $sce.trustAsResourceUrl(fileURL);

            })
            .error(function (data, status) {
                console.log('Error: ' + status);
                console.log(data);
            });

    }

This is just a snippet of the response I get in my console on my browser:

%PDF-1.5
%����
1 0 obj
<</Type/Catalog/Pages 2 0 R/Lang(en-GB) /StructTreeRoot 14 0 R/MarkInfo<</Marked true>>>>
endobj
2 0 obj
<</Type/Pages/Count 1/Kids[ 3 0 R] >>
endobj
3 0 obj
<</Type/Page/Parent 2 0 R/Resources<</Font<</F1 5 0 R/F2 7 0 R/F3 9 0 R>>/XObject<</Image11 11 0 R>>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/MediaBox[ 0 0 595.32 841.92] /Contents 4 0 R/Group<</Type/Group/S/Transparency/CS/DeviceRGB>>/Tabs/S/StructParents 0>>
endobj
4 0 obj
<</Filter/FlateDecode/Length 917>>
stream
x��V[O�X~���p�V>��eUU[B
�J�B���J�-�S�H����  -��Y�������'۴�E1k����۶-f7��]��z�%�~ߔ�E�\UE���7o��ɘO����dR�0l�$�'�v,
V��z8 �PL�����e�r����pp�E6.�6�0L�`�*<�}�5   Q��)ӗ��.k�Hgu�m���dc|h����&���x�ߞ��{GIY�L��d
��e��LMd�ڛb�i��~e%��X���\�l:C~)P%=������\g7+14)^��} ����~�+.m��2"-�w�Q��?   KZKP��Su�=���
  • 写回答

2条回答 默认 最新

  • dongtong848825 2016-07-13 13:41
    关注

    I actually fixed it so that it would work as I needed it!

    I added responseType: 'arraybuffer', just before the blob and then called it as an application/pdf within the success and it worked fine.

    $scope.sendRef = function(ref){
    
            $http({
                method: 'GET',
                url: $scope.endpoint + 'attachments/PDF',
                async: true,
                headers: {
                    'Authorization': 'MyToken'
                },
                params: {
                    ref: ref
                },
                responseType: 'arraybuffer',
                dataType:'blob'
            })
                .success(function (data) {
                    console.log(data);
    
    
                    var file = new Blob([(data['response'])], {type: 'application/pdf'});
                    //var file = data['response'];
                    var fileURL = URL.createObjectURL(file);
                    window.open(fileURL);
                    $scope.content = $sce.trustAsResourceUrl(fileURL);
    
                })
                .error(function (data, status) {
                    console.log('Error: ' + status);
                    console.log(data);
                });
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题