Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

How to send generated PDF from javascript to Servlet.

GnanendraPonnala
Level 2
Level 2

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

 

 

 

 

 

 

1 Accepted Solution
Manjunath_K
Correct answer by
Community Advisor
Community Advisor

@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")));

View solution in original post

3 Replies
Manjunath_K
Correct answer by
Community Advisor
Community Advisor

@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")));

View solution in original post

GnanendraPonnala
Level 2
Level 2

Hi @Manjunath_K

correct me if I am wrong, I think above solution will work when we upload pdf file (any file) manually, but in my case pdf is getting generated dynamically based on user inputs on page. How can I send generated pdf to backend(java class), so that i can use it in email service api to send email with pdf.

 

Code responsible to download pdf on client-side -- 

pdfMake.createPdf(docDefinition).download();

 Instead of download, I want to send pdf to java, so that I can use it to send mail with pdf.

let me if you need more info.

Thanks

Manjunath_K
Community Advisor
Community Advisor

@GnanendraPonnala 

Instead of pdfMake.createPdf(this.docDefinition).download() try below approach, get base64 file content & decrypt base64 file content in java.

 

Javascript


const pdfDocGenerator = pdfMake.createPdf(this.docDefinition);
pdfDocGenerator.getBase64((data) => {
fileContent = data  //data to be sent in ajax request with attr name 'fileContent'
});

 

Java :

 

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