【问题标题】:Exporting content to pdf without using iText不使用 iText 将内容导出为 pdf
【发布时间】:2014-08-22 18:22:39
【问题描述】:

我一直致力于使用表 id 将 html(table) 内容导出到 excel。我使用过像

这样的内容类型
 response.getWriter().write(datatoexport);
 response.setHeader("Content-Type", "application/vnd.ms-excel");
 response.setHeader("Content-Disposition", "attachment; filename=test_file.xls");
 response.getWriter().flush();
 response.getWriter().close();

这里,datatoexport 是表 id。

在 excel 上运行良好。

但是,如果我使用内容类型为 pdf 之类的

 response.setHeader("Content-Type", "application/pdf");
 response.setHeader("Content-Disposition", "attachment; filename=test_file.pdf");

但是,我收到的 pdf 文件已损坏。有什么帮助吗? 不使用 iText 或其他 jars,我该如何实现?尤其是在 IE 8 中

【问题讨论】:

  • 你设置的内容类型为pdf,但实际内容是pdf
  • 将内容类型设置为 pdf 不会使您的输出成为真正的 pdf 文件。 content-type 只是一个标志,而不是服务器的直接命令。
  • 没有。我正在将表格内容写入 pdf,就像 excel 一样。但我的 pdf 文件已损坏。有什么帮助吗?
  • @RafaelOsipov:那么,Rafael 先生,我还能做什么?有什么帮助吗?
  • @user3152748 我已经发布了详细的答案。请检查一下。

标签: java html jsp servlets


【解决方案1】:

在发送pdf文件输出之前,需要在服务器端生成。

要将您的文件转换为 PDF,我建议在无头模式下使用 OpenOffice 和JODConverter

要以无头模式(在 Windows 中)运行 OpenOffice,请运行命令(假设您有 OpenOfficePortable,安装在 C:\Apps

"C:\Apps\OpenOfficePortable\OpenOfficePortable.exe" -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

当您以无头模式启动 OpenOffice 时,使用 JODConverter 库运行一个简单的工作原型:

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import java.io.File;
import java.net.ConnectException;

public class JODConv {  
    public static void main(String[] args) throws ConnectException {

        if (args.length!=2) {
            System.out.println("Usage:\nJODConv <file-to-convert> <pdf-file>");
            System.exit(0);
        }

        String sourceFilePath = args[0];
        String destFilePath = args[1];


        File inputFile = new File(sourceFilePath);
        File outputFile = new File(destFilePath);

        // connect to an OpenOffice.org instance running on port 8100
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
        connection.connect();

        // convert
        DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
        converter.convert(inputFile, outputFile);

        // close the connection
        connection.disconnect();
    }       
}

【讨论】:

  • 我需要在服务器端执行此操作。不在本地主机中。并且不使用任何外部库。我需要像 excel 一样使用它。
  • 当您在同一台服务器上运行 open office 和您的转换代码时,localhost 是可以接受的。如果转换代码和 open office 在不同的服务器上运行,则指定 openoffice 服务器的 ip-address,而不是 localhost。
  • 如果您希望避免使用任何免费/开源库,请查看PDF Format Specification 并制定您自己的转换方法。
  • IE8、Internet Explorer 或其他浏览器与转换任务无关。此任务将在服务器上完成,浏览器在客户端计算机上运行。
  • 有任何工作示例吗? JOD转换器的进程我无法获取。
猜你喜欢
  • 2023-03-29
  • 1970-01-01
  • 2014-03-01
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
  • 2010-11-16
  • 1970-01-01
相关资源
最近更新 更多