2016-11-23 67 views
0

詳情:當我點擊一個URL時,它給了我一個需要轉換爲PDF的jsp頁面。現在我正在使用Java組件中的ITextPDF來讀取jsp並將其寫爲PDF。在沒有使用ITextPDF的情況下,是否有任何替代過程?如何在MULE中將傳入的JSP頁面轉換爲PDF?

import java.io.BufferedReader; 
import java.io.ByteArrayOutputStream; 
import java.io.FileOutputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import org.mule.api.MuleEventContext; 
import org.mule.api.lifecycle.Callable; 
import org.mule.api.transport.PropertyScope; 

import com.itextpdf.text.Chunk; 
import com.itextpdf.text.Document; 
import com.itextpdf.text.PageSize; 
import com.itextpdf.text.pdf.PdfWriter; 
public class PDFConversion implements Callable { 

private final String USER_AGENT = "Mozilla/5.0"; 
StringBuffer response = new StringBuffer(); 
ByteArrayOutputStream bos; 
byte[] result; 
@Override 
public Object onCall(MuleEventContext eventContext) throws Exception { 
    try { 

     String productid=""; 
     String vertical=""; 
     String postcode=""; 
     String metertype=""; 
     String includeSolar=""; 
     productid=eventContext.getMessage().getInvocationProperty("productId"); 
     vertical=eventContext.getMessage().getInvocationProperty("vertical"); 
     postcode=eventContext.getMessage().getInvocationProperty("postcode"); 
     metertype=eventContext.getMessage().getInvocationProperty("metertype"); 
     includeSolar=eventContext.getMessage().getInvocationProperty("includeSolar"); 



     String url = "http://191.111.0.111:****/PDFService/electricitypdf?productid="+productid+"&vertical="+vertical+"&postcode="+postcode+"&metertype="+"&includeSolar="+includeSolar; 
     System.out.println(" URL -Request-----"+url); 
     URL obj = new URL(url); 
     HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 
     // optional default is GET 
     con.setRequestMethod("GET"); 
     // add request header 
     con.setRequestProperty("User-Agent", USER_AGENT); 
     int responseCode = con.getResponseCode(); 
     System.out.println("\nSending 'GET' request to URL : " + url); 
     System.out.println("Response Code : " + responseCode); 
     BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); 
     String inputLine; 
     bos = new ByteArrayOutputStream(); 
     int next = in.read(); 
     while (next > -1) { 
      bos.write(next); 
      next = in.read(); 
     } 
     bos.flush(); 

/ByteArrayOutputStream buffer = new ByteArrayOutputStream(); byte[] bytes = IOUtils.toByteArray(in); while ((inputLine = in.readLine()) != null) { response.append(inputLine); }/ in.close(); 

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

    Document document = new Document(PageSize.LETTER, 0.75F, 0.75F, 0.75F, 0.75F); 
    document.setPageSize(PageSize.LETTER.rotate()); 
    // PdfWriter.setPageEvent(new HeaderFooter(mmPDFDocument.right() - 
    // mmPDFDocument.left(), footer, path, headerType, unicodeFont)); 

    PdfWriter.getInstance(document, bos); 

    document.open(); 

    int numberpages=10; 
    for (int i = 1; i <= numberpages; i++) 
    {  
        document.add(new Chunk("Hello PDF Service - Page "+i)); 
        document.newPage();  
    }   
    FileOutputStream fos= new FileOutputStream("energy.pdf"); 
    fos.write(bos.toByteArray()); 
    fos.close(); 
    document.close(); 

    return bos; 
} 
} 

儘管寫這個瀏覽器是無法顯示的內容爲PDF格式,我可以看到PDF文件正被寫入當URL被擊中它不產生類型(不是PDF)的文件。任何幫助讚賞!

回答

0

您的解決方案基本上是在Mule ESB上運行的標準Java應用程序。 Mule用於基於消息的通信和轉換。如果你想將你的解決方案成爲一個完整的「騾子解決方案」我會建議....

  1. 使用HTTP:請求指令調用JSP頁面即可返回HTML。 實施例這裏:Mule ESB : Read HTML

  2. 寫變壓器以變換text/html的消息給一個字節數組(或一些其他類型的),這將必須使用ITextPDF來完成。

  3. 將字節數組輸出到文件。

你基本上正在做正確的步驟,但沒有使用Mule平臺的任何優勢。當你用這種方法分離解決方案時,你將能夠輕鬆地編寫PDF文件,而不會得到任何奇怪的結果。

相關問題