Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to send generated PDF from javascript to Servlet.

Avatar

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

Avatar

Correct answer by
Level 8

@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

Avatar

Correct answer by
Level 8

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

Avatar

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

Avatar

Level 8

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