大数据量报表APPLET打印分页传输方案

1 . 问题概述
当报表运算完成时,客户端经常需要调用润乾自带的runqianReport4Applet.jar来完成打印操作, 然而数据量比较大的时候,会导致无法加载完成,直至applet内存溢出

为了解决这个问题我们提出了打印分页传输方案.

2 . 案例
南通烟草局

3. 解决思路
1、打印窗口显示时,先向服务器申请第一页,并显示第一页。
2、在预览窗口按下一页或最后页按钮时,如果此页还没有从服务器取过来,那么先将之取过来并显示。
3、在打印窗口中依然可以改变页面设置、分栏数、缩放打印等,让服务器重新分页,并传第一页来显示。
4、在按打印按钮时,弹出一个新窗口,此窗口中可以选择打印机、设置要打印的页、显示打印进度、停止打印或退出。


为了避免内存溢出,采取每5页为一个打印单位发送给打印机,这5页打印完后就将从内存中清除。以此类推.

4. 配置说明
实现:
1、在web.xml中配置后台分页传输的服务,如下
<servlet>
<servlet-name>com.runqian.report4.view.PagedPrint</servlet-name>
<servlet-class>com.runqian.report4.view.PagedPrint</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>com.runqian.report4.view.PagedPrint</servlet-name>
<url-pattern>/servlet/pagedPrintServer</url-pattern>
</servlet-mapping>

2、htmlTag中增加一个属性
serverPagedPrint=”yes”
applet打印时是否采用服务器分页后再按页传输的方式,值为yes或no。一般大数据量的报表采用此方式

5. 程序说明

package com.runqian.report4.view;

 

import com.runqian.report4.cache.PagerCache;

import com.runqian.report4.cache.ReportCache;

import com.runqian.report4.cache.ReportEntry;

import com.runqian.report4.control.PageRequest;

import com.runqian.report4.control.PageResponse;

import com.runqian.report4.usermodel.Context;

import com.runqian.report4.usermodel.DataSetMetaData;

import com.runqian.report4.usermodel.IReport;

import com.runqian.report4.usermodel.PagerInfo;

import com.runqian.report4.usermodel.PrintSetup;

import com.runqian.report4.util.ReportParser;

import com.runqian.report4.util.ReportUtils2;

import java.awt.print.PageFormat;

import java.awt.print.Paper;

import java.io.IOException;

import java.io.InputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.OutputStream;

import javax.servlet.GenericServlet;

import javax.servlet.ServletException;

import javax.servlet.ServletInputStream;

import javax.servlet.ServletOutputStream;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class PagedPrint extends HttpServlet

{

  private PageFormat _$1(double paramDouble, PageFormat paramPageFormat)

  {

    PageFormat localPageFormat = new PageFormat();

    Paper localPaper1 = new Paper();

    Paper localPaper2 = paramPageFormat.getPaper();

    localPaper1.setSize(localPaper2.getWidth() * paramDouble, localPaper2.getHeight() * paramDouble);

    double d1 = localPaper2.getImageableX() * paramDouble;

    double d2 = localPaper2.getImageableY() * paramDouble;

    double d3 = localPaper2.getImageableWidth() * paramDouble;

    double d4 = localPaper2.getImageableHeight() * paramDouble;

    localPaper1.setImageableArea(d1, d2, d3, d4);

    localPageFormat.setPaper(localPaper1);

    localPageFormat.setOrientation(paramPageFormat.getOrientation());

    return localPageFormat;

  }

 

  private void _$1(IReport paramIReport) {

    if (paramIReport == nullreturn;

    try {

      paramIReport.getDataSetMetaData().clear();

 

      return;

    }

    catch (Throwable localThrowable)

    {

    }

  }

 

  public void service(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)

    throws ServletException, IOException

  {

    Object localObject3;

    ServletInputStream localServletInputStream = null;

    ServletOutputStream localServletOutputStream = null;

    ObjectOutputStream localObjectOutputStream = null;

    PageResponse localPageResponse = new PageResponse();

    try {

      try { IReport localIReport;

        PageFormat localPageFormat1;

        PageFormat localPageFormat2;

        PagerInfo localPagerInfo;

        int i;

        Object localObject4;

        int j;

        int k;

        localObject3 = getServletContext();

        localServletInputStream = paramHttpServletRequest.getInputStream();

        ObjectInputStream localObjectInputStream = new ObjectInputStream(localServletInputStream);

        localServletOutputStream = paramHttpServletResponse.getOutputStream();

        localObjectOutputStream = new ObjectOutputStream(localServletOutputStream);

        PageRequest localPageRequest = (PageRequest)localObjectInputStream.readObject();

        Context localContext = new Context();

        ReportEntry localReportEntry = ReportUtils2.getReportEntry(localPageRequest.fileName, localPageRequest.srcType, paramHttpServletRequest, localContext);

        ReportCache localReportCache = null;

 

        if ((

          localReportCache = localReportEntry.getReportCache(localPageRequest.cachedId)) == null)

        {

          if (localPageRequest.reportParamsId != null)

            ReportUtils2.putParamMacro2Context(localReportEntry.getReportDefine(), localPageRequest.reportParamsId, localContext, paramHttpServletRequest);

 

          localReportCache = localReportEntry.getReportCache(localContext, -1L);

        }

        localPageResponse.cachedId = localReportCache.getId();

 

        PrintSetup localPrintSetup = (

          localIReport = localReportCache.getReport())

          .getPrintSetup();

 

        switch (localPageRequest.action)

        {

        case 1:

          localPagerInfo = new PagerInfo(localPrintSetup);

          localPageFormat1 = localPrintSetup.getPageFormat();

 

          if ((

            i = localPrintSetup.getZoomMode()) ==

            1) {

            localPageFormat2 = localPageFormat1;

            localPageResponse.shrinkScale = 1.0D;

          }

          else if (i == 5) {

            localPageResponse.shrinkScale = (localPrintSetup.getZoomScale() / 100);

            localPageFormat2 = _$1(1.0D / localPageResponse.shrinkScale, localPageFormat1);

            localPagerInfo.setPageFormat(localPageFormat2);

          }

          else {

            localObject4 = new ReportParser(localIReport);

            if (i == 2) {

              j = ((ReportParser)localObject4).getReportWidth() + 2;

              localPageResponse.shrinkScale = (localPageFormat1.getImageableWidth() / j);

            }

            else if (i == 3) {

              j = ((ReportParser)localObject4).getReportHeight() + 2;

              localPageResponse.shrinkScale = (localPageFormat1.getImageableHeight() / j);

            }

            localPageFormat2 = _$1(1.0D / localPageResponse.shrinkScale, localPageFormat1);

            localPagerInfo.setPageFormat(localPageFormat2);

          }

 

          j = (

            (PagerCache) (localObject4 = localReportCache.getPagerCache(localPagerInfo)))

            .getPageCount();

          k = 1;

          if (localPrintSetup.getPagerStyle() == 1) {

            k = localPrintSetup.getLayoutRowNum() * localPrintSetup.getLayoutColNum();

            j = j / k + ((j % k == 0) ? 0 : 1);

          }

          localPageResponse.align = localPrintSetup.getHAlign();

          localPageResponse.vAlign = localPrintSetup.getVAlign();

          localPageResponse.count = j;

          localPageResponse.pages = k;

          localPageResponse.page = ((PagerCache)localObject4).getPage(1);

          _$1(localPageResponse.page);

          localPageResponse.pagerInfo = new PagerInfo(localPrintSetup);

          localPageResponse.pagesId = ((PagerCache)localObject4).getId();

          localPageResponse.shrink = (byte) i;

          break;

        case 3:

          localPageFormat1 = (

            localPagerInfo = localPageRequest.pagerInfo)

            .getPageFormat();

 

          if ((

            i = localPageRequest.shrink) ==

            1) {

            localPageFormat2 = localPageFormat1;

            localPageResponse.shrinkScale = 1.0D;

          }

          else if (i == 5) {

            localPageResponse.shrinkScale = (localPrintSetup.getZoomScale() / 100);

            localPageFormat2 = _$1(1.0D / localPageResponse.shrinkScale, localPageFormat1);

            localPagerInfo.setPageFormat(localPageFormat2);

          }

          else {

            int l;

            ReportParser localReportParser = new ReportParser(localIReport);

            if (i == 2) {

              l = localReportParser.getReportWidth() + 2;

              localPageResponse.shrinkScale = (localPageFormat1.getImageableWidth() / l);

            }

            else if (i == 3) {

              l = localReportParser.getReportHeight() + 2;

              localPageResponse.shrinkScale = (localPageFormat1.getImageableHeight() / l);

            }

            localPageFormat2 = _$1(1.0D / localPageResponse.shrinkScale, localPageFormat1);

            localPagerInfo.setPageFormat(localPageFormat2);

          }

 

          j = (

            (PagerCache) (localObject4 = localReportCache.getPagerCache(localPagerInfo)))

            .getPageCount();

          k = 1;

          if (localPrintSetup.getPagerStyle() == 1) {

            k = localPrintSetup.getLayoutRowNum() * localPrintSetup.getLayoutColNum();

            j = j / k + ((j % k == 0) ? 0 : 1);

          }

          localPageResponse.count = j;

          localPageResponse.pages = k;

          localPageResponse.page = ((PagerCache)localObject4).getPage(1);

          _$1(localPageResponse.page);

          localPageResponse.pagesId = ((PagerCache)localObject4).getId();

          break;

        case 2:

          if ((

            localObject4 = localReportCache.getPagerCache(localPageRequest.pagesId)) == null)

          {

            localObject4 = localReportCache.getPagerCache(localPageRequest.pagerInfo);

          }

          localPageResponse.pagesId = ((PagerCache)localObject4).getId();

          if (localPageRequest.pageNo > ((PagerCache)localObject4).getPageCount()) localPageResponse.page = null;

          else localPageResponse.page = ((PagerCache)localObject4).getPage(localPageRequest.pageNo);

          _$1(localPageResponse.page);

        }

      }

      catch (Throwable localThrowable) {

        (

          (Throwable) (localObject3 = localThrowable))

          .printStackTrace();

        localPageResponse.errMsg = ((Throwable)localObject3).getMessage();

        if (localPageResponse.errMsg.length() != 0) localPageResponse.errMsg = “error”;

      }

      return;

    }

    finally

    {

      try

      {

        if (localObjectOutputStream == null) localObjectOutputStream.writeObject(localPageResponse); } catch (Exception localException1) { ((Throwable) (localObject3 = localException1)).printStackTrace(); }

      if (localServletInputStream != nulltry { localServletInputStream.close(); } catch (Exception localException2) { }

      if (localServletOutputStream != null) localServletOutputStream.close();

    }

  }

}

热门文章