代码之家  ›  专栏  ›  技术社区  ›  Sourabh Bhutani

如何在javascript中将base64文件重构为pdf文件?

  •  0
  • Sourabh Bhutani  · 技术社区  · 7 年前

    在nodejs中,我从api获得响应

    {
      "file": "PHN0eWxlPnRlRrU3VRbUNDJyAvPjwvcD4K",
      "mime_type": "text/html",
      "document_type": "shippingLabel"
    }
    

    要重建文件,需要对来自节点的数据进行base64解码,并根据mime_类型进行解释。

    帮我把文件拿进来 .pdf 然后保存到目录。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Patrick Roberts Benjamin Gruenbaum    7 年前

    使用 fs.writeFileSync(file, data[, options]) :

    const fs = require('fs');
    
    // get your response somehow...
    const response = {
      file: 'PHN0eWxlPnRlRrU3VRbUNDJyAvPjwvcD4K',
      mime_type: 'text/html',
      document_type: 'shippingLabel'
    };
    // LUT for MIME type to extension
    const ext = {
      'text/html': 'html',
      // ...
    }
    
    // save to shippingLabel.html
    fs.writeFileSync(`${response.document_type}.${ext[response.mime_type]}`, response.file, 'base64');