【问题标题】:How to generate a PDF document from firebase cloud functions如何从 Firebase 云函数生成 PDF 文档
【发布时间】:2021-05-20 12:58:45
【问题描述】:

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

以下是我到目前为止的代码,但没有创建/生成任何内容。运行该函数时,我只是在 Firebase 日志上收到“崩溃”响应:

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()
      
});

这只是一个测试 sn-p,甚至可以查看 PDFmake 是如何工作的,我似乎无法让它工作。可以通过 http 请求在本地生成和存储 pdf 吗?

【问题讨论】:

    标签: firebase google-cloud-firestore google-cloud-functions pdfkit pdfmake


    【解决方案1】:

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

        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();
    });
    });
    

    【讨论】:

      猜你喜欢
      • 2014-09-09
      • 1970-01-01
      • 2020-10-25
      • 2019-02-18
      • 2012-03-06
      • 2012-05-12
      • 2017-12-08
      • 1970-01-01
      • 2011-08-29
      相关资源
      最近更新 更多