[英]How to send a pdf file from node/express app to Flutter app?
我有一個nodejs代碼,當我在post請求中發送2個參數時,我可以從瀏覽器下載pdf文件:fname和lname。
我在后端使用express和pdfmake包。
const express = require('express');
const router = express.Router();
const pdfMake = require('../pdfmake/pdfmake');
const vfsFonts = require('../pdfmake/vfs_fonts');
pdfMake.vfs = vfsFonts.pdfMake.vfs;
router.post('/pdf', (req, res, next) => {
//res.send('PDF');
const fname = req.body.fname;
const lname = req.body.lname;
var documentDefinition = {
content: [{
image: 'data:image/png;base64 more code',
width: 200,
alignment: 'center'
},
{ text: '\nGrupo de inspecciones predictivas', style: 'header', alignment: 'center' },
{ text: 'Reporte de inspección\n\n', style: 'subheader', alignment: 'center' },
'El siguiente reporte tiene como objetivo describir los resultados encontrados a partir de la inspección en la fecha específica.',
{ text: 'Resumen del reporte', style: 'subheader' },
{
style: 'tableExample',
table: {
widths: ['*', 'auto'],
body: [
['Inspector:', { text: `${ fname }`, noWrap: true }],
['Flota:', { text: '', noWrap: true }],
['Número de flota:', { text: '', noWrap: true }],
['Técnica:', { text: '', noWrap: true }],
['Fecha de inicio:', { text: '', noWrap: true }],
]
}
},
],
styles: {
header: {
fontSize: 18,
bold: true,
margin: [0, 0, 0, 10]
},
subheader: {
fontSize: 16,
bold: true,
margin: [0, 10, 0, 5]
},
tableExample: {
margin: [0, 5, 0, 15]
},
tableHeader: {
bold: true,
fontSize: 13,
color: 'black'
}
},
defaultStyle: {
// alignment: 'justify'
}
};
const pdfDoc = pdfMake.createPdf(documentDefinition);
pdfDoc.getBase64((data) => {
res.writeHead(200, {
'Content-Type': 'application/pdf',
'Content-Disposition': 'attachment;filename="filename.pdf"'
});
const download = Buffer.from(data.toString('utf-8'), 'base64');
res.end(download);
});
});
但是,正如我上面提到的,這段代碼顯然只返回de pdf到瀏覽器。
我需要在Flutter應用程序中將pdf文件下載到Android / IOS存儲。
完成此任務的一個好方法是創建一個直接返回文件的簡單URL端點。 在您的flutter應用程序中,您可以使用文件下載程序使用以下內容將文件直接下載到應用程序:
final taskId = await FlutterDownloader.enqueue(
url: 'your download link',
savedDir: 'the path of directory where you want to save downloaded files',
showNotification: true, // show download progress in status bar (for Android)
openFileFromNotification: true, // click on notification to open downloaded file (for Android)
);
您可以在此處找到有關如何為此設置端點的詳細信息。
我使用谷歌雲平台存儲nodeJS生成的pdf。 您可以按照下一篇文章進行操作: https : //mzmuse.com/blog/how-to-upload-to-firebase-storage-in-node https://github.com/googleapis/google-cloud-node/問題/ 2334
pdfDoc.getBase64((data) => {
const keyFilename = "./myGoogleKey.json";
const projectId = "my-name-project";
const bucketName = `${projectId}.appspot.com`;
var GoogleCloudStorage = require('@google-cloud/storage');
const gcs = GoogleCloudStorage({
projectId,
keyFilename
});
const bucket = gcs.bucket(bucketName);
const gcsname = 'reporte.pdf';
const file = bucket.file(gcsname);
var buff = Buffer.from(data.toString('utf-8'), 'base64');
const stream = file.createWriteStream({
metadata: {
contentType: 'application/pdf'
}
});
stream.on('error', (err) => {
console.log(err);
});
stream.on('finish', () => {
console.log(gcsname);
});
stream.end(buff);
res.status(200).send('Succesfully.');
});
});
這將生成一個URL,您可以按照上面Esh給出的最后一個答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.