Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

Pdfmake in node.js to download pdf to flutter application

So what I exactly want to do is, Generate a pdf for an order in my order management system.

I am using node.js as the backend and flutter as the front end.

What I had in my mind is: Generate the pdf and store it on the server, return the link to the pdf to the flutter app and then download the pdf, but I don't want to store the pdf on the server, so when my flutter app calls the generatePdf endpoint using GET method, how can I send the pdf directly? Do I need to use res.download ? I am new to node.js and cannot understand the flow for downloading the file. Also, how do I need to change the request in my flutter app for the same?

Note: I am using PDFMake to generate the pdf.

question from:https://stackoverflow.com/questions/65860070/pdfmake-in-node-js-to-download-pdf-to-flutter-application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Pdfmake has an option to 'pipe' the generated doc directly to the client. So, if you pass the response object to the code that generates the pdf, it will be something like:

var printer = new PdfPrinter(fonts);
//Assuming pdf is your generated PDF:
doc = printer.createPdfKitDocument(pdf);
res.setHeader('Content-type', 'application/pdf');
res.setHeader('Content-disposition', 'inline; filename="nice.pdf"');
doc.pipe(res);
doc.end();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...