2011-11-07 111 views
-1

我正在爲我的一個性能項目做POC,目前它給了我當前情景中的內存不足錯誤,因爲我們已經將該xml加載爲DOM,然後將該XSL轉換爲PDF,這給OOMemory錯誤,Java轉換爲PDF

僅從這個論壇的意見之一後,我已經使用SAX解析器爲XML文件,並給XSL,然後用FOP這個30 MB,系統內存爲512MB

System.out.println("FOP XMLTOPDFConverter\n"); 
      System.out.println("Preparing..."); 

      // Setup directories 
/*   File baseDir = new File("."); 
      File outDir = new File(baseDir, "out"); 
      outDir.mkdirs();*/ 

      // Setup input and output files 
      File xmlfile = new File("C:/Documents and Settings/agarwgau/Desktop/300k/File_0000036357.XML"); 
      File xsltfile = new File("C:/Documents and Settings/agarwgau/Desktop/300k/UCB110037EventList.xsl"); 
      File pdffile = new File("C:/Documents and Settings/agarwgau/Desktop/300k/ResultXML2PDF.pdf"); 

      System.out.println("Input: XML (" + xmlfile + ")"); 
      System.out.println("Stylesheet: " + xsltfile); 
      System.out.println("Output: PDF (" + pdffile + ")"); 
      System.out.println(); 
      System.out.println("Transforming..."); 

      // configure fopFactory as desired 
      FopFactory fopFactory = FopFactory.newInstance(); 

      FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); 
      // configure foUserAgent as desired 

      // Setup output 
      OutputStream out = new java.io.FileOutputStream(pdffile); 
      out = new java.io.BufferedOutputStream(out); 

      try { 
       // Construct fop with desired output format 
       Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, 
         foUserAgent, out); 

       // Setup XSLT 
       TransformerFactory factory = TransformerFactory.newInstance(); 
       Transformer transformer = factory 
         .newTransformer(new StreamSource(xsltfile)); 

       // Set the value of a <param> in the stylesheet 
       transformer.setParameter("versionParam", "2.0"); 

       // Setup input for XSLT transformation 
       Source src = new StreamSource(xmlfile); 

       // Resulting SAX events (the generated FO) must be piped through 
       // to FOP 
       Result res = new SAXResult(fop.getDefaultHandler()); 

       // Start XSLT transformation and FOP processing 
       transformer.transform(src, res); 
      } finally { 
       out.close(); 
      } 

      System.out.println("Success!"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      System.exit(-1); 
     } 

    } 
+0

是否要將XML處理爲某種人類可讀的格式,或者將XML內容本身放入PDF文件(可能使用語法高亮顯示)? – Bruno

+0

一些可讀的格式? ' \t 蘇德赫 \t 馬杜 \t Deepti \t 拉夫 ' PDF它會是這樣 後------- ------------------------- ' 父親\t母親\t \t FirstChild \t SecondChild – gaurav

+0

請僅編輯您的原始文章,而不是嘗試在評論中對問題進行新的描述。謝謝。 – sarnold

回答

1

這聽起來像Apache FOP應該能夠幫助你。

+0

我對Apache FOP是否能夠將25MB文件轉換爲PDF有困惑,我還沒有開始我的工作,仍然在谷歌上尋找它 – gaurav

+2

我不知道(這將取決於你有多少RAM) ,但25MB聽起來不那麼大,尤其是因爲Apache FOP使用SAX:http://xmlgraphics.apache.org/fop/dev/design/parsing.html#input – Bruno

+0

根據我的知識,RAM在服務器上很好。 。as it; sa Prod。服務器將最終部署...讓我試試這個FOP感謝這個有用的 – gaurav

1

您有兩種選擇:FOP(它是PDF的XML表示)或iText(使用Java類創建PDF)。

我推薦FOP與Velocity結合使用。製作一個代表您的FO XML的Velocity模板並將數據映射到其中。

+0

讓我試試velocity模板也謝謝 – gaurav