0

I have a customer detail form page in which customer will enter his/her detail and click submit button. After clicking, data is submitting in mongodb using node.js. Now I want to generate the PDF file of the entered data at the same time when click on submit button.

Can anyone suggest me how to generate PDF file when click on button.

2
  • Use command 'window.print()' Commented Jul 12, 2016 at 5:04
  • I am not asking for print the page... I want to generate pdf file in just a click. Commented Jul 12, 2016 at 5:14

1 Answer 1

2

You can use PDFKit if you want to make your own formatting or node-html-pdf if you just need to convert your HTML into PDF

Sign up to request clarification or add additional context in comments.

4 Comments

please check my code , Its not working router.post('/create', function(req, res, next){ var PDFDocument=require('pdfkit'); var doc=new PDFDocument(); var path=require('./public/pdf'); var filename=req.body.filename; var content=req.body.content; doc.y=320; doc.text(content, 100, 100); doc.write(path.resolve('.')+'/PDF/'+filename+'.pdf'); res.download(path.resolve('.')+'/PDF/'+filename+'.pdf'); doc.end(); });
Did you try to run example from GitHub? What is require('./public/pdf');? Use createWriteStream instead of write. Keep in mind, that it is async operation and you must wait while it finished. Also you should finalize PDF before sending it to client with res.download
Now I am writing this- var PDF = require('pdfkit'); var fs = require('fs'); var text = 'Candidate Name:'+req.body.filename; doc = new PDF(); doc.pipe(fs.createWriteStream('filee.pdf')); doc.text(text, 20, 20); doc.end(); pdf is creating successfully. now problem is that when I create again and again the pdf is overwriting with previous one. any solution?
Write files with different names. And I think it would be better if you will write them into specific directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.