struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Karr, David" <david.k...@wamu.net>
Subject RE: PDF & Email
Date Thu, 02 Sep 2004 13:17:05 GMT
The key to avoiding the use of a disk file is to use a
"ByteArrayDataSource" in the DataHandler for the attachment.  A class
like this wasn't provided out of the box, so you have to provide it.
I'll include the one I have.  It isn't optimized, in case that's an
issue for you.

------------------
    /**
     * This class is used along with the DataHandler to attach a byte
array as
     * an email attachment.
     */
    public static class ByteArrayDataSource implements DataSource
    {
        private   InputStream inputStream;
        private   byte[]      data;
        private   boolean     dataLoaded  = false;
        private   String      contentType;
        private   String      name;

        public ByteArrayDataSource(InputStream  inputStream,
                                   String       contentType,
                                   String       name)
        {
            setContentType(contentType);
            setName(name);
            setInputStream(inputStream);
        }

        public ByteArrayDataSource(byte[] data,
                                   String contentType,
                                   String name)
        {
            setContentType(contentType);
            setName(name);
            setData(data);
        }

        public ByteArrayDataSource(String data,
                                   String contentType,
                                   String name)
        {
            setContentType(contentType);
            setName(name);
            setData(data.getBytes());
        }

        void loadData()
            throws IOException
        {
            int   available   = 0;
            while ((available = inputStream.available()) > 0)
            {
                byte[] newdata  = new byte[available];

                if (data == null)
                    data   = newdata;
                else
                {
                    byte[] olddata  = data;
                    data   = new byte[olddata.length + newdata.length];
                    System.arraycopy(olddata, 0, data, 0,
olddata.length);
                    System.arraycopy(newdata, 0, data, olddata.length,
                                     newdata.length);
                }
            }
            
            data  = new byte[inputStream.available()];
            int   readBytes   = inputStream.read(data);

            setData(data);
        }
        
        public void setInputStream(InputStream inputStream)
        { this.inputStream  = inputStream; }
        public void setContentType(String contentType)
        { this.contentType  = contentType; }
        public void setName(String name)
        { this.name  = name; }

        public void setData(byte[] data)
        {
            this.data  = data;
            dataLoaded  = true;
        }
        
        public InputStream getInputStream()
            throws IOException
        {
            if (!dataLoaded)
                loadData();
            return (new ByteArrayInputStream(data));
        }

        public OutputStream getOutputStream()
            throws IOException
        {
            throw new
                UnsupportedOperationException("This is a read-only
DataSource");
        }

        public String getContentType() { return (contentType); }
        public String getName() { return (name); }
    }
------------------

> -----Original Message-----
> From: CRANFORD, CHRIS [mailto:chris.cranford@setechusa.com] 
> Sent: Thursday, September 02, 2004 5:25 AM
> To: 'Struts Users Mailing List'
> Subject: RE: PDF & Email
> 
> 
> What I'm trying to get at is a way to generate the PDF in 
> memory and attachment it that way from memory without 
> actually having to use a physical disk file.  Basically we 
> have a form that gathers information about a request.  That 
> form in turn is converted to the message's body content but 
> we also want to render a PDF attachment that is an approval 
> form that can be printed by the recipient, signed and then 
> filled.  I'd like to do all this without any physical disk 
> I/O files.  Possible?
> 
> -----Original Message-----
> From: Matthias Wessendorf [mailto:mailings@matthias-wessendorf.de] 
> Sent: Thursday, September 02, 2004 8:25 AM
> To: 'Struts Users Mailing List'
> Subject: RE: PDF & Email
> 
> 
> or like this:
> 
> 
> 
> // Create the attachment
> EmailAttachment attachment = new EmailAttachment(); 
> attachment.setPath("mypictures/john.jpg");
> attachment.setDisposition(EmailAttachment.ATTACHMENT);
> attachment.setDescription("Picture of John"); 
> attachment.setName("John");
> 
> // Create the email message
> MultiPartEmail email = new MultiPartEmail(); 
> email.setHostName("mail.myserver.com");
> email.addTo("jdoe@somewhere.org", "John Doe"); 
> email.setFrom("me@apache.org", "Me"); email.setSubject("The 
> picture"); email.setMsg("Here is the picture you wanted");
> 
> // add the attachment
> email.attach(attachment);
> 
> // send the email
> email.send();
> 
> 
> 
> 
> 
> 
> 
> 
> > -----Original Message-----
> > From: Matthias Wessendorf [mailto:mailings@matthias-wessendorf.de]
> > Sent: Thursday, September 02, 2004 2:21 PM
> > To: 'Struts Users Mailing List'
> > Subject: RE: PDF & Email
> > 
> > 
> > you only use setter-methods
> > for clazz Email / simpleEmail
> > and so one...
> > 
> > see:
> > 
> > SimpleEmail email = new SimpleEmail();
> > email.setHostName("mail.myserver.com");
> > email.addTo("jdoe@somewhere.org", "John Doe");
> > email.setFrom("me@apache.org", "Me"); email.setSubject("Test 
> > message"); email.setMsg("This is a simple test of commons-email");
> > email.send();
> > 
> > 
> > no painfull
> > 
> > Storage and so on ... :-)
> > 
> > it is easy... more than easy
> > 
> > 
> > Regards,
> > Matthias
> > 
> > > -----Original Message-----
> > > From: HG [mailto:struts@nospam.websolver.dk]
> > > Sent: Thursday, September 02, 2004 2:26 PM
> > > To: Struts Users Mailing List
> > > Subject: Re: PDF & Email
> > > 
> > > 
> > > Hi Chris
> > > 
> > > Can't recall from my memroy how to do it, and I don't 
> have access to
> > > the source from my current location, sorry.
> > > 
> > > No one says, you should use JavaMail as is.
> > > The mail library referred to by Matthias seems to be 
> great...and as
> > > far as I can see it uses the Java Mail API also....but greatly 
> > > simplifies the act of doing E-mails...The Java Mail API 
> can be very 
> > > cumbersome....
> > > 
> > > HTH
> > > 
> > > Henrik
> > > 
> > > ----- Original Message -----
> > > From: "CRANFORD, CHRIS" <chris.cranford@setechusa.com>
> > > To: "'Struts Users Mailing List'" <user@struts.apache.org>
> > > Sent: Thursday, September 02, 2004 2:09 PM
> > > Subject: RE: PDF & Email
> > > 
> > > 
> > > > Have any code snippets you could share on how to pass the
> > generated
> > > > PDF to javamail as an attachment.  That would greatly 
> expedite my 
> > > > coding
> > > efforts...
> > > >
> > > >
> > > > TIA
> > > > Chris
> > > >
> > > > -----Original Message-----
> > > > From: HG [mailto:struts@nospam.websolver.dk]
> > > > Sent: Thursday, September 02, 2004 8:14 AM
> > > > To: Struts Users Mailing List
> > > > Subject: Re: PDF & Email
> > > >
> > > >
> > > > Hi Chris
> > > >
> > > > I used iText (http://www.lowagie.com/iText/) in the past to
> > > generate
> > > > the
> > > PDF
> > > > and then JavaMail API to send it as an attachment. Works great..
> > > >
> > > > Regards
> > > >
> > > > Henrik
> > > >
> > > >
> > > > ----- Original Message -----
> > > > From: "CRANFORD, CHRIS" <chris.cranford@setechusa.com>
> > > > To: <user@struts.apache.org>
> > > > Sent: Thursday, September 02, 2004 1:59 PM
> > > > Subject: PDF & Email
> > > >
> > > >
> > > > > I need to generate an email along with a PDF within a
> > java struts
> > > > > action
> > > > and
> > > > > send that email with the PDF attachment using java mail.
> > > Can anyone
> > > > > recommend a great PDF package that I can do this and
> > > render the PDF
> > > > > as an email attachment in realtime?
> > > > >
> > > > > _______________________________________________________
> > > > > Chris Cranford
> > > > > Programmer/Developer
> > > > > SETECH Inc. & Companies
> > > > > 6302 Fairview Rd, Suite 201
> > > > > Charlotte, NC  28210
> > > > > Phone: (704) 362-9423, Fax: (704) 362-9409, Mobile:
> > (704) 650-1042
> > > > > Email: chris.cranford@setechusa.com
> > > > >
> > > > >
> > > >
> > > >
> > > > 
> > > 
> > 
> ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > > For additional commands, e-mail: user-help@struts.apache.org
> > > >
> > > >
> > > > 
> > > 
> > 
> ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > > For additional commands, e-mail: user-help@struts.apache.org
> > > >
> > > 
> > > 
> > > 
> > 
> ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > For additional commands, e-mail: user-help@struts.apache.org
> > > 
> > 
> > 
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> > 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message