In my code pdf is generated with dynamic contents using iText
Now I need to send this pdf as an attachment with a e-mail. For sending e-mail I am using Java mail.
In Java mail tjere is an option to add attachment, but this method is accepting InputStream as an arguement.
Aggarwal Arpit wrote:Now I need to send this pdf as an attachment with a e-mail.
Does that mean that you need to modify the code you posted so that it sends the PDF in e-mail as well as sending it as the response? Or so that it sends the PDF in e-mail instead of sending it as the response? Or something else?
Hi Paul, Iwant to keep this functionality as it is.
Now what I need is I am having another java class which is sending email, so I need to send this pdf as an attachment with that mail.
In class which is sending email there is an addAttachment() function but this function accepts argument as InputStrream and String
So, now what I thought either sending of this pdf as InputStream to this addAttachment method or saving this pdf file in a temporary location and then sending as a attachment to email.
But for both I am not getting how to proceed. any help would be appreciable:)
There isn't much to it - use a ByteArrayOutputStream as parameter to the createPDF method, and then create a ByteArrayInputStream from the underlying byte. That you can then pass to the addAttachment method. Have you read the javadocs of the ByteArrayOutputStream and ByteArrayInputStream classes yet? That's where you should start if you haven't used them before.
You are right that pdf is already created.
What I am doing is after creating pdf from OutputStream, I am trying to write that output stream to Byte Array and then making InputStream to read that Byte Array.Because i have to pass data in responseOutputStream as input to another method which is accepting as InputStream.
In doing this, I am getting null in InputStream. Here is my code
Please help me to write OutputStream in aByte Array and reading that array to inputStream.
Once you're written the PDF data to the servlet response there's no way to get the data from it. If you need the PDF for more than one purpose, render it into a byte (by way of ByteArrayOutputStream in line 4), and then use that byte as the basis for both the email and the servlet response.