By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,212 Members | 2,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,212 IT Pros & Developers. It's quick & easy.

Merging PDF document with XML file using Java class/servlet

P: 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
Share this question for a faster answer!
Share on Google+

Post your reply

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