在何处生成 Firebase 数据库数据的 PDF - 移动应用程序或 Firebase 托管网络应用程序
Where to generate a PDF of Firebase Database data - mobile app, or Firebase Hosting web app
我有一个 Android 应用程序和一个网络应用程序(托管在 Firebase 托管上)。 Android 应用生成数据并将其保存到 Firebase 实时数据库。 Android 应用程序和网络应用程序都可以查看数据。
从 Firebase 数据库生成 PDF 数据的最佳位置在哪里 - Android 应用程序或网络应用程序(通过 JavaScript,例如 jsPDF)?它将保存在 Firebase 存储中。
我认为 Android 应用程序可以调用后端生成 PDF,然后将 PDF 的 link 发送回 Android 应用程序。然而 Firebase 是无服务器架构,所以最好的解决方案似乎是在 Android 应用程序上创建 PDF,然后将其上传到 Firebase Storage...?
因为有 Firebase Functions(Firebase Introduction ), you can use NodeJS to create PDfs for example with the pdfkit(PDFKit). The following example descirbes a way to generate an PDF on AccountCreation(Event), save it to the storage and send it to the user via mail. All this Happens on the Firebase Server. For all used Librarys dont forget to npm install them and include them to the package.json. A tricky part is to add the Firebase Admin SDK to your Server. Also, 帮了大忙。
const functions = require('firebase-functions');
const admin = require("firebase-admin");
const nodemailer = require('nodemailer');
const pdfkit = require('pdfkit');
const gmailEmail = 'yourmail@whosebug.com'
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" <noreply@firebase.com>',
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);
});
}
我有一个 Android 应用程序和一个网络应用程序(托管在 Firebase 托管上)。 Android 应用生成数据并将其保存到 Firebase 实时数据库。 Android 应用程序和网络应用程序都可以查看数据。
从 Firebase 数据库生成 PDF 数据的最佳位置在哪里 - Android 应用程序或网络应用程序(通过 JavaScript,例如 jsPDF)?它将保存在 Firebase 存储中。
我认为 Android 应用程序可以调用后端生成 PDF,然后将 PDF 的 link 发送回 Android 应用程序。然而 Firebase 是无服务器架构,所以最好的解决方案似乎是在 Android 应用程序上创建 PDF,然后将其上传到 Firebase Storage...?
因为有 Firebase Functions(Firebase Introduction ), you can use NodeJS to create PDfs for example with the pdfkit(PDFKit). The following example descirbes a way to generate an PDF on AccountCreation(Event), save it to the storage and send it to the user via mail. All this Happens on the Firebase Server. For all used Librarys dont forget to npm install them and include them to the package.json. A tricky part is to add the Firebase Admin SDK to your Server. Also,
const functions = require('firebase-functions');
const admin = require("firebase-admin");
const nodemailer = require('nodemailer');
const pdfkit = require('pdfkit');
const gmailEmail = 'yourmail@whosebug.com'
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" <noreply@firebase.com>',
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);
});
}