Problem:
My requirement was to develop an application to read data from user_ table which is one of the default table present in liferay default database lportal and publish that data in pdf format. Each and every thing is working fine but when I was try to increase the size of pages then except first page all other pages size increases according to my need. I don't know why this is happening and why only first page size is not increases. my action class code is
public class pdfPortlet extends MVCPortlet
{
public void serveResource(ResourceRequest req, ResourceResponse res)
throws PortletException, IOException
{
if(req.isUserInRole("administrator"))
{
try
{
adminMethod(req,res);
}
catch (PortalException e)
{
e.printStackTrace();
}
catch (SystemException e)
{
e.printStackTrace();
}
catch (DocumentException e)
{
e.printStackTrace();
}
}
else
{
userMethod(req,res);
}
}
/********** adminMethod which can fetch the whole user data from user_ table
* @throws SystemException
* @throws PortalException
* @throws DocumentException
* @throws IOException **********/
public void adminMethod(ResourceRequest req, ResourceResponse res) throws PortalException, SystemException, DocumentException, IOException
{
int numOfUser=UserLocalServiceUtil.getUsersCount(); //get the number of users
String remoteUserId = req.getRemoteUser(); //getting current user id
Integer io=Integer.parseInt(remoteUserId); //getting current userid
int currentUserId=io.intValue(); //typecasting from Integer to primitive int
User tblUser = UserLocalServiceUtil.getUser(currentUserId); //will return an object containing element of each row and hence by iterating this element we can get number of columns
System.out.println("@@@@@@ tbluser: "+tblUser);
req.setCharacterEncoding(StringPool.UTF8);
com.lowagie.text.Document document = new com.lowagie.text.Document(); //blank pdf created
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos); //predefine class PdfWriter calls static method getInstance
//document.setPageSize(new Rectangle(2000, 2600));
document.open(); //open pdf in write method
PdfPTable table= new PdfPTable(39); //creating a pdf table having 39 columns
document.setPageSize(new Rectangle(2000, 2600));
table.setWidthPercentage(100);
table.addCell("uuid_");
table.addCell("userId");
table.addCell("companyId");
table.addCell("createDate");
table.addCell("modifiedDate");
table.addCell("defaultUser");
table.addCell("contactId");
table.addCell("password_");
table.addCell("passwordEncrypted");
table.addCell("passwordReset");
table.addCell("passwordModifiedDate");
table.addCell("digest");
table.addCell("reminderQueryQuestion");
table.addCell("reminderQueryAnswer");
table.addCell("graceLoginCount");
table.addCell("screenName");
table.addCell("emailAddress");
table.addCell("facebookId");
table.addCell("openId");
table.addCell("portraitId");
table.addCell("languageId");
table.addCell("timeZoneId");
table.addCell("greeting");
table.addCell("comments");
table.addCell("firstName");
table.addCell("middleName");
table.addCell("lastName");
table.addCell("jobTitle");
table.addCell("loginDate");
table.addCell("loginIP");
table.addCell("lastLoginDate");
table.addCell("lastLoginIP");
table.addCell("lastFailedLoginDate");
table.addCell("failedLoginAttempts");
table.addCell("lockout");
table.addCell("lockoutDate");
table.addCell("agreedToTermsOfUse");
table.addCell("emailAddressVerified");
table.addCell("status");
List<User> totalUsers=UserLocalServiceUtil.getUsers(0,numOfUser); //fetch whole table in our object
Iterator<User> it=totalUsers.iterator();
while(it.hasNext())
{
User eachRow=(User) it.next();
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
String str=null;
java.util.Date jDate=null;
table.addCell(eachRow.getUuid());
table.addCell(eachRow.getUserId()+"");
table.addCell(eachRow.getCompanyId()+"");
jDate=eachRow.getCreateDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
jDate=eachRow.getModifiedDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getDefaultUser()+"");
table.addCell(eachRow.getContactId()+"");
table.addCell(eachRow.getPassword());
table.addCell(eachRow.getPasswordEncrypted()+"");
table.addCell(eachRow.getPasswordReset()+"");
jDate=eachRow.getPasswordModifiedDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getDigest());
Iterator itr=(eachRow.getReminderQueryQuestions()).iterator();
Object obj=itr.next();
str=(String)obj;
table.addCell(str);
table.addCell(eachRow.getReminderQueryAnswer());
table.addCell(eachRow.getGraceLoginCount()+"");
table.addCell(eachRow.getScreenName());
table.addCell(eachRow.getEmailAddress());
table.addCell(eachRow.getFacebookId()+"");
table.addCell(eachRow.getOpenId());
table.addCell(eachRow.getPortraitId()+"");
table.addCell(eachRow.getLanguageId());
table.addCell(eachRow.getTimeZoneId());
table.addCell(eachRow.getGreeting());
table.addCell(eachRow.getComments());
table.addCell(eachRow.getFirstName());
table.addCell(eachRow.getMiddleName());
table.addCell(eachRow.getLastName());
table.addCell(eachRow.getJobTitle());
jDate=eachRow.getLoginDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getLoginIP());
jDate=eachRow.getLastLoginDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getLastLoginIP());
jDate=eachRow.getLastFailedLoginDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getFailedLoginAttempts()+"");
table.addCell(eachRow.getLockout()+"");
jDate=eachRow.getLockoutDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getAgreedToTermsOfUse()+"");
table.addCell(eachRow.getEmailAddressVerified()+"");
table.addCell(eachRow.getStatus()+"");
}//end of wile
document.add(table); //adding table to created pdf document
document.close(); //we have to first close the document
String fileName="attachment;filename=ashraf.pdf"; //filename
res.setContentType("application/pdf"); //setting the content type either application or pdf(Portable Document Format)
res.addProperty(HttpHeaders.CONTENT_DISPOSITION, fileName); //
OutputStream out = res.getPortletOutputStream();
byte[] downloadBytes = Base64.decode((String) req.getAttribute("fileToDownloadBase64"));
out.write(downloadBytes);
baos.writeTo(out);
out.flush();
out.close();
} //adminMethod
Solution:
First I ask a question then I am giving the answer. In this post my
intention is to just share my experience that what's the problem that I
faced and how I get rid off from that problem.
In my question I said that the only hitch in my application is that
size of first page in not large as I mentioned and other pages are
according to my command.
Now solution is just we have set the page size before document.open()
and problem solve.
public class pdfPortlet extends MVCPortlet
{
public void serveResource(ResourceRequest req, ResourceResponse res)
throws PortletException, IOException
{
if(req.isUserInRole("administrator"))
{
try
{
adminMethod(req,res);
}
catch (PortalException e)
{
e.printStackTrace();
}
catch (SystemException e)
{
e.printStackTrace();
}
catch (DocumentException e)
{
e.printStackTrace();
}
}
else
{
userMethod(req,res);
}
}
/********** adminMethod which can fetch the whole user data from user_ table
* @throws SystemException
* @throws PortalException
* @throws DocumentException
* @throws IOException **********/
public void adminMethod(ResourceRequest req, ResourceResponse res) throws PortalException, SystemException, DocumentException, IOException
{
int numOfUser=UserLocalServiceUtil.getUsersCount(); //get the number of users
String remoteUserId = req.getRemoteUser(); //getting current user id
Integer io=Integer.parseInt(remoteUserId); //getting current userid
int currentUserId=io.intValue(); //typecasting from Integer to primitive int
User tblUser = UserLocalServiceUtil.getUser(currentUserId); //will return an object containing element of each row and hence by iterating this element we can get number of columns
System.out.println("@@@@@@ tbluser: "+tblUser);
req.setCharacterEncoding(StringPool.UTF8);
com.lowagie.text.Document document = new com.lowagie.text.Document(); //blank pdf created
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos); //predefine class PdfWriter calls static method getInstance
document.setPageSize(new Rectangle(2000, 2600));//"this is the difference form my previous code here I use this function before the document.open();
document.open(); //open pdf in write method
PdfPTable table= new PdfPTable(39); //creating a pdf table having 39 columns
//document.setPageSize(new Rectangle(2000, 2600));
table.setWidthPercentage(100);
table.addCell("uuid_");
table.addCell("userId");
table.addCell("companyId");
table.addCell("createDate");
table.addCell("modifiedDate");
table.addCell("defaultUser");
table.addCell("contactId");
table.addCell("password_");
table.addCell("passwordEncrypted");
table.addCell("passwordReset");
table.addCell("passwordModifiedDate");
table.addCell("digest");
table.addCell("reminderQueryQuestion");
table.addCell("reminderQueryAnswer");
table.addCell("graceLoginCount");
table.addCell("screenName");
table.addCell("emailAddress");
table.addCell("facebookId");
table.addCell("openId");
table.addCell("portraitId");
table.addCell("languageId");
table.addCell("timeZoneId");
table.addCell("greeting");
table.addCell("comments");
table.addCell("firstName");
table.addCell("middleName");
table.addCell("lastName");
table.addCell("jobTitle");
table.addCell("loginDate");
table.addCell("loginIP");
table.addCell("lastLoginDate");
table.addCell("lastLoginIP");
table.addCell("lastFailedLoginDate");
table.addCell("failedLoginAttempts");
table.addCell("lockout");
table.addCell("lockoutDate");
table.addCell("agreedToTermsOfUse");
table.addCell("emailAddressVerified");
table.addCell("status");
List<User> totalUsers=UserLocalServiceUtil.getUsers(0,numOfUser); //fetch whole table in our object
Iterator<User> it=totalUsers.iterator();
while(it.hasNext())
{
User eachRow=(User) it.next();
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
String str=null;
java.util.Date jDate=null;
table.addCell(eachRow.getUuid());
table.addCell(eachRow.getUserId()+"");
table.addCell(eachRow.getCompanyId()+"");
jDate=eachRow.getCreateDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
jDate=eachRow.getModifiedDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getDefaultUser()+"");
table.addCell(eachRow.getContactId()+"");
table.addCell(eachRow.getPassword());
table.addCell(eachRow.getPasswordEncrypted()+"");
table.addCell(eachRow.getPasswordReset()+"");
jDate=eachRow.getPasswordModifiedDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getDigest());
Iterator itr=(eachRow.getReminderQueryQuestions()).iterator();
Object obj=itr.next();
str=(String)obj;
table.addCell(str);
table.addCell(eachRow.getReminderQueryAnswer());
table.addCell(eachRow.getGraceLoginCount()+"");
table.addCell(eachRow.getScreenName());
table.addCell(eachRow.getEmailAddress());
table.addCell(eachRow.getFacebookId()+"");
table.addCell(eachRow.getOpenId());
table.addCell(eachRow.getPortraitId()+"");
table.addCell(eachRow.getLanguageId());
table.addCell(eachRow.getTimeZoneId());
table.addCell(eachRow.getGreeting());
table.addCell(eachRow.getComments());
table.addCell(eachRow.getFirstName());
table.addCell(eachRow.getMiddleName());
table.addCell(eachRow.getLastName());
table.addCell(eachRow.getJobTitle());
jDate=eachRow.getLoginDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getLoginIP());
jDate=eachRow.getLastLoginDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getLastLoginIP());
jDate=eachRow.getLastFailedLoginDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getFailedLoginAttempts()+"");
table.addCell(eachRow.getLockout()+"");
jDate=eachRow.getLockoutDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getAgreedToTermsOfUse()+"");
table.addCell(eachRow.getEmailAddressVerified()+"");
table.addCell(eachRow.getStatus()+"");
}//end of wile
document.add(table); //adding table to created pdf document
document.close(); //we have to first close the document
String fileName="attachment;filename=ashraf.pdf"; //filename
res.setContentType("application/pdf"); //setting the content type either application or pdf(Portable Document Format)
res.addProperty(HttpHeaders.CONTENT_DISPOSITION, fileName); //
OutputStream out = res.getPortletOutputStream();
byte[] downloadBytes = Base64.decode((String) req.getAttribute("fileToDownloadBase64"));
out.write(downloadBytes);
baos.writeTo(out);
out.flush();
out.close();
} //adminMethod
No comments:
Post a Comment