3

我们已经实现了 xHTML 到 PDF 的转换,以方便我们使用 iText 打印一些网页的可打印版本。以下是创建pdf的代码:

response.setContentType("application/pdf");
response.setHeader("content-type", "application/pdf");

ITextRenderer renderer = null;
ServletOutputStream out = null;
ByteArrayOutputStream os = null;
try {
    os = new ByteArrayOutputStream();
    renderer = new ITextRenderer();
    renderer.setDocument("http://www.url.com");
    renderer.layout();
    renderer.createPDF(os);
    out = response.getOutputStream();
    out.write(os.toByteArray());
} catch(Exception e) {
    log.error("Error occurred while creating pdf", e);
} finally {
    try {
        if(os != null) {
            os.flush();
            os.close();
        }
    } catch(Exception e) {}
    try {
        if(out != null) {
            out.flush();
            out.close();
        }
    } catch(Exception e) {}
}

使用 Eclipse MAT,我看到以下内容占用 1.2GB:

Class Name                                                          Shallow Heap    Retained Heap   Percentage
java.lang.Thread @ 0x2aaac31fdd38 activeThread-6                    168             1,335,868,960   86.43% 
    org.xhtmlrenderer.layout.LayoutContext @ 0x2aaad4af4358         152             63,981,672      4.14% 
    org.xhtmlrenderer.layout.SharedContext @ 0x2aaad13d92f0         152             36,439,120      2.36% 
    org.xhtmlrenderer.render.BlockBox @ 0x2aaaf9130c88              264             7,742,120       0.50% 
    org.xhtmlrenderer.css.style.CalculatedStyle @ 0x2aaad21ada60    96              7,341,800       0.47% 
    org.xhtmlrenderer.render.BlockBox @ 0x2aaafd7f7cc0              264             3,868,920       0.25% 
    org.xhtmlrenderer.css.style.CalculatedStyle @ 0x2aaaf06a1c58    96              3,701,320       0.24% 
    org.xhtmlrenderer.render.BlockBox @ 0x2aab002761a8              264             1,937,680       0.13% 
    org.xhtmlrenderer.layout.Layer @ 0x2aaad4af43f0                 136             1,910,640       0.12% 
    org.xhtmlrenderer.css.style.CalculatedStyle @ 0x2aaaf9153760    96              1,851,384       0.12% 
    org.xhtmlrenderer.render.BlockBox @ 0x2aab013cdab8              264             968,320         0.06% 
    org.xhtmlrenderer.css.style.CalculatedStyle @ 0x2aaafd7f7598    96              926,520         0.06% 
    org.xhtmlrenderer.render.BlockBox @ 0x2aab01cbb710              264             484,016         0.03% 
    org.xhtmlrenderer.css.style.CalculatedStyle @ 0x2aab00275a80    96              464,088         0.03% 
    org.xhtmlrenderer.render.BlockBox @ 0x2aab020e5be0              264             242,032         0.02% 
    org.xhtmlrenderer.css.style.CalculatedStyle @ 0x2aab013cd390    96              234,888         0.02% 
    org.xhtmlrenderer.render.BlockBox @ 0x2aaad4b25228              264             140,280         0.01% 
    org.xhtmlrenderer.render.LineBox @ 0x2aab0294a8f8               224             139,744         0.01% 
    org.xhtmlrenderer.layout.Layer @ 0x2aab02947d58                 136             139,656         0.01% 
    org.xhtmlrenderer.render.BlockBox @ 0x2aab02306478              264             121,352         0.01% 
    org.xhtmlrenderer.css.style.CalculatedStyle @ 0x2aab01cbafe8    96              118,272         0.01% 

    Total: 20 entries                                               3,584           132,753,824     0.086 

我尝试过谷歌搜索 iText 内存性能增强方法,但很不幸!!请指教!!!顺便说一句...会使用renderer.finishPDF()工作吗?

以下命令用于运行 jboss:

java -Dprogram.name=run.sh -server -Xms256m -Xmx1024m -XX:PermSize=64m -XX:MaxPermSize=256m -verbose:gc -Xloggc:/data1/logs/jboss/GC.log -XX:+HeapDumpOnOutOfMemoryError -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=false -Djava.net.preferIPv4Stack=true -Djava.library.path=/usr/local/java/jboss-4.2.2.GA/bin/native -Djava.endorsed.dirs=/usr/local/java/jboss-4.2.2.GA/lib/endorsed -classpath /usr/local/java/jboss-4.2.2.GA/bin/run.jar:/usr/local/java/jdk1.6.0_06/lib/tools.jar org.jboss.Main -c default -b <IP_ADDRESS> -Djboss.messaging.ServerPeerID=1

提前致谢...

编辑:

PS:我假设这是由于 iText!

4

1 回答 1

1

renderer.finishPDF()应该做你提到的伎俩。

如果您尝试渲染的页面有很多图像,您可能需要稍微增加最大内存。

于 2011-03-18T17:04:39.587 回答