21
Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 1 Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage Summary This article throws light on using JSP Dyne Page in Enterprise Portal. This article illustrates how to generate a PDF file with image background in Enterprise Portal by taking a simple real time scenario as an example. Author: Nabendu Kanti Maity Company: HCL Technologies Created on: 23 August 2006 Author Bio Nabendu Kanti Maity is working as a Netweaver consultant for HCL, Kolkata.

Generate with Background Image in SAP Enterprise ... · Generate with Background Image in SAP Enterprise Portal Using JSPDynPage SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS

  • Upload
    others

  • View
    25

  • Download
    0

Embed Size (px)

Citation preview

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 1

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

Summary This article throws light on using JSP Dyne Page in Enterprise Portal. This article illustrates how to generate a PDF file with image background in Enterprise Portal by taking a simple real time scenario as an example. Author: Nabendu Kanti Maity Company: HCL Technologies Created on: 23 August 2006

Author Bio Nabendu Kanti Maity is working as a Netweaver consultant for HCL, Kolkata.

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 2

Table of Contents Introduction ...................................................................................................................................... 3 The Problem to Solve ...................................................................................................................... 4 Solution Approach ........................................................................................................................... 5

Setting up the Application ............................................................................................................ 5 Creating Jsp Files ........................................................................................................................ 5

Solution Implementation .................................................................................................................. 6 Implementing the JSPDynPage ................................................................................................... 6 Managing Configuration Files .................................................................................................... 14

The Application in Action ............................................................................................................... 15 Appendix........................................................................................................................................ 17

Appendix A................................................................................................................................. 17 Appendix B................................................................................................................................. 18 Appendix C................................................................................................................................. 19

Related Content............................................................................................................................. 20 Disclaimer and Liability Notice....................................................................................................... 21

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 3

Introduction JSP are Modules of Java Code which resides on a web server, and responds to a web client request. In a portal environment the client requests are often handled by a portlet, which runs within a portal server. It is always possible to have a co existence of both JSP and Portlets in a portal environment. The JSP DynPage provides enhanced event handling and easy session management. In this example we use the HTML-Business for Java (HTMLB) controls to create the Graphical User Interface (GUI). HTMLB is a Portal service. Dataflow of a DynPage Component:

Some of the real time situations which demands such a scenario are discussed below Dynamic Generation PDF file. Even here we add image file as PDF background.

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 4

The Problem to Solve This article shows how to create a file in PDF format with background Image in SAP Enterprise Portal. The following assumptions set the scenario for the fictitious problem the implementation solves. An organization needs to create or convert the webpage document for example student information in PDF format. System needs to display the Student details, so that it can be printed or generated PDF file can save in disk for further use.

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 5

Solution Approach Let’s discuss in detail about the solution approach

Setting up the Application Launch NetWeaver Developer Studio Create a Portal Application Project. To create a portal application project follow the below mentioned steps. From the File menu, select New... Other. The New window is displayed. Select Portal Application, and then Create a Portal Application Project. Click Next. In the Project name textbox, enter a name for the project, say StudentDetails Click Finish. Create an JSPDynPage. To crate an JSPDynPage follow the below mentioned steps From the File menu, select new → other. The New window is displayed. Select Portal Application, and then Create a New Portal Application Object. Click Next. Select the project to which you want to add the portal component (StudentDetails). Click Next. Select Portal Component → JSPDynPage. Click Next. Enter the following fields: Name: Name of component (say StudentDynPage)

Location: The location of the new Java class file for this component JSPDynPage Class name: The Java class file for this component. JSPDynPage Package name: The package name for the Java class file for this component. JSPDynPage JSP Filename: The JSP filename for this component. Click Next. Enter the fields of “Use a bean for Information Exchange” Bean Name: Name of the bean (say StudentBean) Bean Scope: Specify the scope using the dropdown box. Location: The location of the new Bean(Java) class file for this component Class name: The Bean(Java) class file for this component(say StudentBean) . Package name: The package name for the Java class file for this component. Click Finish. Add the following jar files to your project class path itext-2.0.3 .jar (can be downloaded from http://www.lowagie.com/iText/download.html )

Creating Jsp Files Create one Jsp Files namely GeneratePDF.jsp under PORTAL-INF/pagelet folder.

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 6

Solution Implementation Now we are going to start solution implementation.The entire application shows with example student information.

Implementing the JSPDynPage 1: StudentDynPage.class package com.hclt.page; import javax.servlet.http.HttpSession; import com.hclt.bean.StudentBean; import com.sapportals.htmlb.*; import com.sapportals.htmlb.enum.*; import com.sapportals.htmlb.event.*; import com.sapportals.htmlb.page.*; import com.sapportals.portal.htmlb.page.*; import com.sapportals.portal.prt.component.*; import com.help.ApplicationUtil; public class StudentDynPage extends PageProcessorComponent { private final static int INITIAL_STATE = 0; private final static int PDF_CERIFICATE_STATE = 1; private static int state; private static String pageState=""; public DynPage getPage(){ return new StudentDynPageDynPage(); } public static class StudentDynPageDynPage extends JSPDynPage { private StudentBean StudentBean = null; public void doInitialization() { state = INITIAL_STATE; // create the bean and set a default text value "unknown user IPortalComponentRequest request = (IPortalComponentRequest) this.getRequest(); IPortalComponentContext myContext = request.getComponentContext(); IPortalComponentProfile myProfile = myContext.getProfile(); StudentBean studentContainer=new StudentBean(); HttpSession userSession=request.getComponentSession().getHttpSession(); userSession.putValue("studentBean",studentContainer); } public void doProcessAfterInput() throws PageException {

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 7

pageState = ((InputField) getComponentByName("state")).getValueAsDataType().toString().trim(); IPortalComponentRequest request = (IPortalComponentRequest) this.getRequest(); IPortalComponentResponse resp=(IPortalComponentResponse)this.getResponse(); IPortalComponentContext myContext = request.getComponentContext(); IPortalComponentProfile myProfile = myContext.getProfile(); HttpSession userSession=request.getComponentSession().getHttpSession(); StudentBean studentContainer=(StudentBean)userSession.getValue("studentBean"); //URAErrorBean errorBean=(URAErrorBean)userSession.getValue("errorBean"); if(pageState.equals("INITIAL_STATE")) { try { this.setPDFdata(studentContainer); } catch(Exception e) { e.toString(); } } } public void doProcessBeforeOutput() throws PageException { switch (state) { case PDF_CERIFICATE_STATE : this setJspName("GeneratePDF.jsp"); . break; default : this.setJspName("StudentInformation.jsp"); break; } } public void onSubmitMe(Event event) throws PageException { state=PDF_CERIFICATE_STATE; } private void setPDFdata(StudentBean studentContainer) { try {

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 8

InputField studentID = (InputField) getComponentByName("studentID"); InputField studentName = (InputField) getComponentByName("studentName"); InputField studentDOB = (InputField) getComponentByName("studentDOB"); studentContainer.setStudentID(studentID.getValueAsDataType().toString()); studentContainer.setStudentName(studentName.getValueAsDataType().toString()); studentContainer.setSudentDOB(ApplicationUtil.stringtoDate(studentDOB.getValueAsDataType().toString())); } catch(Exception e1) { } } } }

2: Implementing the JSP Files(StudentInformation.jsp) <%@ page import="com.hclt.bean.StudentBean,com.help.StudentUtil"%> <%@ taglib uri= "tagLib" prefix="hbj" %> <% StudentBean studentBean=(StudentBean)componentRequest.getComponentSession().getHttpSession().getValue("studentBean"); %> <hbj:content id="myContext" > <hbj:page title="PageTitle"> <hbj:form id="myFormId" > <br><br><br><br><br> <hbj:inputField id="state" type="STRING" visible="FALSE" value="INITIAL_STATE" design="STANDARD"/> <table align="center" width="90%" border="0">

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 9

<th colspan="2"> <font color="#CA4E1B"><u>Student Information</u></font> </th> <tr> <td> &nbsp; </td> </tr> <tr> <td> <table align="center" width="60%" border="0" cellspacing="0" cellpadding="5"> <tr> <td width="40%" height="30%" align="right" bgcolor="#D56E4D"> &nbsp; </td> <td width="40%" height="30%" align="right" bgcolor="#CA4E1B"> &nbsp; </td> </tr> <tr> <td width="40%" align="right" bgcolor="#CA4E1B"> <hbj:label id="lblStudentId" text="Student ID" labelFor="studentID" design="STANDARD" encode="FALSE"> <% lblStudentId.setHasDesignBar(false); %> </hbj:label> </td> <td width="40%" align="center" bgcolor="#D56E4D"> <hbj:inputField id="studentID" type="STRING" design="STANDARD" jsObjectNeeded="true"> </hbj:inputField> </td> </tr> <tr>

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 10

<td width="40%" align="right" bgcolor="#CA4E1B"> <hbj:label id="lblStudentName" text="Student Name" labelFor="studentName" design="STANDARD" encode="FALSE"> <% lblStudentName.setHasDesignBar(false); %> </hbj:label> </td> <td width="40%" align="center" bgcolor="#D56E4D"> <hbj:inputField id="studentName" type="STRING" design="STANDARD" jsObjectNeeded="true"> </hbj:inputField> </td> </tr> <tr> <td width="40%" align="right" bgcolor="#CA4E1B"> <hbj:label id="lblStudentDOB" text="Student Date Of Birth" labelFor="studentDOB" design="STANDARD" encode="FALSE"> <% lblStudentDOB.setHasDesignBar(false); %> </hbj:label> </td> <td width="40%" align="center" bgcolor="#D56E4D"> <hbj:inputField id="studentDOB" type="Date" showHelp="true" design="STANDARD" jsObjectNeeded="true"> </hbj:inputField> </td> </tr> <tr> <td align="right" bgcolor="#CA4E1B"> <hbj:button id="buttonSubmit" text="submit" tooltip="Click Me to generate PDF file" onClick="onSubmitMe" width="80" design="STANDARD"> <%

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 11

myFormId.setDefaultButton(buttonSubmit); %> </hbj:button> </td> <td align="left" bgcolor="#D56E4D"> </td> </tr> <tr> <td width="40%" height="30%" align="right" bgcolor="#D56E4D"> &nbsp; </td> <td width="40%" height="30%" align="right" bgcolor="#CA4E1B"> &nbsp; </td> </tr> </table> </td> </tr> </table> </hbj:form> </hbj:page> </hbj:content> Above JSP page contains three input fields - Student ID - Student Name - Student Date Of Birth The following JSP code create a PDF file with information Student Id, Student Name and Student Date Of Birth whatever coming from the previous JSP(StudentInformation,jsp). 3:GeneratePDF.jsp <%@ page import="java.awt.Color,java.io.File,java.io.FileOutputStream,java.io.IOException,com.lowagie.text.Document,com.lowagie.text.DocumentException"%>

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 12

<%@ page import="com.lowagie.text.Font,com.lowagie.text.pdf.PdfContentByte,com.lowagie.text.FontFactory,com.lowagie.text.Paragraph,com.lowagie.text.Table"%> <%@ page import="com.lowagie.text.html.HtmlWriter,com.lowagie.text.Image,com.lowagie.text.pdf.PdfWriter,com.lowagie.text.rtf.RtfWriter2,com.lowagie.text.PageSize,com.lowagie.text.pdf.PdfStamper,com.lowagie.text.pdf.PdfReader"%> <%@ page import="com.sapportals.portal.prt.component.IPortalComponentRequest,com.sapportals.portal.prt.component.IPortalComponentResponse"%> <%@ page import="javax.servlet.http.HttpServletResponse,javax.servlet.ServletOutputStream" %> <%@ page import="com.hclt.bean.StudentBean,java.io.File"%> <%@ taglib uri= "tagLib" prefix="hbj" %> <% StudentBean studentBean=(StudentBean)componentRequest.getComponentSession().getHttpSession().getValue("studentBean"); %> <% Document document = new Document(); HttpServletResponse res=componentRequest.getServletResponse(true); res.setContentType("application/pdf"); PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("/CertificatePDF123.pdf")); String s="/CertificatePDF123.pdf"; document.open(); Paragraph heading=new Paragraph("Student Details",FontFactory.getFont(FontFactory.TIMES_BOLD, 16, Font.BOLDITALIC,new Color(155, 100, 0))); heading.setAlignment(1); heading.setSpacingBefore(10); document.add(heading); Table t = new Table(2,6); t.setBorderColor(new Color(100, 100, 100)); t.setPadding(3); t.setSpacing(0); t.setBorderWidth(3); String studentID=studentBean.getStudentID(); String studentName=studentBean.getStudentName(); String studentDOB=studentBean.getSudentDOB().toString(); t.addCell("Student ID::"); t.addCell(studentID); t.addCell("Student Name::"); t.addCell(studentName); t.addCell("Student Date Of Birth::"); t.addCell(studentDOB); document.add(t); document.close();

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 13

int zIndex = 0; try { document = new Document(PageSize.A4, 50, 50, 50, 50); writer = PdfWriter.getInstance(document,res.getOutputStream()); PdfReader pdfReader = new PdfReader(s); int n = pdfReader.getNumberOfPages(); System.out.println("n:: " + n); // creae an outputstream ServletOutputStream streamOut = res.getOutputStream(); // give it to the pdfstamper PdfStamper pdfStamper = new PdfStamper(pdfReader, streamOut); // contentbyte is a static object, no constructor PdfContentByte under = new PdfContentByte(writer); // now create an instance with this file (report_watermark.jpg) Image img = Image.getInstance("F:/Sunset.jpg"); //System.out.println("Here 1 ::" + img); img.setAbsolutePosition(0,350); //System.out.println("Here 2"); //for (int i=0;i<n;i++) //{ under = pdfStamper.getUnderContent(1); under.addImage(img); //} pdfStamper.close(); File f= new File("/CertificatePDF123.pdf"); if(f.exists()) { f.delete(); } } catch (Exception ex) { } %> <hbj:content id="myContext" > <hbj:page> </hbj:page>

</hbj:content>

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 14

Managing Configuration Files In order to make the application work a few changes needs to be done in portalapp.xml. The code of portalapp.xml is given below. 4.Part of portalapp.xml <component-config> <property name="ClassName" value="com.hclt.page.StudentDynPage"/> <property name="SecurityZone" value="com.sap.pct.pdk/low_safety"/>

</component-config>

Since we are using HTMLB components in our Jsp pages we need to make them available at runtime. This can done as shown below 5. Part of portalapp.xml

<component-profile> <property name="AuthScheme" value="anonymous"/> <property name="tagLib" value="/SERVICE/htmlb/taglib/htmlb.tld"/>

</component-profile>

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 15

The Application in Action Some snapshots of the application , running in EP 6.0 given below

Fig: 1 Student Data Entry for PDF Generation

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 16

Fig: 2 Student Details in PDF Format with Image Background of PDF File

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 17

Appendix

Appendix A 6. ApplicationUtil.java package com.help; import java.sql.Date; public class ApplicationUtil { public static Date stringtoDate(String str) { return Date.valueOf(str.trim()); }

}

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 18

Appendix B 7.Bean Classs (StudentBean.java) package com.hclt.bean; import java.io.Serializable; import java.sql.Date; public class StudentBean implements Serializable { private String studentID = ""; private String studentName = ""; private Date sudentDOB = null; /** * @return */ public String getStudentID() { return studentID; } /** * @return */ public String getStudentName() { return studentName; } /** * @return */ public getSudentDOB() { Date return sudentDOB; } /** * @param string */ public void setStudentID(String string) { studentID = string; } /** * @param string */ public void setStudentName(String string) { studentName = string; } /** * @param date */ public void setSudentDOB(Date date) { sudentDOB = date; } }

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 19

Appendix C 8. portalapp.xml <?xml version="1.0" encoding="utf-8"?> <application> <application-config> <property name="PrivateSharingReference" value="com.sap.portal.htmlb"/> </application-config> <components> <component name="StudentDynPage"> <component-config> <property name="ClassName" value="com.hclt.page.StudentDynPage"/> <property name="SecurityZone" value="com.sap.pct.pdk/low_safety"/> </component-config> <component-profile> <property name="AuthScheme" value="anonymous"/> <property name="tagLib" value="/SERVICE/htmlb/taglib/htmlb.tld"/> </component-profile> </component> </components> <services/> </application>

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 20

Related Content www.sdn.sap.com http://www.lowagie.com/ https://media.sdn.sap.com/javadocs/NW04/SP9/runtime/index.html

Generate PDF File with Background Image in SAP Enterprise Portal Using JSPDynPage

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 21

Disclaimer and Liability Notice This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade. SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk. SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document.