473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Merging PDF document with XML file using Java class/servlet

4
Hi all, I've tried to google this but could not find a clear solution. I have a Web application developed in JDevloper using mostly html and Javascript. I have a pre-defined PDF form which I merge with a XML file. I want to be able to save the form automatically for the User so as to prevent typing errors i.e. c:/erikaTemp/subFolder/erika_12345.pdf but from what I've seen this can only be done via the the 'File download' pop up which only accepts a file name and even then when I try to open the document I get an error saying 'Adobe could not read 'filename' because.....If I don't include
Expand|Select|Wrap|Line Numbers
  1. response.setHeader("Content-disposition", "attachment; filename=\"erikaForm.pdf\"");
then the PDF forms opens without a problem. Could anyone let me know if there is a way to save the PDF form before it opens and if I have any error in my code?

Thanks

Here is my code

Expand|Select|Wrap|Line Numbers
  1. package blAdmin;
  2. import com.softwareag.entirex.aci.Broker;
  3. import java.io.File;
  4. import java.util.*;
  5. import java.io.InputStream;
  6. import javax.servlet.ServletContext;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import org.dom4j.Document;
  11. import org.dom4j.DocumentHelper;
  12. import org.dom4j.Element;
  13. import org.dom4j.Node;
  14. import org.dom4j.io.SAXReader;
  15. import NSFASGenInfo.*;
  16.  
  17. public class NSFASLoanAgreement extends HttpServlet
  18. {
  19.   public void doPost(HttpServletRequest request, HttpServletResponse response) {
  20.     doGet(request, response);
  21.   }
  22.  
  23.   public void doGet(HttpServletRequest request, HttpServletResponse response) {
  24.         try{
  25.             Utilities.setEtbIp(request.getServerName());
  26.             String protocol=request.getProtocol();
  27.             if (protocol.toUpperCase().startsWith("HTTPS")) {
  28.                 protocol="https";
  29.             } else {
  30.                 protocol="http";
  31.             }
  32.             String urlBase=protocol+"://"+request.getServerName()+":"+request.getServerPort()+
  33.                 request.getContextPath()+"/";
  34.  
  35.             String usNumber = request.getParameter("pInputUsnr");
  36.  
  37.             InputStream stream = getServletContext().getResourceAsStream("/htdocs/erikaTemp/testData.xml");
  38.  
  39.             StringBuffer responseString = new StringBuffer();
  40.             response.setContentType("application/vnd.adobe.xdp+xml");
  41.             response.setHeader("Content-disposition", "attachment; filename=\"loanAgreementForm.pdf\"");
  42.             responseString.append( "<?xml version='1.0' encoding='UTF-8'?>");
  43.             responseString.append("<?xfa generator='AdobeDesigner_V7.0' APIVersion='2.2.4333.0'?>");
  44.             responseString.append("<xdp:xdp xmlns:xdp='http://ns.adobe.com/xdp/'>");
  45.             responseString.append("<xfa:datasets xmlns:xfa='http://www.xfa.org/schema/xfa-data/1.0/'>");
  46.             responseString.append("<xfa:data xfa:dataNode='dataGroup'>");
  47.  
  48.             responseString.append(getString(getServletContext(), usNumber,stream)).append("/n");
  49.             String serverInfo = request.getServerName(); //+ ":" + request.getServerPort();
  50.  
  51.             responseString.append("</xfa:data>");
  52.             responseString.append("</xfa:datasets>");
  53.             responseString.append("<pdf href='").append(urlBase).append("htdocs/erikaTemp/testForm.pdf' xmlns='http://ns.adobe.com/xdp/pdf/' />");
  54.             responseString.append("<xfdf xmlns='http://ns.adobe.com/xfdf/' xml:space='preserve'>");
  55.             responseString.append("<annots/>");
  56.             responseString.append("</xfdf>");
  57.             responseString.append("</xdp:xdp>");
  58.             stream.close();
  59.             response.getWriter().write(responseString.toString());
  60.             response.getWriter().flush();
  61.         }catch(Exception exa){
  62.             exa.printStackTrace();
  63.             try{
  64.               response.setContentType("text/html");
  65.               response.getWriter().write(exa.getMessage());
  66.               response.getWriter().flush();             
  67.             }catch(Exception ex){
  68.             }
  69.         }
  70.     }
  71.  
  72.   public static String getString(ServletContext context, String usNumber, InputStream stream) throws Exception {
  73.         try {
  74.             SAXReader reader = new SAXReader();
  75.             Document document = reader.read(stream);
  76.  
  77.             Dbj502n1 natd = new Dbj502n1(new Broker(Utilities.getEtbIp(), "JavaUser"), "RPC/SRV1/CALLNAT");
  78.             natd.setDbj502n1I_us_nommer(usNumber);
  79.             natd.dbj502n1();
  80.  
  81.             GenericInformationServiceStub gis = new GenericInformationServiceStub();
  82.             GenericInformation gi = gis.getGenericInformation(natd.getDbj502n1O_id());
  83.  
  84.             fillDocument(document.getRootElement(), natd, gi);
  85.  
  86.             return document.asXML().substring(38);
  87.  
  88.         } catch (Exception exa) {
  89.             exa.printStackTrace();
  90.             throw exa;
  91.         }
  92.  
  93.     }
  94.  
  95.     private static void fillDocument(Element rootElement, Dbj502n1 dbj502n1, GenericInformation gi) {
  96.  
  97.         String studAccNo = String.valueOf(gi.getAccountNo());
  98.         String rate = gi.getInterestRate();
  99.  
  100.         ((Node) DocumentHelper.createXPath("//gender").selectNodes(rootElement).get(0)).setText(dbj502n1.getDbj502n1O_gender());
  101.            }
  102. }  
Jul 18 '07 #1
0 2551

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jagdeesh | last post by:
Hai Colleagues, I am using Tomcat 4.1.24 and JDK 1.4.0_03 in my winXP machine. I've transferred a set of folders(containing jsp files) into tomcat's webapps directory(to /webapps/bob ,...
0
by: Phil Powell | last post by:
Ok, I installed ANT, I have c:\ant in my ANT_HOME environment variable, I have c:\ant\bin in my PATH environmental variable, I installed the JAR files for TCL and for Javascript (Rhino)... to no...
6
by: Mike Daniel | last post by:
I am attempting to use document.write(pageVar) that displays a new html page within a pop-up window and the popup is failing. Also note that pageVar is a complete HTML page containing other java...
1
by: kommaraju | last post by:
iam a starter to db2 & jdbc.i have a servlet program which connects to ibm db2 using jdbc.when i run this using apache tomcat 4.1.34 , it is showing a error message of HTTP STATUS 500 my jdbc...
0
by: TraceyAnnison | last post by:
I wonder if you can help me - I'm new to this, and working in a project that has already been configured to work with Axis. We have a Java project built with the Spring framework, in order to...
1
by: rohitkec | last post by:
I am new to both java andb apache tomcat.i am presently using apache tomcat 4.1.31 & j2re-1_4_2_10.when i am running http://127.0.0.1:8080/admin/login.jsp or admin file i am getting the following...
1
by: sathyak83 | last post by:
i want to format the numbers using css for each country ie if country is france the format=0.0.0 if country is UK=00.00 i use the below code <LINK REL=stylesheet HREF="<%= new...
2
by: praveenkumarvpk | last post by:
Hi friends Please help me! Following is my servlet-code import java.util.Enumeration; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import...
1
by: shyaminf | last post by:
hi everybody! iam facing a problem with the transfer of file using servlet programming. i have a code for uploading a file. but i'm unable to execute it using tomcat5.5 server. kindly help me how to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.