【问题标题】:How to get and display pdf file from Amazon S3 with Node.js?如何使用 Node.js 从 Amazon S3 获取和显示 pdf 文件?
【发布时间】:2016-09-09 12:31:07
【问题描述】:

我正在使用此代码从 Amazon S3 获取数据:

var s3 = new aws.S3({
    access_key_id: system.config.amazonS3.accessKey,
    secret_access_key: system.config.amazonS3.secretAccessKey
});
var params = {Bucket: 'bucket', Key: 'b9ca6cca65f34bf60d3ffalse.pdf'};
//    var file = require('fs').createWriteStream('./file.pdf');
    s3.getObject(params, function (err, data) {
        if (err)
            console.log(err, err.stack); // an error occurred
        else
            console.log(data);           // successful response
});

它会返回如下内容:

{ AcceptRanges: 'bytes',
  LastModified: 'Fri, 09 Sep 2016 12:08:18 GMT',
  ContentLength: '688609',
  ETag: '"3063770da82053b9c97fe5f2b7b923ac"',
  ContentType: 'application/octet-stream',
  Metadata: { fieldname: 'files' },
  Body: <Buffer 25 50 44 46 2d 31 2e 36 0d 25 e2 e3 cf d3 0d 0a 32 37 20 30 20 6f 62 6a 0d 3c 3c 2f 46 69 6c 74 65 72 2f 46 6c 61 74 65 44 65 63 6f 64 65 2f 46 69 72 ... > }

如何创建 pdf 文件来显示数据(响应)?

【问题讨论】:

标签: node.js amazon-web-services pdf amazon-s3


【解决方案1】:

您可以使用jsPDF()生成PDF

   var doc = new jsPDF();
   for(var attribute in data){
       doc.text(35, 25, attribute +": " + data[attribute]);
   }
   doc.save('DataOutput.pdf');

【讨论】:

    【解决方案2】:

    响应的正文部分包含作为缓冲区的 PDF 文件内容。如果要将其转换为文本,则需要将其转换为 Unit8 array。然后将该 Unit8 数组传递给 Node.js 中的任何 PDF 阅读器包。要获得最佳结果,您可以使用PDF.JS

    s3Client.getObject({Bucket: 'BUCKET_NAME', Key: 'FILE_NAME'}, function (err, data) {
    if (err) cb(err);
    getText(data.Body).then(function (text) { //data.Body will contain the content as buffer.
    cb(null, {item: text}); //text will contain the text corresponding to the buffer.
    }).catch(function(e){
    cb('something went wrong !');
    });
    });
    

    在 getText() 函数中,我使用 PDFJS 从 PDF 文件中提取文本。为了在后端使用 PDF.jS,您应该使用 const pdfjsLib = require('pdfjs-dist');

    要求库 pdfjs-dist
    //getText() function
    function getText(typedarray) {
    
        //PDFJS should be able to read this typedarray content
        var pdf = pdfjsLib.getDocument(new Uint8Array(typedarray));
        return pdf.then(function (pdf) {
            // get all pages text
            var maxPages = pdf._pdfInfo.numPages;
            var countPromises = [];
            // collecting all page promises
            for (var j = 1; j <= maxPages; j++) {
                var page = pdf.getPage(j);
    
                countPromises.push(page.then(function (page) {
                    // add page promise
                    var textContent = page.getTextContent();
    
                    return textContent.then(function (text) {
    
                        var pageContent = text.items.map(function (s) {
                            return s.str;
                        }).join(' '); // value page text
                        pageContent = pageContent.replace(/</g, '');
                        return ((page.pageNumber > 1) ? (' #pagebreak# ' + pageContent) : pageContent);
                    });
                }));
            }
            // Wait for all pages and join text
            return Promise.all(countPromises).then(function (texts) {
                return texts.join('');
            });
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      • 2011-12-17
      相关资源
      最近更新 更多