java - 将 XLS 转换为 PDF 的 Web 服务

标签 java tomcat pdf-generation jodconverter

我正在开发一个生成 XLS 文件作为输出的 Java 小程序。在让用户查看、下载和打印文档之前,我需要将它转换为 PDF(或者更好的 PDF/A),因为我需要它不可修改。

我尝试编写一个解决方案,它有效。我在我的小程序中下载并捆绑了 JODConverter 2,以便输出 XLS 成为 JODConverter 输入文件并且一切正常。问题是这个组件的大小:差不多 2mb。因为我的小程序已经是 3mb,所以我不想在其中捆绑 JODConverter...

我在文档中读到它也可以作为网络服务工作:我创建一个 POST 请求,将其发送到服务并获取文件,所有这些都无需下载一个 kb 的 JODConverter。听起来不错,但我无法正常工作。

下面是我写的代码:

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();
        }
    }
}

我下载了一个开箱即用的包含 JODConverter Web 服务的 WAR,并将其放入我的 tomcat\webapps\pdfconverter,创建了启动 OpenOffice 服务的脚本

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

但这是我的堆栈跟踪:

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)

Main.java:128 是行

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

Tomcat 日志显示: 访问日志:

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

[将内容类型从 text/plain 更改为 multipart/form-data 后进行编辑] 标准日志:

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)

我相信POST有问题,因为在转换过程中发生了异常,但我似乎找不到问题所在:发布的代码是经过多次试验和错误的最后版本,所以也许在此之前,我为 POST 使用了正确的代码并搞砸了其他东西...... 任何建议表示赞赏!

最佳答案

起初你发送 .xls 文件,Mime-Typetext/plain 并且你得到异常说它无法由于某些错误解析您的文档(可能是您的 JODConverter 2 试图将 .xls 文件转换为 .txt 文件)。

这是第一个异常(exception)。现在你得到了 multipart/form-data 不受支持的异常。此 Mime-Type 从不代表文档,如果您想将一个文档更改为 .pdf,则必须提供有效的 Mime-Type 定义。

对于 .xls 文件,这个有效的 Mime-Typeapplication/vnd.ms-excel。您可以在这里找到其他类型:mime-types for xls .

此更改应允许您将请求发送到 JODConverter

关于java - 将 XLS 转换为 PDF 的 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14602342/

相关文章:

Java系统环境变量当前用户

java - Spring Boot - 无法启动嵌入式tomcat错误

c# - 合并带有书签的 pdf 文件

pdf - 打印时隐藏 PDF 中的文本

java - 使用 TextIO 读取外部文件

java - 更改 NatTable 中选定行的颜色

tomcat - Azure 网站不会为默认网页加载 ROOT.war

java - 获取表格中单元格的X和Y坐标

java - Netty 日志现在显示 "low-level API for accessing direct buffers reliably"

spring - PermGen 内存不足 - Netbeans