1

是一个笨拙的链接。我pdfSample.js使用节点运行,pdfsample.html是要转换为 pdf 并generatepdf.html用于获取它的实际数据。

当我运行它时,它向我显示一个错误Failed to load Pdf Document。我参考了这个样本。我没有得到任何问题,为什么它不起作用?

    $http.get(url, {responseType: 'arraybuffer'}).then(function successCallback(response){
            console.log(response);
            var file = new Blob([response], {type: 'application/pdf'});
            console.log(file);
            var fileURL = URL.createObjectURL(file);
            $scope.loading = false;
            q.resolve(fileURL);

        },
            function errorCallback(response){
                $scope.loading = false;
                q.reject(response);
            });
           /* .success(function (response) {

            })
            .error(function (err) {

            });*/
        return q.promise;
    };
4

1 回答 1

3

您需要为实际数据而不是响应对象创建 blob。

你需要new Blob([response.data], {type: 'application/pdf'})

但请确保当您点击http://localhost:8081/api/printpdf1时获得 pdf 而不是错误。

$http.get(url, {responseType: 'arraybuffer'}).then(function successCallback(response){
            console.log(response);
            var file = new Blob([response.data], {type: 'application/pdf'});
            console.log(file);
            var fileURL = URL.createObjectURL(file);
            $scope.loading = false;
            q.resolve(fileURL);

        },
            function errorCallback(response){
                $scope.loading = false;
                q.reject(response);
            });
           /* .success(function (response) {

            })
            .error(function (err) {

            });*/
        return q.promise;
    };

于 2017-12-15T12:31:09.483 回答