2

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.

2
  • You can send files with AngularJS using FormData API Commented Dec 30, 2015 at 7:51
  • @rahilwazir But will the file be in the same format .. I mean PDF in this case after sending to server? Commented Dec 30, 2015 at 8:11

1 Answer 1

2

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:

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

4 Comments

Be sure to check on modern browsers that implements Blob API
Sry for the late response. I am having some issue with email sending. File upload seems to be working though there is some problem with image rendering in the pdf. The image is getting blurred in black & white. Other than that rest of the data is intact. An idea about this problem?
Thats totally a different question thread. You can check the pdf file locally, if it has the same problem better raise an issue here: github.com/MrRio/jsPDF/issues . Attach the image and the code you tried to render, so that author of the lib can address it.
Okay. Thanks for the response .. Will check that issue.

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.