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.
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.
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.
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 *
*/
/**
* 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" ) );
/**
* This will print the usage for this document.
*/
private static void usage()
{
System.err.println( "Usage: java org.pdfbox.examples.util.ExtractTextByArea <input-pdf>" );
}
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).
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".
Post by:autobot
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
a bit of art, as a gift, the permaculture playing cards