Java,OpenOffice,创建将文档作为jsp响应作为pdf

时间:2017-08-23 09:39:37

标签: java jsp pdf servlets openoffice.org

我正在尝试在java中集成开放式办公室。我正在做的是创建一个文档,并希望将该文档作为jsp响应以pdf格式获取..但我无法做到这一点。我能够创建一个空白的开放式办公文档,并在其中创建所需的全部文本。然后我希望每当运行这个jsp时,我们将该文档作为pdf格式的响应。

代码是

<%@page import="com.sun.star.text.XParagraphCursor"%>
<%@page import="java.io.IOException"%>
<%@page import="org.apache.commons.io.FileUtils"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page import="com.sun.star.beans.PropertyValue"%>
<%@page import="com.sun.star.beans.XPropertySet"%>
<%@page import="com.sun.star.comp.helper.Bootstrap"%>
<%@page import="com.sun.star.frame.XComponentLoader"%>
<%@page import="com.sun.star.frame.XDesktop"%>
<%@page import="com.sun.star.frame.XStorable"%>
<%@page import="com.sun.star.lang.XComponent"%>
<%@page import="com.sun.star.lang.XMultiComponentFactory"%>
<%@page import="com.sun.star.text.XText"%>
<%@page import="com.sun.star.text.XTextDocument"%>
<%@page import="com.sun.star.uno.UnoRuntime"%>
<%@page import="com.sun.star.uno.XComponentContext"%>
<%@page import="com.sun.star.util.XReplaceDescriptor"%>
<%@page import="com.sun.star.util.XReplaceable"%>
<%@page import="ooo.connector.BootstrapSocketConnector"%>
<%@page import="com.sun.star.text.XText"%>
<%@page import="com.sun.star.text.XTextRange"%>
<%@page import="com.sun.star.lang.XMultiServiceFactory"%>
<%@page import="com.sun.star.text.XTextTable"%>
<%@page import="com.sun.star.table.XCellRange"%>
<%@page import="com.sun.star.table.XCell"%>
<%@page import="com.sun.star.util.XCloseable"%>
<%@page import="com.sun.star.text.XTextCursor"%>

<!DOCTYPE html>

<%

    // Initialise
    String oooExeFolder = "C:/Program Files/OpenOffice 4/program/";
    XComponentContext xContext = BootstrapSocketConnector.bootstrap(oooExeFolder);
    XMultiComponentFactory xMCF = xContext.getServiceManager();

    Object oDesktop = xMCF.createInstanceWithContext(
            "com.sun.star.frame.Desktop", xContext);

    XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, oDesktop);
    XComponentLoader xCLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, oDesktop);
    //----------------------------------

    try {

        // Create a document
        XComponent xdocument = xCLoader.loadComponentFromURL("private:factory/swriter", "_blank", 0, new PropertyValue[0]);

        // Get the textdocument
        XTextDocument aTextDocument = (XTextDocument) UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, xdocument);

        // Get its text
        XText xText = aTextDocument.getText();
        XTextRange xTextRange = xText.createTextCursor();
        ((XTextCursor) xTextRange).gotoEnd(true);

        XPropertySet xTextProps = (XPropertySet) UnoRuntime.queryInterface(
                XPropertySet.class, xTextRange);
        xTextProps.setPropertyValue("CharFontName", "Helvetica");
        xTextProps.setPropertyValue("CharWeight",
                new Float(com.sun.star.awt.FontWeight.BOLD));
        xTextProps.setPropertyValue("CharHeight", new Float(16));
        XParagraphCursor xPC = (XParagraphCursor) UnoRuntime.queryInterface(
                XParagraphCursor.class, xTextRange);

        XPropertySet xPS = (XPropertySet) UnoRuntime.queryInterface(
                XPropertySet.class, xPC);

        xPS.setPropertyValue("ParaAdjust", com.sun.star.style.ParagraphAdjust.CENTER);

        xText.insertString(xText.getEnd(), "Records Management Division \n Records Center Statistics \n Weekly Report\n\n", false);

        xText.insertString(xText.getEnd(), "Week :" + month + " " + date + " - " + endmonth + " " + date1 + "," + endyear + "\n\n", false);

        XMultiServiceFactory xMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, xdocument);

        // Creating a table with 3 rows and 4 columns
        XTextTable xTextTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xMSF.createInstance("com.sun.star.text.TextTable"));
        xTextTable.initialize(13, 2); // rows, cols

        // insert table  in the xText
        xText.insertTextContent(xText.getEnd(), xTextTable, false);

        XCellRange xCellRangeHeader = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, xTextTable);
        XCell xCellHeader = null;
        XText xHeaderText = null;

        xCellHeader = xCellRangeHeader.getCellByPosition(0, 0); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("Records Center Total Capacity");

        xCellHeader = xCellRangeHeader.getCellByPosition(1, 0); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("" + RecordCentrecapacity);

        xCellHeader = xCellRangeHeader.getCellByPosition(0, 1); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("Current Inventory For Week Ending");

        xCellHeader = xCellRangeHeader.getCellByPosition(1, 1); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("" + currentTotalInventory);

        xCellHeader = xCellRangeHeader.getCellByPosition(0, 2); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("Open Space / 1.1 Cu. Ft.");

        xCellHeader = xCellRangeHeader.getCellByPosition(1, 2); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("" + CurrentAvailableSpaceS);

        xCellHeader = xCellRangeHeader.getCellByPosition(0, 3); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("Open Space / 2.2 Cu. Ft.");

        xCellHeader = xCellRangeHeader.getCellByPosition(1, 3); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("" + CurrentAvailableSpaceL);

        xCellHeader = xCellRangeHeader.getCellByPosition(0, 4); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("Open Space / 1.1 Cold Room");

        xCellHeader = xCellRangeHeader.getCellByPosition(1, 4); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("" + CurrentAvailableSpaceC);

        xCellHeader = xCellRangeHeader.getCellByPosition(0, 5); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("Boxes Received For Storage/ Cu. Ft.");

        xCellHeader = xCellRangeHeader.getCellByPosition(1, 5); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("" + totalBoxesReceivedforStorage);

        xCellHeader = xCellRangeHeader.getCellByPosition(0, 6); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("Boxes Destroyed From Storage/ Cu. Ft.");

        xCellHeader = xCellRangeHeader.getCellByPosition(1, 6); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("" + totalBoxesDestroyedfromStorage);

        xCellHeader = xCellRangeHeader.getCellByPosition(0, 7); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("Boxes Received For Destruction Only/ Cu. Ft.");

        xCellHeader = xCellRangeHeader.getCellByPosition(1, 7); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("" + BoxesInForDestructionTotalCuFt);

        xCellHeader = xCellRangeHeader.getCellByPosition(0, 8); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("Retrievals");

        xCellHeader = xCellRangeHeader.getCellByPosition(1, 8); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("");

        xCellHeader = xCellRangeHeader.getCellByPosition(0, 9); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("Re-Files");

        xCellHeader = xCellRangeHeader.getCellByPosition(1, 9); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("");

        xCellHeader = xCellRangeHeader.getCellByPosition(0, 10); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("Customer Inquiries");

        xCellHeader = xCellRangeHeader.getCellByPosition(1, 10); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("");

        xCellHeader = xCellRangeHeader.getCellByPosition(0, 11); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("Microfilm Filed (Roll Canister)");

        xCellHeader = xCellRangeHeader.getCellByPosition(1, 11); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("");

        xCellHeader = xCellRangeHeader.getCellByPosition(0, 12); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("Boxes Returned To Departments /Cu. Ft.");

        xCellHeader = xCellRangeHeader.getCellByPosition(1, 12); // cols, rows
        xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader);
        xHeaderText.setString("" + BoxesReturnedToDepartmentSTotalCuFt);

        //close the document
        XCloseable xcloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, xdocument);
        xcloseable.close(false);

%> 

转换代码是

 byte[] pdf = baos.toByteArray();
             dir = new File(System.getProperty("user.dir")+File.separator+folderName);

                        if(!dir.exists())
                        {    
                             dir.mkdir();
                        }


                        fos = new FileOutputStream(System.getProperty("user.dir")+File.separator+folderName+File.separator+"Records Centre Statistics Weekly Report"+".pdf");
                        fos.write(pdf);
                        fos.close();


                        response.setContentType ("application/pdf");    
                        //set the header and also the Name by which user will be prompted to save
                        response.setHeader ("Content-Disposition", "attachment;filename="+"Records Centre Statistics Weekly Report"+".pdf");

                        File f = new File (System.getProperty("user.dir")+File.separator+folderName+File.separator+"Records Centre Statistics Weekly Report"+".pdf");

                        InputStream inputStream = new FileInputStream(f);
                        ServletOutputStream servletOutputStream = response.getOutputStream();
                        int bit = 256;
                        int i = 0;

                        try 
                        {

                              while ((bit) >= 0) 
                              {
                                    bit = inputStream.read();
                                    servletOutputStream.write(bit);
                              }
                              System.out.println("bit:" +bit);

                         }
                         catch (Exception ioe) 
                         {
                              ioe.printStackTrace(System.out);
                         }

                         servletOutputStream.flush();

                         inputStream.close();   



   }catch (Exception ex)
   {
       System.out.println("exception : "+ex.getMessage());
   }
   finally
              {
                  System.out.println("directory delete");
                  FileUtils.deleteDirectory(dir);
              }
%> 

如果你能帮助我,因为我是这个话题的新手。

1 个答案:

答案 0 :(得分:1)

哇!!! .cat-1中的Scriptlet ......多大的学校!!

通常,您会在$("ul.accordion-content > li").on("click", function() { var category = $(this).id; if ($(".gallery-item").hasClass(category)) { $(".gallery-item").show(); } else { $(".gallery-item").hide(); } });而不是<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="accordion-content"> <li id="cat-1">Category One</li> <li id="cat-2">Category Two</li> <li id="cat-3">Category Three</li> </ul> <div class="gallery-item cat-1"> <div class="item-content">Item One</div> </div> <div class="gallery-item cat-1"> <div class="item-content">Item One</div> </div> <div class="gallery-item cat-2"> <div class="item-content">Item Two</div> </div> <div class="gallery-item cat-2"> <div class="item-content">Item Two</div> </div> <div class="gallery-item cat-3"> <div class="item-content">Item Three</div> </div> <div class="gallery-item cat-3"> <div class="item-content">Item Three</div> </div>中执行此操作。 Jsp通常用于响应html,在这种情况下,你想要将二进制文件写入响应输出流。

您可以在jsp

的scriptlet中执行此操作
Servlet

但我建议您改用jsp

或者更好的是,停止使用15年前的技术,选择一个Web框架并停止使用jsp,scriptlet和servlet