我使用表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,我建议在无头模式下使用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();
}
}
关于java - 不使用iText将内容导出为pdf,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24523010/