File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services and the fly likes want to send pdf file to client from server. pdf is generated at server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "want to send pdf file to client from server. pdf is generated at server" Watch "want to send pdf file to client from server. pdf is generated at server" New topic
Author

want to send pdf file to client from server. pdf is generated at server

lokesh jain
Ranch Hand

Joined: Feb 21, 2009
Posts: 58
Hi, this is my first question here. I am working in web services in java on Netbeans 6.1. I have created web service to fetch records from DB and stored the result in pdf. Now my client want to open and save that pdf file on his local machine. Can any one help me on this. please send me the code.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
"lucky jenny",

Please check your private messages regarding an important administrative matter.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Sounds like the web service portion of the process is already complete, so that remaining problems are
  • Obtaining a location to save the file to.
  • Saving the file to the file system.
  • Viewing the file from the file system.

  • Example: Opening PDF file in a standalone Java Swing application
    lokesh jain
    Ranch Hand

    Joined: Feb 21, 2009
    Posts: 58
    No, My problem is that the pdf file generated is on Server machine and my client wants to view and save it on his local machine. For that I need the code, if possible.

    Thanks in advance
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 42263
        
      64
    This may be of interest: Transferring files over a web service


    Ping & DNS - my free Android networking tools app
    lokesh jain
    Ranch Hand

    Joined: Feb 21, 2009
    Posts: 58
    Can i send the pdf file from server to client using byte array? Please send me the code.
    Thanks in advance
    Balaji Loganathan
    author and deputy
    Bartender

    Joined: Jul 13, 2001
    Posts: 3150
    lokesh jain wrote:Can i send the pdf file from server to client using byte array? Please send me the code.
    Thanks in advance

    You can send/receive it as byte array so that you dont need to scratch your head about interoperability with non-java client.

    On the other hand you can also use SAAJ API along with Axis1.2 to send and receive file as soap attachment. Worked out sample can be found here.

    >>Please send me the code ?
    What code you are looking for ?
    Many sample codes can be found in the samples directory of Axis1.2.

    Regards
    Balaji

    Spritle Software Blogs
    Peer Reynders
    Bartender

    Joined: Aug 19, 2005
    Posts: 2922
        
        5
    Balaji Loganathan wrote:On the other hand you can also use SAAJ API along with Axis1.2 to send and receive file as soap attachment.


    Given that Netbeans 6.1 is being used, JAX-WS should already be supported and MTOM should be the first choice for binary attachments.
    Preet Prasannan.
    Ranch Hand

    Joined: Feb 09, 2009
    Posts: 64
    Sorry removing the post as Ulf Dittmer very correctly pointed out that the conversion can be done from XML to PDF and not the other way.
    Thanks Ulf and sorry Lokesh.

    "The more I learn,the more,I get to know, is left to learn."
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 42263
        
      64
    Preet Prasannan wrote:Another solution can be : Using a coverter like Apache FOP,u can convert it to an XML.

    Apache FOP can NOT convert PDF to XML; it does just the opposite - create a PDF from XML with the help of an XSL-FO transformation.
    Preet Prasannan.
    Ranch Hand

    Joined: Feb 09, 2009
    Posts: 64
    I have tried to figure out a way to get the pdf contents as string.You can then convert these string objects as per your requirement.
    Hope this helps..Please find the java code below.YOu can test it with a pdf.You willl need some jars.You can get them at findjar.com and the main jar at www.pdfbox.org.


    //////////////////////////////////
    /**
    * Copyright (c) 2005, www.pdfbox.org
    * All rights reserved.
    *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions are met:
    *
    * 1. Redistributions of source code must retain the above copyright notice,
    * this list of conditions and the following disclaimer.
    * 2. Redistributions in binary form must reproduce the above copyright notice,
    * this list of conditions and the following disclaimer in the documentation
    * and/or other materials provided with the distribution.
    * 3. Neither the name of pdfbox; nor the names of its
    * contributors may be used to endorse or promote products derived from this
    * software without specific prior written permission.
    *
    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
    * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
    * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    *
    * http://www.pdfbox.org
    *
    */


    import org.pdfbox.exceptions.InvalidPasswordException;

    import org.pdfbox.pdmodel.PDDocument;
    import org.pdfbox.pdmodel.PDPage;
    import org.pdfbox.util.PDFTextStripperByArea;

    import java.awt.Rectangle;

    import java.util.List;

    /**
    * This is an example on how to extract text from a specific area on the PDF document.
    *
    *
    */
    public class ExtractTextByArea
    {
    private ExtractTextByArea()
    {
    //utility class and should not be constructed.
    }


    /**
    * This will print the documents text in a certain area.
    *
    * @param args The command line arguments.
    *
    * @throws Exception If there is an error parsing the document.
    */
    public static void main( String[] args ) throws Exception
    {
    /*if( args.length != 1 )
    {
    usage();
    }*/
    //else
    {
    PDDocument document = null;
    try
    {
    document = PDDocument.load( "a.pdf");
    if( document.isEncrypted() )
    {
    try
    {
    document.decrypt( "" );
    }
    catch( InvalidPasswordException e )
    {
    System.err.println( "Error: Document is encrypted with a password." );
    System.exit( 1 );
    }
    }
    PDFTextStripperByArea stripper = new PDFTextStripperByArea();
    stripper.setSortByPosition( true );
    Rectangle rect = new Rectangle( 0, 0, 875, 960 );
    stripper.addRegion( "class1", rect );
    List allPages = document.getDocumentCatalog().getAllPages();
    PDPage firstPage = (PDPage)allPages.get( 0 );
    stripper.extractRegions( firstPage );
    System.out.println( "Text in the area:" + rect );
    System.out.println( stripper.getTextForRegion( "class1" ) );

    }
    finally
    {
    if( document != null )
    {
    document.close();
    }
    }
    }
    }

    /**
    * This will print the usage for this document.
    */
    private static void usage()
    {
    System.err.println( "Usage: java org.pdfbox.examples.util.ExtractTextByArea <input-pdf>" );
    }

    }
    Peer Reynders
    Bartender

    Joined: Aug 19, 2005
    Posts: 2922
        
        5
    The following is a JDK 6 example of a JAX-WS SOAP web service that stores and returns PDF files.
    Consider the following XML Schema:

    Both documentContent elements are based on {http://www.w3.org/2001/XMLSchema}base64Binary and the {http://www.w3.org/2005/05/xmlmime}expectedContentTypes attributes triggers the mapping to standard Java types. In the case of application/pdf the content will be mapped to a javax.activation.DataHandler (see also 6.2.1.2. xmime:expectedContentType to Java type mapping).

    This schema can be used in a web service contract

    Before processing some binding files are necessary to specify some options.

    A JAX-WS binding file
    which maps the Java web service artifacts to the com.coderanch.jaxws.mtompdf.ws package.

    A JAXB external binding customization file
    which maps the JAXB artifacts to the com.coderanch.jaxws.mtompdf.dto package.
    wsimport PdfStore.wsdl -b PdfStore-jaxws.xml -b PdfStore.xjb -s src
    generates the following Java artifacts:
  • com.coderanch.jaxws.mtompdf.ws.PdfStore - the endpoint interface (SEI)
  • com.coderanch.jaxws.mtompdf.ws.PdfStoreService - the web service client proxy
  • com.coderanch.jaxws.mtompdf.ws.PdfStoreException - the web service Exception/Fault
  • com.coderanch.jaxws.mtompdf.dto.GetRequest class to represent {http://mtompdf.jaxws.coderanch.com/}Get.
  • com.coderanch.jaxws.mtompdf.dto.GetResponse class to represent {http://mtompdf.jaxws.coderanch.com/}GetResponse.
  • com.coderanch.jaxws.mtompdf.dto.PutRequest class to represent {http://mtompdf.jaxws.coderanch.com/}Put.
  • com.coderanch.jaxws.mtompdf.dto.PutResponse class to represent {http://mtompdf.jaxws.coderanch.com/}PutResponse.
  • com.coderanch.jaxws.mtompdf.dto.PdfStoreFault class to represent {http://mtompdf.jaxws.coderanch.com/}PdfStoreFault.
  • com.coderanch.jaxws.mtompdf.dto.ObjectFactory - the JAXB ObjectFactory.
  • package-info.java containing the JAXB package level annotations.


  • We still need a utility class to deal with the PDF files and DataHandlers:

    Now the Web Service can be implemented.

    Note how @javax.xml.ws.soap.MTOM is used to turn on MTOM support.

    The code to host the endpoint (with com.sun.net.httpserver.HttpServer).

    To compile everything:
    javac -d . src\com\coderanch\jaxws\mtompdf\util\*.java
    javac -d . src\com\coderanch\jaxws\mtompdf\provider\*.java
    wsgen -cp . com.coderanch.jaxws.mtompdf.provider.PdfStoreImpl


    Start the web service with:
    java com.coderanch.jaxws.mtompdf.provider.Main

    Point a browser to http://localhost:8080/wsdemo/pdfstore?wsdl to see the JAX-WS generated WSDL.

    Open a separate console window to work on the web service consumer.

    Note the use of javax.xml.ws.soap.MTOMFeature to enable MTOM on the client side.
    Compile it with
    javac -d . src\com\coderanch\jaxws\mtompdf\consumer\PdfStoreClient.java
    and run it with
    java -cp . com.coderanch.jaxws.mtompdf.consumer.PdfStoreClient
    after you place a test.pdf file in the working directory. test1.pdf should appear after the client "puts" the file to the service. test2.pdf should appear after the client "gets" "test1" and saves it as "test2".
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: want to send pdf file to client from server. pdf is generated at server