Archive for Tháng tám, 2005
JasperReport: Fill, Create PDF,-attach to email, send
JasperPrint jasperPrint = JasperFillManager.fillReport(pathToJapserReportFilename, parameters, jrBeanArrayDataSource);
byte[] pdfBytes = JasperExportManager.exportReportToPdf(jasperPrint);
mailer.sendMail(MAIL_FROM, to_email_address, subject, MAIL_BODY, pdfBytes, filename + “.pdf”, “application/pdf”);
The actual emailing part is a little more complex. You’ll need a javax.activation.DataSource implementation that handles byte[]’s.
Here’s one I wrote, or you can use org.apache.commons.mail.ByteArrayDataSource.
public static DataSource getDataSource(final byte[] bytes, final String theContentType) {
return new DataSource() {
public InputStream getInputStream()
{
return new ByteArrayInputStream(bytes);
}
public OutputStream getOutputStream()
{
throw new UnsupportedOperationException();
}
public String getContentType() {
return theContentType;
}
public String getName() {
throw new UnsupportedOperationException();
}
};
}
And then here’s the code from my MailerBean (this is running inside JBoss).
Once you have the datasource working, this is all pretty standard Java mailing stuff.
public void sendMail(String from,String to,String subject,String bodyText,
byte[] theAttachmentBytes, String theFilename, String theContentType)
{
javax.mail.Session session = null;
try {
session = (Session)PortableRemoteObject.narrow(new InitialContext().lookup(“java:Mail”), Session.class);
} catch (javax.naming.NamingException e) {
e.printStackTrace();
}
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = {new InternetAddress(to)};
message.setRecipients(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setSentDate(new Date());
// create and fill the first message part
MimeBodyPart body = new MimeBodyPart();
body.setText(bodyText);
body.setDisposition(Part.INLINE);
body.setHeader(“Content-Type”, “text/plain”);
// turn the File into a Part
MimeBodyPart part = new MimeBodyPart();
part.setFileName(theFilename);
part.setDisposition(Part.ATTACHMENT);
DataSource dataSource = getDataSource(theAttachmentBytes, theContentType);
part.setDataHandler(new DataHandler(dataSource));
// set the header for the content type, using the type
// determined by the file name to mime type mapper that
// is inherent in the FileDataSource.
//part.setHeader(“Content-Type”, dataSource.getContentType());
part.setHeader(“Content-Type”, dataSource.getContentType());
// set the transfer encoding so that binary data will
// come through cleanly. in cases where the content
// type is a “text” type you may not need to do base64
// encoding, but it’s simple and safe to always encode
// attachments.
part.setHeader(“Content-Transfer-Encoding”, “base64″);
Multipart multipart = new MimeMultipart();
message.setContent(multipart);
message.setHeader(“Content-Type”, multipart.getContentType());
Multipart mp = new MimeMultipart();
mp.addBodyPart(body);
mp.addBodyPart(part);
// add the Multipart to the message
message.setContent(mp);
Transport.send(message);
} catch (javax.mail.MessagingException e) {
Debug.error(“MailerBean.sendHtmlMail”, e);
}
Add comment Tháng tám 16, 2005
JasperReports voi Hibernate, again
Day la mot trang rat hay noi ve cach dung JasperReports voi Hibernate
http://www.jroller.com/page/wakaleo/?anchor=using_hibernate_queries_with_jasperreports
Add comment Tháng tám 12, 2005
Tutorial for WebApps using JasperReports on Tomcat
1 comment Tháng tám 1, 2005
Fonts for JasperReport
Set JVM với tham số -Djava.awt.fonts tới thư mục chứa font.
Add comment Tháng tám 1, 2005
JasperReport on Unix/Linux environments with X-Window
Một trong những vấn đề developer thường gặp trong môi trường *nix là conflict của jasperreports với X-Window. Để tránh vấn đề này, các giải pháp có thể là:
- Chạy JVM với tham số -Djava.awt.headless=true
- Trong TomCat, dùng tham số này bằng cách thay đổi tham số trong catarina.sh. Cụ thể như sau: Tomcat
Đặt dòng lệnh này trong startup script:
CATALINA_OPTS=”$CATALINA_OPTS -Djava.awt.headless=true” hay có thể dùng cách khác bằng cách đặt trong ./bin/catalina.sh
# ----- Execute The Requested Command ----------------------------------------- echo "Using CATALINA_BASE: $CATALINA_BASE"echo "Using CATALINA_HOME: $CATALINA_HOME"echo "Using CATALINA_TMPDIR: $CATALINA_TMPDIR"echo "Using JAVA_HOME: $JAVA_HOME" if [ "$1" = "jpda" ] ; then if [ -z "$JPDA_TRANSPORT" ]; then JPDA_TRANSPORT="dt_socket" fi if [ -z "$JPDA_ADDRESS" ]; then JPDA_ADDRESS="8000" fi if [ -z "$JPDA_OPTS" ]; then JPDA_OPTS="-Xdebug -Xrunjdwp:transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=n" fi CATALINA_OPTS="$CATALINA_OPTS $JPDA_OPTS" shiftfiCATALINA_OPTS="$CATALINA_OPTS -Djava.awt.headless=true"
- Trong chương trình java, có thể đặt lệnh set trong đoạn Init của web/app với câu lệnh sau: system.setProperty(“java.awt.headless”, “true”)
- Trong môi trường Ecclipse, dùng
eclipse -vmargs -Djava.awt.headless=true
Hy vọng các giải pháp này sẽ giúp mình giải quyết ổn thoả mọi chuyện
Add comment Tháng tám 1, 2005