【问题标题】:How to open and read a PDF from a binary stream in javascript如何在 javascript 中从二进制流中打开和读取 PDF
【发布时间】:2016-07-13 08:07:39
【问题描述】:

我在用 JavaScript 读取二进制数据流时遇到了一些困难。

目前,会弹出一个窗口并显示加载 PDF 文档失败。 我将在下面发布我的回复和代码,任何帮助将不胜感激。

这是我的后端 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);
    }

这是我的前端 JavaScript 控制器:

$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);
            });

    }

这只是我在浏览器的控制台中得到的响应的一个 sn-p:

%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�=���

【问题讨论】:

    标签: javascript php url pdf


    【解决方案1】:

    我实际上已经修复了它,以便它可以按我的需要工作!

    我在 blob 之前添加了responseType: 'arraybuffer',,然后成功地将其作为应用程序/pdf 调用,它运行良好。

    $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);
                });
    
        }
    

    【讨论】:

      【解决方案2】:

      我认为你不能像那样“打开并阅读”PDF 文件。您应该考虑使用PDF 渲染和解析库,例如PDF.js

      【讨论】:

      猜你喜欢
      • 2016-05-02
      • 2016-09-09
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      相关资源
      最近更新 更多