第一个pdf页面的大小不像在liferay中使用itext的其他pdf页面那么大

时间:2013-12-01 07:28:56

标签: pdf liferay itext liferay-6

我的要求是开发一个从user_ table读取数据的应用程序,这是liferay默认数据库lportal中存在的默认表之一,并以pdf格式发布该数据。  每一件事都很好但是当我试图增加页面的大小时,除了第一页以外,所有其他页面的大小都会根据我的需要增加。我不知道为什么会发生这种情况以及为什么只有第一页尺寸没有增加。 我的动作类代码是

 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);
/*  float[] columnWidths = new float[39];
for(int i=0;i<39;i++)
    columnWidths[i]=10f;
table.setWidths(columnWidths);*/

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

由于 asif aftab

1 个答案:

答案 0 :(得分:3)

首先我问一个问题,然后我给出答案。在这篇文章中,我的目的是分享我的经验,即我面临的问题是什么,以及我如何摆脱这个问题。   在我的问题中,我说我的应用程序中唯一的障碍是第一页的大小并不像我提到的那么大,其他页面是根据我的命令。   现在解决方案是我们在document.open()和问题解决之前设置了页面大小。

   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);
    /*  float[] columnWidths = new float[39];
    for(int i=0;i<39;i++)
        columnWidths[i]=10f;
    table.setWidths(columnWidths);*/

    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