简体   繁体   English

不使用iText将内容导出为pdf

[英]Exporting content to pdf without using iText

I have worked on exporting the html(table)contents to excel using table id. 我已经使用表ID将html(table)contents导出到excel。 I have used content type like 我曾经使用过类似的内容类型

 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();

Here, datatoexport is the table id. 在这里,datatoexport是表ID。

It is working fine with excel. 与excel配合正常。

But, if I use content type as pdf like 但是,如果我使用类似pdf的内容类型

 response.setHeader("Content-Type", "application/pdf");
 response.setHeader("Content-Disposition", "attachment; filename=test_file.pdf");

But, I am getting pdf file is corrupted. 但是,我得到的pdf文件已损坏。 Any help? 有什么帮助吗? Without using iText or other jars, How can I achieve it? 在不使用iText或其他jar的情况下,如何实现? Especially in IE 8 尤其是在IE 8中

Before sending pdf file to output, you need to generate it on the server side. 在将pdf文件发送到输出之前,您需要在服务器端生成它。

To convert your file to PDF I recommend to use OpenOffice in headless mode and JODConverter . 要将文件转换为PDF,我建议在无头模式和JODConverter中使用OpenOffice。

To run OpenOffice in headless mode (in Windows) run the command (assume you have OpenOfficePortable, installed in C:\\Apps : 要以无头模式运行OpenOffice(在Windows中),请运行以下命令(假设您已将OpenOfficePortable安装在C:\\Apps

"C:\Apps\OpenOfficePortable\OpenOfficePortable.exe" -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

As you have OpenOffice started in headless mode, run a simple working prototype using JODConverter library: 当您以无头模式启动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