2012-01-08 62 views
1

我想用的servelt 我使用的飛碟,用來從網站的網址 PDF生成的URL PDF和我的代碼是不產生從HTTP網站的網址PDF

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package abc; 

import com.lowagie.text.DocumentException; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.OutputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import org.xhtmlrenderer.pdf.ITextRenderer; 

public class TestPdf { 
    public static void main(String args[]) throws MalformedURLException 
    { 
     String File_To_Convert = "test.htm"; 
     //String url = new File(File_To_Convert).toURI().toURL().toString(); 
     URL testurl=new URL("http://www.ipillion.com/"); 
     String url=testurl.toString(); 
     System.out.println(""+url); 
     String HTML_TO_PDF = "d:\\ConvertedFile.pdf"; 
     OutputStream os;  
     try { 
      os = new FileOutputStream(HTML_TO_PDF); 


     ITextRenderer renderer = new ITextRenderer(); 
     renderer.setDocument(url);  
     renderer.layout(); 

     renderer.createPDF(os); 

     os.close(); 

}catch(Exception e) 
{ 
    e.printStackTrace(); 
} 
    }} 

我一直在使用不同的網站試過,但每次它拋出異常 爲如

run: 
http://www.ipillion.com/ 
ERROR: 'The element type "SCRIPT" must be terminated by the matching end-tag "</SCRIPT>".' 
org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 71; The element type "SCRIPT" must be terminated by the matching end-tag "</SCRIPT>". 
    at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:191) 
    at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:71) 
    at org.xhtmlrenderer.swing.NaiveUserAgent.getXMLResource(NaiveUserAgent.java:211) 
    at org.xhtmlrenderer.pdf.ITextRenderer.loadDocument(ITextRenderer.java:134) 
    at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:138) 
    at abc.TestPdf.main(TestPdf.java:33) 
Caused by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 71; The element type "SCRIPT" must be terminated by the matching end-tag "</SCRIPT>". 
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:723) 
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:317) 
    at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:189) 
    ... 5 more 
Caused by: org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 71; The element type "SCRIPT" must be terminated by the matching end-tag "</SCRIPT>". 
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1236) 
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:640) 
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:711) 
    ... 7 more 
BUILD SUCCESSFUL (total time: 9 seconds) 

回答

0

您使用的是XHTML渲染器(org.xhtmlrenderer.pdf.ITextRenderer),但該網站是HTML

+0

那麼需要做什麼? – user1047873 2012-01-08 15:44:52

0

問題在於輸入,它與您正在使用的渲染器不兼容。

我會選擇另一個渲染器,或整理輸入,因此它是有效的XHTML。通過http://tidy.sourceforge.net/抽取內容應該可行。

+0

謝謝ii工作正常,因爲給定的網址,但我想健壯的一個將適用於所有網站.so爲此需要做什麼 – user1047873 2012-01-08 15:57:44

+0

PLZ建議一些其他選項 – user1047873 2012-01-08 16:47:08