我有一个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应用程序中,您可以使用file downloader将文件直接下载到应用程序,使用以下内容:
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)
);
您可以找到有关如何为该here设置端点的详细信息。
我使用谷歌云平台存储nodeJS生成的pdf。你可以按照下一篇文章来做:https://mzmuse.com/blog/how-to-upload-to-firebase-storage-in-node https://github.com/googleapis/google-cloud-node/issues/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给出的最后一个答案。