Question

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.

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...

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.

Below is the code I wrote:

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)) > 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)) > 0;)
            {
                wrFile.write(buffer, 0, length);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

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

soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard

but here's my stacktrace:

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)

where Main.java:128 is the line

DataInputStream dataInput = new DataInputStream(conn.getInputStream());

Tomcat logs show this: Access log:

127.0.0.1 - - [30/Jan/2013:11:45:24 +0100] "POST /pdfconverter/service HTTP/1.1" 500 4426

[EDIT after changing content-type from text/plain to multipart/form-data] Standard log:

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)

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!

Was it helpful?

Solution

At first you were sending .xls file with Mime-Type as text/plain and you got exception which said that it's unable to parse your document because of some error (probably your JODConverter 2 was trying to convert .xls file as .txt file).

This was first exception. Now you are getting exception that multipart/form-data is unsuported. This Mime-Type never represent a document and if you want to change one doc in another one as .pdf you have to provide valid Mime-Type definition.

For .xls files this valid Mime-Type is application/vnd.ms-excel. You can found other types here: mime-types for xls.

This change should allow you to send your request to JODConverter

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top