繁体   English   中英

不使用iText将内容导出为pdf

[英]Exporting content to pdf without using iText

我已经使用表ID将html(table)contents导出到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的情况下,如何实现? 尤其是在IE 8中

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

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

要以无头模式运行OpenOffice(在Windows中),请运行以下命令(假设您已将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();
    }       
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM