• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sending PDF as Input Stream

 
Aggarwal Arpit
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

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.


How can I do it?

Thanks in advance for help.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have the absolute path of the PDF? If so you could create a FileInputStream and send to the method. Check FileInputStream(File)
 
Aggarwal Arpit
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply John, but in my code, pdf is not saved anywhere.
pdf is written in an response output stream.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a ByteArrayOutputStream to write the PDF data into a byte[], and from that create a ByteArrayInputStream for attaching to the mail.
 
Aggarwal Arpit
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf, for that I first need to save file and then read it using pdf reader?
 
Paul Clapham
Sheriff
Posts: 21863
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aggarwal Arpit wrote:Hi Ulf, for that I first need to save file and then read it using pdf reader?

No
 
Aggarwal Arpit
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Aggarwal Arpit
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,
Can you please help me with an example?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aggarwal Arpit
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf, Thanks a lot for your valuable suggestion.
Now another issue I am facing is of conversion of ServletOutputStream to Input Stream.

Now What I am doing is ,




But it is now thrrowing an exception:



Please help me to resolve this issue.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think lines 8-15 are accomplishing? The PDF has already been sent to the ServletOutputStream in lines 5 and 7; if you try to do it again, there'll be problems like the one you encountered.
 
Aggarwal Arpit
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,

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.

Thanks in Advance
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aggarwal Arpit
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf, Does it mean, once response.getOutputStream() is passed to createPDF() method, then data is no more in response?

Can you please explain me with a code how to render it into byte[]?


 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what my 3rd post was all about; have you read the javadocs of those classes yet? They don't have many methods, and it should be relatively straightforward to get started with them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!