簡體   English   中英

如何在 NodeJS 中的 MongoDB 中保存和檢索 pdf 文件

[英]How to Save and Retrieve a pdf file in MongoDB in NodeJS

我有一個問題,我將使用 Express 在我的 Node.js 服務器后端創建的一個小的 pdf 文件(~128KB)保存到 Mongodb 中的文檔中。 我沒有使用 Mongo GridFS,因為文件將始終低於 16MB 限制。 該集合具有以下架構:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const ReportsSchema = new Schema({
  ReportFileData: Buffer,
  Cert_objid: { type: Schema.Types.ObjectId, ref: 'Certs' },
  Report_Type: String,
  Note: String,
  Dau_objID: { type: Schema.Types.ObjectId, ref: 'Dau' },
  Pau_objID: { type: Schema.Types.ObjectId, ref: 'Pau' }
});

module.exports = Reports = mongoose.model('Reports', ReportsSchema);

我使用以下代碼創建pdf文件並保存。 我沒有包含 docDefinition 因為如果它直接保存到服務器,它會生成正確的文件。

const PdfPrinter = require('pdfmake/src/printer');
const path = require('path');
const moment = require('moment');

const Reports = require('../../models/Reports');  // Mongoose Schema

const createFAReport = data => {
    docDefinition...

createPdfBinary(docDefinition, binary => {
  const rpt = new Reports({
    ReportFileData: binary,
    Cert_objid: data._id,
    Report_Type: 'Water Use Report',
    Note: 'Testing 123'
  });

  rpt.save(err => {
    if (err) throw err;
    });
});

const createPdfBinary = (pdfDoc, callback) => {
  const fonts = {
    Roboto: {
    normal: path.join(__dirname, '../../', '/fonts/Roboto-Regular.ttf'),
    bold: path.join(__dirname, '../../', '/fonts/Roboto-Medium.ttf'),
    italics: path.join(__dirname, '../../', '/fonts/Roboto-Italic.ttf'),
    bolditalics: path.join(__dirname, '../../', '/fonts/Roboto-MediumItalic.ttf')
    }
  };

  const printer = new PdfPrinter(fonts);
  const doc = printer.createPdfKitDocument(pdfDoc);
  const chunks = [];
  let result;

  doc.on('data', function(chunk) {
    chunks.push(chunk);
  });
  doc.on('end', function() {
    result = Buffer.concat(chunks);
    callback('data:application/pdf;base64,' + result.toString('base64'));
  });
  doc.end();
};

然后從 MongoDB 檢索編碼文檔並將其寫入本地文件以進行測試,我使用了以下代碼(請注意,聚合是獲取檢索正確報告所需的一些關聯字段):

router.get('/getReport', passport.authenticate('jwt', { session: false }), (req, res) => {
  Certs.aggregate([
    {
      $match: {
        Cert_ID: '1578'
      }
    },
    {
      $lookup: {
        from: 'reports',
        localField: '_id',
        foreignField: 'Cert_objid',
        as: 'rpt'
      }
    },
    {
      $unwind: {
        path: '$rpt',
        includeArrayIndex: '<<string>>',
        preserveNullAndEmptyArrays: false
      }
    }
  ]).then(result => {
    result.map(rslt => {
      console.log(rslt.Cert_ID);
      res.json({ msg: 'Got the report.' });

      const fullfilePath = path.join(__dirname, '../../', '/public/pdffiles/', `1578.pdf`
      );

      fs.writeFile(fullfilePath, rslt.rpt.ReportFileData, 'base64', () => {
        console.log('File Saved.');
       });
     });
   });
 });

一切似乎都很好,除非我打開文件時收到文件損壞的錯誤。 我想知道將它保存到“base64”是否有問題,或者 MongoDB 的數據類型是否有問題。 數據類型是緩沖區,所以你會把它作為緩沖區來檢索嗎? 任何幫助,將不勝感激。

我不建議將 pdf 或圖像直接寫入數據庫。 這里有一些關於為什么在 DB 中存儲圖像的信息- 是還是不是?

您通常保存文件名並將文件存儲在文件系統上,您自己的或更可擴展的選項將類似於 S3。

這是一個可以幫助您的模塊https://www.npmjs.com/package/formidable如果您計划推出自己的模塊,您仍然可以從中獲得一些靈感。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM