node.js - 无法加载 PDF 文档 html-pdf

标签 node.js html-pdf

This是一个笨蛋链接。我使用 Node 运行pdfSample.jspdfsample.html是要转换为pdf的实际数据,generatepdf.html用于获取它。

当我运行它时,它显示一个错误无法加载 Pdf 文档。我推荐了this sample 。我没有遇到任何问题,为什么它不起作用?

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

最佳答案

您需要为实际数据而不是响应对象创建 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;
    };

关于node.js - 无法加载 PDF 文档 html-pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47750561/

相关文章:

node.js - 使用nodejs在mongodb中插入嵌入文档

node.js - 未在 windows 中设置的环境变量

javascript - 删除属性或构造仅具有所需属性的新对象更快吗?

node.js - 错误: spawn ENOTDIR html-pdf in mac

Angular 2 - HTML 和 CSS 转 PDF(作为电子邮件附件发送)

node.js - 使用 node.js 中的云 sql 代理续写连接超时

node.js - 从存储为 Node Buffer 的字节数组创建类型化数组

node.js - 如何将nodejs服务器发送的pdf数据转换为angularjs中的pdf

java - iText 7、htmlPDF 2 - DefaultFontProvider 的并行使用