如何从 firebase 云函数生成 PDF 文档

How to generate a PDF document from firebase cloud functions

我对 firebase 云功能还很陌生,我正在尝试生成并保存 PDF 文档(在本地、在 firebase 存储中,甚至在浏览器中,然后通过 nodemailer 发送给用户)。

下面是我目前拥有的代码,但没有创建/生成任何内容。当函数为 运行:

时,我在 Firebase 日志中收到 'crash' 响应
const functions = require("firebase-functions");
const admin = require('firebase-admin');
const nodemailer = require('nodemailer');
const cors = require('cors')({origin: true});
var pdf = require('pdfmake');

var printer = new pdf();
var fs = require('fs');
var BUCKET = 'gs://mpca-19527.appspot.com'
admin.initializeApp();

exports.createWill = functions.https.onRequest((req, res) => {
    var docDefinition = {
        pageSize: 'A4',
        pageMargins: [ 40, 60, 40, 60 ],
        content: [
        'First paragraph',
        'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
        ]
        };

      var pdfDoc = printer.createPdfKitDocument(docDefinition);
      let file_name = 'facture.pdf';
      const myPdfFile = storage.bucket(BUCKET).file(file_name);
      pdfDoc.pipe(fs.createWriteStream('document.pdf'));    
      pdfDoc.end()
      
});

这只是一个测试片段,用于查看 PDFmake 的工作原理,但我似乎无法让它工作。能否通过 http 请求在本地生成和存储 pdf?

所以我在这里发布我的代码以防其他人需要它:

    const functions = require("firebase-functions");
const admin = require('firebase-admin');
const nodemailer = require('nodemailer');
const {Storage} = require('@google-cloud/storage');
admin.initializeApp();
const PdfPrinter = require('pdfmake/src/printer');
const BUCKET = 'INSERTLINK HERE FROM FIREBASE.appspot.com'

//手动创建了一个新的字体文件夹

const fonts = {
    Roboto: {
      normal: 'fonts/Roboto-Regular.ttf',
      bold: 'fonts/Roboto-Medium.ttf',
      bolditalics: 'fonts/Roboto-MediumItalic.ttf'
    }
  };



const printer = new PdfPrinter(fonts);
    const fs = require('fs'); 
    const storage = new Storage({
        projectId: 'INSERT YOUR PROJECT ID' });
        const cors = require('cors')({origin: true});

    let transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: 'INSERTEMAIL@gmail.com',
            pass: 'INSERT PASSWORD'
        }
    });

exports.createWill = functions.https.onRequest((req, res) => {
    
var docDefinition = {
    content: [
        'First paragraph',
        'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines',
        
    ]
};

      var pdfDoc = printer.createPdfKitDocument(docDefinition);   
      let file_name = 'facture.pdf';
    const myPdfFile = storage.bucket(BUCKET).file(file_name);
    pdfDoc.pipe(myPdfFile.createWriteStream())
    .on('finish', function (){
      cors(req, res, () => {
        // getting dest email by query string
        const dest = 'INSERT DESTINATION EMAIL@gmail.com';
        const mailOptions = {
            from: 'NAME <MY EMAIL@gmail.com>', 
            to: dest,
            subject: 'I\'M A PICKLE!!!', 
            html: `<p style="font-size: 16px;">Pickle Riiiiiiiiiiiiiiiick!!</p>
                <br />`,
                attachments: [
                    {   
                        filename: 'facture.pdf',
                        contentType: 'application/pdf',
                        content: myPdfFile.createReadStream()
                    }
                ]  
        };
  
        // returning result
        return transporter.sendMail(mailOptions, (erro, info) => {
            if(erro){
                return res.send(erro.toString());
            }
            return res.send('Sent');
        });
    });  
    })
    .on('error', function(err){
      console.log('Error during the wirtestream operation in the new file');
     // res.send('Error: something goes wrong ! '+ err);
    });
    
    pdfDoc.end();
});
});