首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不使用iText将内容导出到pdf

不使用iText将内容导出到pdf
EN

Stack Overflow用户
提问于 2014-07-02 04:40:20
回答 1查看 880关注 0票数 2

我一直致力于使用table id将html(Table)内容导出到excel。我使用过内容类型,如

代码语言:javascript
复制
 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,如

代码语言:javascript
复制
 response.setHeader("Content-Type", "application/pdf");
 response.setHeader("Content-Disposition", "attachment; filename=test_file.pdf");

但是,我得到的pdf文件是损坏的。有什么帮助吗?如果不使用iText或其他jars,我如何实现它?特别是在IE 8中

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-02 04:53:31

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

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

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

代码语言:javascript
复制
"C:\Apps\OpenOfficePortable\OpenOfficePortable.exe" -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

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

代码语言:javascript
复制
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();
    }       
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24523010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档