« Return to Thread: How to send PDF FileStream directly as response???

Re: How to send PDF FileStream directly as response???

by pocketom :: Rate this Message:

Reply to Author | View in Thread

Hi, and thank you for your replies.

Here is the demo source code:




Service Interface:

package com.mycompany.pdfservice;

import java.io.IOException;

import com.lowagie.text.DocumentException;

public interface OnDemandFileService {

        public byte[] serve2File(String request) throws DocumentException, IOException;
}



Service Component

package com.mycompany.pdfservice;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;
import com.mwgbiotechag.pdfengine.Graph2D;
import com.mwgbiotechag.pdfengine.Point2D;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

public class PDFServiceComponent implements OnDemandFileService
{
        @Override
        public byte[] serve2File(String request) throws DocumentException, IOException
        {
                System.out.println("PDFService:Request="+request);
                System.out.println("PDFService:\tNew PDF Document Requested");
               
                //benchmark
                long start = System.currentTimeMillis();  
           
                //pages
                int numberpages = 100;
   
                ByteOutputStream bops = new ByteOutputStream();    
                Document document = new Document();    
                PdfWriter writer = PdfWriter.getInstance(document, bops);    
                document.open();
       
                for (int i = 1; i <= numberpages; i++)
                {
                                document.add(new Chunk("Hello PDF Service - Page "+i));
                                document.newPage();
                }    
                document.close();    
   
 
                long end = System.currentTimeMillis();
                long time= end-start;
   
                System.out.println("PDFService:\tPDF Generation finished");
                System.out.println("PDFService:\tOverall generation time: "+time+" ms+\t("+((float)time/numberpages)+"ms/page)");
                System.out.println("\n");

                return bops.getBytes();
        }



Here the Mule conf.xml:


<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:spring="http://www.springframework.org/schema/beans"
       xmlns:http="http://www.mulesource.org/schema/mule/http/2.2"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
       http://www.mulesource.org/schema/mule/http/2.2 http://www.mulesource.org/schema/mule/http/2.2/mule-http.xsd">


    <description>        
    </description>
   
   
    <http:connector name="HttpConnector" enableCookies="true" keepAlive="true"/>
    <model name="pdfonthefly">
         <service name="pdftest">
            <inbound>
                <http:inbound-endpoint address="http://localhost:9090/pdf" method="GET" synchronous="true" contentType="attachment/pdf"  />
            </inbound>
            <component class="com.mycompany.pdfservice.PDFServiceComponent"/>
  </service>
    </model>    
</mule>

 « Return to Thread: How to send PDF FileStream directly as response???