Using jspdf I have generated pdf
file on the client side (AngularJS). I am able to successfully download the file. Now I have another function to send the pdf via email
to user email address.
Problem:
When user clicks on send email
button, the pdf which I created using jspdf
should be uploaded to server and there I should be able to attach the pdf file which I got from client to email. Is it possible to do the same? I am not sure whether we can do this or not.
Can we send the doc object in var doc = new jsPDF('p', 'pt');
to nodejs
then render it and finally attach to the email?
If the above task is not possible then let me know about other possibilities.
P.S: I am using nodemailer for sending emails.
I have forked a sample code for both client and server side code, tested and working perfectly. Please modify according to your need.
Server side: Hosted on cloud9 for testing - Hence it takes the public ip and port provided by the provider via process
object. Change the listener according to your hosting environment.
Note: Please read the inline comment for better understanding
var express = require('express');
var app = express();
var fs = require('fs');
var bodyParser = require('body-parser');
var formidable = require('formidable'),
form = new formidable.IncomingForm();
app.post("/", function(req, res, next) {
form.parse(req, function(err, fields, files) {
console.log("File received:\nName:"+files.pdf.name+"\ntype:"+files.pdf.type);
});
form.on('end', function() {
/* this.openedFiles[0].path -- object Contains the path to the file uploaded
------- Use NodeMailer to process this file or attach to the mail-----------------*/
console.log("PDF raw data:"+ fs.readFileSync(this.openedFiles[0].path, "utf8"));
res.status(200).send("thank you");
});
})
.listen(process.env.PORT, process.env.IP, function() {
console.log('Server app listening');
});
Client Side: Fiddler
Note: I didn't paste imgData
as SO has character limit. Refer the fiddler link for the client code. Change the request URL to your server. Client side uses Blob API which is an HTML5 standard. so test it on HTML5 compliant browsers.
var imgData = [[**Copy the same image provided by JSpdf example. Check the fiddler for complete code**]]
var doc = new jsPDF();
doc.setFontSize(40);
doc.text(35, 25, "Octonyan loves jsPDF");
doc.addImage(imgData, 'JPEG', 15, 40, 180, 180);
var data = new Blob([doc.output()], {
type: 'application/pdf'
});
var formData = new FormData();
formData.append("pdf", data, "myfile.pdf");
var request = new XMLHttpRequest();
request.open("POST", "https://saltjs-nirus.c9.io"); // Change to your server
request.send(formData);
References:
- https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects
- https://github.com/felixge/node-formidable
- http://mrrio.github.io/jsPDF/ (Example reference)
- https://developer.mozilla.org/en/docs/Web/API/Blob