How to send generated PDF from javascript to Servlet. | Community
Skip to main content
GnanendraPonnala
Level 2
September 23, 2020
Solved

How to send generated PDF from javascript to Servlet.

  • September 23, 2020
  • 1 reply
  • 2862 views

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

 

 

 

 

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Manjunath_K

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

1 reply

Manjunath_K
Manjunath_KAccepted solution
Level 7
September 23, 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")));

GnanendraPonnala
Level 2
September 24, 2020

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