Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不使用iText将内容导出为pdf_Java_Html_Jsp_Servlets - Fatal编程技术网

Java 不使用iText将内容导出为pdf

Java 不使用iText将内容导出为pdf,java,html,jsp,servlets,Java,Html,Jsp,Servlets,我曾使用表id将html(表)内容导出到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(); re

我曾使用表id将html(表)内容导出到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或其他jar,我如何实现它?特别是在IE8中,在将pdf文件发送到输出之前,需要在服务器端生成它

要将您的文件转换为PDF,我建议在headless模式下使用OpenOffice,然后使用

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

"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();
    }       
}
导入com.artofsolving.jodconverter.DocumentConverter;
导入com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
导入com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
导入com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
导入java.io.File;
导入java.net.ConnectException;
公共类JODConv{
公共静态void main(字符串[]args)引发ConnectException{
如果(参数长度!=2){
System.out.println(“用法:\nJODConv”);
系统出口(0);
}
字符串sourceFilePath=args[0];
字符串destFilePath=args[1];
文件输入文件=新文件(sourceFilePath);
File outputFile=新文件(destFilePath);
//连接到在端口8100上运行的OpenOffice.org实例
OpenOfficeConnection连接=新的SocketOpenOfficeConnection(8100);
connection.connect();
//皈依
DocumentConverter converter=新OpenOfficeDocumentConverter(连接);
convert.convert(inputFile,outputFile);
//关闭连接
连接断开();
}       
}

您将内容类型设置为
pdf
,但实际内容是否为
pdf
?将内容类型设置为pdf不会使输出成为真正的pdf文件。内容类型只是一个标志,但不是服务器的直接顺序。否。我正在将表内容像excel一样写入pdf。但我的pdf文件已损坏。有什么帮助吗?@RafaelOs伊波夫:那么,拉斐尔先生,我还能做什么?有什么帮助吗?@user3152748我已经发布了一个详细的答案。请检查一下。我需要在服务器端做这件事。不是在localhost。而且不使用任何外部库。我需要像excel一样使用它。因为您在同一台服务器上运行open office和您的转换代码,那么localhost是可以接受的。如果转换代码和open office在不同的服务器上运行,然后指定openoffice服务器的ip地址,而不是本地主机。如果您希望避免使用任何免费/开源库,请检查并创建自己的转换方法。IE8、Internet Explorer或其他浏览器与转换任务无关。此任务k将在服务器上完成,浏览器在客户端计算机上运行。是否有任何工作示例?我无法获得JOD转换器的过程。