parallax / jsPDF

Client-side JavaScript PDF generation for everyone.
https://parall.ax/products/jspdf
MIT License
29.32k stars 4.68k forks source link

Regarding save file on server #1962

Closed jurjees23 closed 6 years ago

jurjees23 commented 6 years ago

Hi, Is there is any possible to save pdf file directly on server.

Thank you for submitting an issue to jsPDF. Please read carefully.

Are you using the latest version of jsPDF?

Have you tried using jspdf.debug.js?

Steps to reproduce

Ideally a link too. Try fork this http://jsbin.com/rilace/edit?html,js,output

What I saw

What I expected

Uzlopak commented 6 years ago

yes.

vinodsamy commented 5 years ago

yes.

could you please give me example. when i save to server side i got this: %PDF-1.3 %�߬� 3 0 obj <</Type /Page /Parent 1 0 R /Resources 2 0 R /MediaBox [0 0 595.28 841.89] /Contents 4 0 R

endobj 4 0 obj <<

Uzlopak commented 5 years ago

Build the jspdf with npm run-script build and check https://github.com/MrRio/jsPDF/tree/master/examples/node

vinodsamy commented 5 years ago

Build the jspdf with npm run-script build and check https://github.com/MrRio/jsPDF/tree/master/examples/node

thanks for your response but it's not what i am trying to resolve. i need to send pdf which already generated in frontend to backend(nodejs).

Here is my code

Angular Code:

var pdf = new jsPDF('p', 'pt', 'a4');

pdf.internal.scaleFactor = 2.25;

pdf.addHTML(elementToPrint, 0, 0, { pagesplit: true }, function () { // pdf.addPage() // pdf.save('FieldStack-PracticeMetrics-' + new Date() + '.pdf'); }); Note:When i output the pdf data i've got same gibberish on frontend(Angualr).

var data = new Blob([pdf.output()], { type: 'application/pdf'})

var formData = new FormData(); formData.append("pdf", data, "myfile.pdf");

var request = new XMLHttpRequest(); request.open("POST", "http://localhost:3000/caseforms"); request.send(formData);

Nodejs Backend:

var formidable = require('formidable'), util = require('util'); router.post('/', (req, res, next) => { var form = new formidable.IncomingForm();

form.parse(req, function (err, fields, files) { res.writeHead(200, { 'content-type': 'application/pdf', "Access-Control-Allow-Origin": "*" }); res.write('received upload:\n\n'); res.end(util.inspect({ fields: fields, files: files })); console.log("File received:\nName:" + files.pdf.name + "\ntype:" + files.pdf.type); });

form.on('end', function (fields, files) { let fileBinary = fs.readFileSync(this.openedFiles[0].path,'UTF-8') console.log(fileBinary) })