title: Webservice to convert XLS to PDF
body: <p>I'm working on a Java applet that produces an XLS file as output. I need to convert it to PDF (or even better PDF/A) before I let the user view, download and print the document, since I need it unmodifyable.</p>
  
  <p>I tried coding a solution, and it works. I downloaded and bundled JODConverter 2 in my applet so that the output XLS becomes JODConverter input file and it all works ok. The problem is the size of this component: almost 2mb. Since my applet is already 3mb I don't want to bundle JODConverter in it too...</p>
  
  <p>I read in the docs that it can work as a webservice too: I create a POST request, send it to the service and get the file, all without downloading a single kb of JODConverter. It sounds great, but I can't get it working.</p>
  
  <p>Below is the code I wrote:</p>
  
  <pre><code>public class Main
  {
      public static void main(String[] args) throws Exception
      {
          doPost(new URL("http://localhost:8090/pdfconverter/service"), "C:\\Documents and Settings\\Administrator\\Desktop\\Gestione oneri\\calcolo oneri XP.xls");
      }
  
  
      private static void doPost(URL url, String binaryFile)
      {
          try
          {
              File binFile = new File(binaryFile);
              URLConnection conn = url.openConnection();
              conn.addRequestProperty("Content-Type", "multipart/form-datastrong text");
              conn.addRequestProperty("Accept", "application/pdf");
              conn.setDoOutput(true);
              DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
              FileInputStream input = new FileInputStream(binFile);
              byte[] buffer = new byte[1024];
              for(int length = 0; (length = input.read(buffer)) &gt; 0;)
              {
                  wr.write(buffer, 0, length);
              }
              wr.flush(); // Important! Output cannot be closed. Close of writer will close output as well.
              // Get the response and write it to a file.
              File file = new File("C:\\Documents and Settings\\Administrator\\Desktop\\Gestione oneri\\calcolo oneri XP2.pdf");
              FileOutputStream wrFile = new FileOutputStream(file);
              DataInputStream dataInput = new DataInputStream(conn.getInputStream());
              buffer = new byte[1024];
              for(int length = 0; (length = dataInput.read(buffer)) &gt; 0;)
              {
                  wrFile.write(buffer, 0, length);
              }
          }
          catch(Exception e)
          {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>I downloaded a WAR which included JODConverter web service out of the box and put it into my tomcat\webapps\pdfconverter, created the script to start OpenOffice service</p>
  
  <pre><code>soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard
  </code></pre>
  
  <p>but here's my stacktrace:</p>
  
  <pre><code>java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8090/pdfconverter/service
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
  at main.Main.doPost(Main.java:128)
  at main.Main.main(Main.java:25)
  </code></pre>
  
  <p>where Main.java:128 is the line</p>
  
  <pre><code>DataInputStream dataInput = new DataInputStream(conn.getInputStream());
  </code></pre>
  
  <p>Tomcat logs show this:
  Access log:</p>
  
  <pre><code>127.0.0.1 - - [30/Jan/2013:11:45:24 +0100] "POST /pdfconverter/service HTTP/1.1" 500 4426
  </code></pre>
  
  <p>[EDIT after changing content-type from text/plain to multipart/form-data]
  Standard log:</p>
  
  <pre><code>Grave: Servlet.service() for servlet [DocumentConverterServiceServlet] in context with path [/pdfconverter] threw exception
  java.lang.IllegalArgumentException: unsupported input mime-type: multipart/form-data
  at com.artofsolving.jodconverter.web.DocumentConverterServiceServlet.doPost(DocumentConverterServiceServlet.java:69)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
  at java.lang.Thread.run(Thread.java:722)
  </code></pre>
  
  <p>I believe there's something wrong in the POST, because an exception has occurred during conversion process, but I can't seem to find the problem: the code posted is the last version of a various numbers of trials and errors, so maybe before this I went with the right code for the POST and messed up something else...
  Any suggestion is appreciated!</p>