Firebase 기능 (Firebase Introduction)이 있으므로 NodeJS를 사용하여 pdfkit (PDFKit)과 같은 PDfs를 만들 수 있습니다. 다음 예제는 AccountCreation (Event)에서 PDF를 생성하고 저장소에 저장 한 후 메일을 통해 사용자에게 보내는 방법을 설명합니다. 이 모든 것이 Firebase 서버에서 발생합니다. 사용 된 모든 라이브러리에 대해서는 npm을 설치하고 package.json에 포함하는 것을 잊지 마십시오. 까다로운 부분은 add the Firebase Admin SDK to your Server입니다. 또한 this이 많은 도움이되었습니다. 솔직히 말해서 이것은 몇 시간이 걸렸습니다. 그래서 자세한 내용을 물어 hestiate!
const functions = require('firebase-functions');
const admin = require("firebase-admin");
const nodemailer = require('nodemailer');
const pdfkit = require('pdfkit');
const gmailEmail = '[email protected]'
const gmailPassword = 'test123.ThisisNoTSave'
const mailTransport = nodemailer.createTransport(`smtps://${gmailEmail}:${gmailPassword}@smtp.gmail.com`);
const serviceAccount = require("./youradminsdk.json");
//Save this file to your functions Project
// Your company name to include in the emails
const APP_NAME = 'My App';
const PROJECT_ID = "google-testproject";
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: `https://${PROJECT_ID}.firebaseio.com`,
storageBucket: `${PROJECT_ID}.appspot.com`
});
var config = {
projectId: `${PROJECT_ID}`,
keyFilename: './youradminsdk.json'
};
const storage = require('@google-cloud/storage')(config);
const datastore= require('@google-cloud/datastore')(config);
// [START onCreateTrigger]
exports.sendPDFMail = functions.auth.user().onCreate(event => {
// [END onCreateTrigger]
// [START eventAttributes]
const doc = new pdfkit;
const user = event.data; // The Firebase user.
const email = user.email; // The email of the user.
const displayName = user.displayName; // The display name of the user.
// [END eventAttributes]
const bucket = storage.bucket(`${PROJECT_ID}.appspot.com`);
console.log(bucket);
const filename = Date.now() + 'output.pdf';
const file = bucket.file(filename);
const stream = file.createWriteStream({resumable: false});
// Pipe its output to the bucket
doc.pipe(stream);
doc.fontSize(25).text('Some text with an embedded font!', 100, 100);
doc.end();
stream.on('finish', function() {
return sendPDFMail(email, displayName, doc);
});
stream.on('error', function(err) {
console.log(err);
});
})
;
// [END sendPDFMail]
// Sends a welcome email to the given user.
function sendPDFMail(email, displayName, doc) {
const mailOptions = {
from: '"MyCompany" <[email protected]>',
to: email,
};
mailOptions.subject = `Welcome to ${APP_NAME}!`;
mailOptions.text = `Hey ${displayName}!, Welcome to ${APP_NAME}. I hope you will enjoy our service.`;
mailOptions.attachments = [{filename: 'meinPdf.pdf', content: doc, contentType: 'application/pdf' }];
return mailTransport.sendMail(mailOptions).then(() => {
console.log('New welcome email sent to:', email);
});
}
Firebase에는 PDF를 생성하는 기본 제공 기능이 없으므로 클라이언트에서 생성하거나 서버를 설정해야합니다. 특정 라이브러리를 추천하는 것은 오프 토픽입니다. Stack Overflow –