【问题标题】:Angular 4 show pdf in new tab with node apiAngular 4 在新选项卡中使用节点 api 显示 pdf
【发布时间】:2018-04-23 22:46:36
【问题描述】:

我的要求是使用 Nodejs api 生成 UI(角度 4 应用程序)的 pdf 视图。为此,我从 UI 中获取了全部内容并进行了一些预处理,这些预处理将发送到 nodejs api。在节点 api 中,我使用 html-pdfpackage 从收到的 html 生成 pdf。能够生成具有正确样式的 pdf,因为它出现在 UI 中。以下是我的问题

  • 将整个 UI 内容(html、样式、引导 css)传递给 Node api 的安全方法是什么。 (目前为 POC 目的作为普通字符串传递)
  • 我将如何将html-pdfpackage 生成的 pdf 流返回到 UI 以在新选项卡上显示它

html-pdf package

来自 Angular 的节点 api 调用:

  this.http.post('http://localhost:3000/api/createpdf', { 'arraybuff': data }, options)
        .catch(this.handleError)
        .subscribe(res => {

        })

当前数据是普通的 html 字符串,我通过在节点中设置 body-parser 来检索它。

【问题讨论】:

    标签: node.js angular api node-html-pdf


    【解决方案1】:

    我认为没有更好的方法可以将 HTML 传递到服务器,或者我可能不知道,但要在新标签页中打开链接,请使用下面的代码

     this.http.post('http://localhost:3000/api/createpdf', { 'arraybuff': data }, options)
    .catch(this.handleError)
    .subscribe(res => {
           var blob = new Blob([(<any>res)], { type: 'application/pdf' });
           var url = window.URL.createObjectURL(blob);
           window.open(url);
    })
    

    【讨论】:

    • 但这会创建一个blob url。那安全吗?我已经尝试过了,通过查看 url (blob:https://....) 只是犹豫不决。
    • 此 blob url 将在客户端计算机本地。由于您是在客户端计算机上生成它。我没有发现任何问题。你也可以发送一个带有有效期之类的url。
    猜你喜欢
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 2022-10-13
    • 2021-12-02
    • 1970-01-01
    • 2021-10-21
    相关资源
    最近更新 更多