我一直致力于使用table 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();在这里,数据导出是表id。
它在excel上运行得很好。
但是,如果我将内容类型用作pdf,如
response.setHeader("Content-Type", "application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=test_file.pdf");但是,我得到的pdf文件是损坏的。有什么帮助吗?如果不使用iText或其他jars,我如何实现它?特别是在IE 8中
发布于 2014-07-02 04:53:31
在发送pdf文件输出之前,您需要在服务器端生成它。
要将您的文件转换为PDF格式,我建议在无头模式下使用OpenOffice和JODConverter。
若要以无头模式(在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();
}
}https://stackoverflow.com/questions/24523010
复制相似问题