How to send generated PDF from javascript to Servlet.

GnanendraPonnala

23-09-2020

Hello Team,

I have generated pdf based user inputs through javascript using pdfmake library, now i have to send email with generated pdf to customers. I am able send mail with pdf using smpt.js library but due to some security concerns its not recommended. Now I am thinking to send/post the generated pdf to backend (servlet class ) & send using acs commons email api.

 

Tried with below scenarious,

1. I tried sending pdf as Base64 from js & decode the same in servlet using java.util.Base64. getDecoder.decode(String)  - getting Illegal exception.

2. attaching pdf object to formData() object (as key- value) sending to servlet through ajax, still getting data as empty.

 

Can please helpn me is there any to achieve this.

 

Thanks in Advance

 

 

 

 

 

 

Accepted Solutions (1)

Accepted Solutions (1)

Manjunathh

23-09-2020

@GnanendraPonnala 

Please try below code & it will not throw any error in java while decrypting the base64 encrypted file content.

 

Javascript

 

var file = $('#file-input')[0].files[0];
var reader = new FileReader();
var fileContent;    //data to be sent in ajax request with attr name 'fileContent'

 

reader.onload = function(readerEvt, test){
        fileContent = btoa(readerEvt.target.result);
}

reader.readAsBinaryString(file);

 

 

Java :

 

String decryptedFileContent = new String(Base64.decodeBase64(request.getParameter("fileContent")));

Answers (0)