欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在线预览文档简单例子

程序员文章站 2022-07-13 15:22:58
...

以下是一个Java实现简单浏览器预览服务端文档的例子,文档包括pdf,excel,word,ppt,思路是将excel,word转化为html;将ppt转为图片,再转为html;将pdf以pdf流的形式返回给浏览器,一般的浏览器都支持预览pdf。

maven依赖

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>ooxml-schemas</artifactId>
  <version>1.1</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.15</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>3.15</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-scratchpad</artifactId>
  <version>3.15</version>
</dependency>
<dependency>
  <groupId>fr.opensagres.xdocreport</groupId>
  <artifactId>org.apache.poi.xwpf.converter.core</artifactId>
  <version>1.0.4</version>
</dependency>
<dependency>
  <groupId>fr.opensagres.xdocreport</groupId>
  <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
  <version>1.0.4</version>
</dependency>
<dependency>
  <groupId>org.apache.xmlbeans</groupId>
  <artifactId>xmlbeans</artifactId>
  <version>2.5.0</version>
</dependency>

预览pdf

byte[] datas = readFile(f);
response.setContentType("application/pdf");
response.setContentLength(datas.length);
OutputStream os = res.getOutputStream();
os.write(datas, 0, datas.length);
os.flush();
os.close();

预览页面

将转为html或者图片的文档,嵌入此html中返回给浏览器展示。

private static final StringBuilder ORIGIN_HTML = new StringBuilder("<!DOCTYPE html>\n" +
        "<html lang=\"en\">\n" +
        "<head>\n" +
        "    <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />\n" +
        "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n" +
        "    <title>预览</title>\n" +
        "    <link rel=\"stylesheet\" href=\"/resource/css/simple.slide.css\">\n" +
        "    <link rel=\"stylesheet\" href=\"/resource/css/preview.css\">\n" +
        "    <script type=\"text/javascript\" src=\"../../resource/jquery-1.9.0.min.js\"></script>\n" +
        "    <script type=\"text/javascript\" src=\"/resource/simple.slide.js\"></script>\n" +
        "    <script type=\"text/javascript\">\n" +
        "        $(function(){\n" +
        "           $(\".pptimage\").simpleSlide();\n" +
        "        });\n" +
        "    </script>\n" +
        "</head>\n" +
        "<body>\n" +
        "       ####\n" +
        "</body>\n" +
        "</html>");

word07转为html

public static void doc07ToHtml(String from, OutputStream out, String imagePath){
    try{
        Map<String, String> map = new HashMap<String, String>();
        //1.如果图片文件夹存在,删除重新创建之
        File imagePathFile = new File(imagePath);
        if(imagePathFile.exists()) {
            FileUtils.deleteDirectory(imagePathFile);
        }
        imagePathFile.mkdirs();

        FileInputStream fis = new FileInputStream(from);
        XWPFDocument doc = new XWPFDocument(fis);
        XHTMLOptions options = XHTMLOptions.create();
        options.setExtractor(new IImageExtractor() {

            @Override
            public void extract(String imagePath1, byte[] imageData) throws IOException {
                //2.处理文件后缀
                String suf = "";
                if(imagePath1.lastIndexOf(".") > 0) {
                    suf = imagePath1.substring(imagePath1.lastIndexOf("."), imagePath1.length());
                }
                //3.写文件,并将文件对应关系存入map
                String tempFile = imagePath+File.separator+UuidUtil.getUUID()+suf;
                File f = new File(tempFile);
                FileUtils.writeByteArrayToFile(f, imageData);
                map.put(imagePath1, tempFile);

            }});
        options.setIgnoreStylesIfUnused(false);
        options.setFragment(true);
        options.URIResolver(new IURIResolver() {

            @Override
            public String resolve(String arg0) {
                if(map.containsKey(arg0)) {
                    String u = map.get(arg0);
                    if(!XaUtil.isEmpty(u)){
                        try {
                            u = URLEncoder.encode(u,"ISO-8859-1");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                    return "/sys/filemanage/getViewImage.do?id="+u;
                }
                return arg0;
            }
        });
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        XHTMLConverter.getInstance().convert(doc, os, options);
        byte[] datas = os.toByteArray();
        os.close();
        //处理表格没有边框
        String content = new String(datas,"utf-8");
        content = ORIGIN_HTML.toString().replaceAll("####",content);
        datas = content.getBytes("utf-8");
        out.write(datas, 0, datas.length);
        out.flush();
        out.close();
    }catch(Exception e){

        log.error("",e);
    }
}

word03转为html

public static void doc03ToHtml(String from, OutputStream out, String imagePath)throws Exception{
    try{
        //1.如果图片文件夹存在,删除重新创建之
        File imagePathFile = new File(imagePath);
        if(imagePathFile.exists()) {
            FileUtils.deleteDirectory(imagePathFile);
        }
        imagePathFile.mkdirs();
        FileInputStream fis = new FileInputStream(from);
        HWPFDocument doc = new HWPFDocument(fis);
        WordToHtmlConverter converter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
        converter.setPicturesManager(new PicturesManager() {

            @Override
            public String savePicture(byte[] arg0, PictureType arg1, String arg2, float arg3, float arg4) {
                //2.处理文件后缀
                String suf = "";
                if(arg2.lastIndexOf(".") > 0) {
                    suf = arg2.substring(arg2.lastIndexOf("."), arg2.length());
                }
                //3.写文件,并将文件对应关系存入map
                String tempFile = imagePath+File.separator+UuidUtil.getUUID()+suf;
                File f = new File(tempFile);
                try {
                    FileUtils.writeByteArrayToFile(f, arg0);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if(!XaUtil.isEmpty(tempFile)){
                    try {
                        tempFile = URLEncoder.encode(tempFile,"ISO-8859-1");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                return "/sys/filemanage/getViewImage.do?id="+tempFile;
            }
        });

        converter.processDocument(doc);
        Document htmlDocument = converter.getDocument();
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        DOMSource domSource = new DOMSource(htmlDocument);
        StreamResult streamResult = new StreamResult(outStream);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.METHOD, "html");
        serializer.transform(domSource, streamResult);
        outStream.close();
        byte[] datas = outStream.toByteArray();
        //处理表格没有边框
        String content = new String(datas,"utf-8");
        content = ORIGIN_HTML.toString().replaceAll("####",content);
        datas = content.getBytes("utf-8");
        out.write(datas, 0, datas.length);
        out.flush();
        out.close();
    }catch(Exception e){

        log.error("",e);
    }
}

Excel转为html

public static void exceToHtml(String from, OutputStream out){
    InputStream in = null;
    XSSFWorkbook xwb = null;
    HSSFWorkbook hwb= null;
    try{
        in = new FileInputStream(from);
        Workbook wb = WorkbookFactory.create(in);
        if(wb instanceof XSSFWorkbook) {
            xwb = (XSSFWorkbook) wb;
            hwb = new HSSFWorkbook();
            ConvertXSSF2HSSF c = new ConvertXSSF2HSSF();
            c.transformXSSF(xwb, hwb);
        }else{
            hwb = (HSSFWorkbook) wb;
        }

        ExcelToHtmlConverter converter = new ExcelToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
        converter.processWorkbook(hwb);

        wb.close();
        Document htmlDocument = converter.getDocument();
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        DOMSource domSource = new DOMSource (htmlDocument);
        StreamResult streamResult = new StreamResult (outStream);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer = tf.newTransformer();
        serializer.setOutputProperty (OutputKeys.ENCODING, "utf-8");
        serializer.setOutputProperty (OutputKeys.INDENT, "yes");
        serializer.setOutputProperty (OutputKeys.METHOD, "html");
        serializer.transform (domSource, streamResult);
        outStream.close();
        String content = new String (outStream.toByteArray(),"utf-8" );
        content = ORIGIN_HTML.toString().replaceAll("####",content);
        byte[] datas = content.getBytes("utf-8");
        out.write(datas,0,datas.length);
        out.flush();
        out.close();
    }catch (Exception e){
        log.error("",e);
    }finally {
        try {
            if(null != in){
                in.close();
            }
            if(null != xwb){
                xwb.close();
            }
            if(null != hwb){
                hwb.close();
            }
        } catch (IOException e) {

           log.error("",e);
        }
    }
}


ppt07转为图片

public static String ppt07ToImage(String from, String images) {
    StringBuilder sb = new StringBuilder();
    FileInputStream fis = null;
    XMLSlideShow ppt = null;
    FileOutputStream fos = null;
    try {
        // 1.如果图片文件夹存在,删除重新创建之
        File imagePathFile = new File(images);
        if (imagePathFile.exists()) {
            FileUtils.deleteDirectory(imagePathFile);
        }
        imagePathFile.mkdirs();

        fis = new FileInputStream(from);
        ppt = new XMLSlideShow(fis);
        Dimension pageSize = ppt.getPageSize();
        List<XSLFSlide> slides = ppt.getSlides();
        String xmlFontFormat = "<xml-fragment xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\">"
                + "<a:rPr lang=\"zh-CN\" altLang=\"en-US\" dirty=\"0\" smtClean=\"0\"> "
                + "<a:latin typeface=\"+mj-ea\"/> " + "</a:rPr>" + "</xml-fragment>";
        int index = 0;
        for (XSLFSlide slide : slides) {
            CTSlide cts = slide.getXmlObject();
            CTGroupShape groupShape = cts.getCSld().getSpTree();
            List<CTShape> shapeList = groupShape.getSpList();
            for (CTShape ctShape : shapeList) {
                CTTextBody oneCTTextBody = ctShape.getTxBody();
                if (null == oneCTTextBody) {
                    continue;
                }
                CTTextParagraph[] oneCTTextParagraph = oneCTTextBody.getPArray();
                CTTextFont oneCTTextFont = null;
                oneCTTextFont = CTTextFont.Factory.parse(xmlFontFormat);
                for (CTTextParagraph ctTextParagraph : oneCTTextParagraph) {
                    CTRegularTextRun[] onrCTRegularTextRunArray = ctTextParagraph.getRArray();
                    for (CTRegularTextRun ctRegularTextRun : onrCTRegularTextRunArray) {
                        CTTextCharacterProperties oneCTTextCharacterProperties = ctRegularTextRun.getRPr();
                        oneCTTextCharacterProperties.setLatin(oneCTTextFont);
                    }

                }
            }
            // 创建BufferedImage 对象,图像尺寸为原来的PPT的每页尺寸
            BufferedImage oneBufferedImage = new BufferedImage(pageSize.width, pageSize.height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D oneGraphics2D = oneBufferedImage.createGraphics();
            // 将PPT文件中的每个页面中的相关内容画到转换后的图片中
            slides.get(index).draw(oneGraphics2D);
            /**
             * 设置图片的存放路径和图片格式,注意生成的文件路径为绝对路径,最终获得各个图像文件所对应的输出流的对象
             */

            String imgName = images + File.separator + UuidUtil.getUUID() + ".jpg";
            sb.append(imgName + ",");

            fos = new FileOutputStream(imgName);
            ImageIO.write(oneBufferedImage, "jpg", fos);
            index++;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        try {

            if(null != fis) {

                fis.close();
            }
            if(null != ppt) {

                ppt.close();
            }
            if(null != fos){
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

ppt03转为图片

public static String ppt03ToImage(String from, String images) {
    StringBuilder sb = new StringBuilder();
    FileInputStream fis = null;
    FileOutputStream fos = null;
    HSLFSlideShow ppt = null;
    try {
        // 1.如果图片文件夹存在,删除重新创建之
        File imagePathFile = new File(images);
        if (imagePathFile.exists()) {
            FileUtils.deleteDirectory(imagePathFile);
        }
        imagePathFile.mkdirs();

        fis = new FileInputStream(from);
        ppt = new HSLFSlideShow(fis);
        // 获取PPT每页的大小(宽和高度)
        Dimension onePPTPageSize = ppt.getPageSize();

        // 获得PPT文件中的所有的PPT页面(获得每一张幻灯片),并转为一张张的播放片
        List<HSLFSlide> pptPageSlideList = ppt.getSlides();
        // 下面循环的主要功能是实现对PPT文件中的每一张幻灯片进行转换和操作
        for (int i = 0; i < pptPageSlideList.size(); i++) {
            // 这几个循环只要是设置字体为宋体,防止中文乱码,
            List<List<HSLFTextParagraph>> oneTextParagraphs = pptPageSlideList.get(i).getTextParagraphs();
            for (List<HSLFTextParagraph> list : oneTextParagraphs) {
                for (HSLFTextParagraph hslfTextParagraph : list) {
                    List<HSLFTextRun> HSLFTextRunList = hslfTextParagraph.getTextRuns();
                    for (int j = 0; j < HSLFTextRunList.size(); j++) {

         /*
          * 如果PPT在WPS中保存过,则 HSLFTextRunList.get(j).getFontSize();的值为0或者26040,
          * 因此首先识别当前文本框内的字体尺寸是否为0或者大于26040,则设置默认的字体尺寸。
          *
          */
                        // 设置字体大小
                        Double size = HSLFTextRunList.get(j).getFontSize();
                        if ((size <= 0) || (size >= 26040)) {
                            HSLFTextRunList.get(j).setFontSize(20.0);
                        }
                        // 设置字体样式为宋体
                        // String family=HSLFTextRunList.get(j).getFontFamily();
                        // HSLFTextRunList.get(j).setFontFamily("宋体");
                        int index = HSLFTextRunList.get(j).getFontIndex();
                        String name = HSLFTextRunList.get(j).getFontFamily();
                        HSLFTextRunList.get(j).setFontIndex(1);
                        HSLFTextRunList.get(j).setFontFamily("宋体");
                        log.info("ppt info:"+HSLFTextRunList.get(j).getRawText());
                    }
                }

            }
            /**
             * 创建BufferedImage对象,图像的尺寸为原来的每页的尺寸
             */
            BufferedImage oneBufferedImage = new BufferedImage(onePPTPageSize.width, onePPTPageSize.height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D oneGraphics2D = oneBufferedImage.createGraphics();
            /**
             * 设置转换后的图片背景色为白色
             *
             */
            oneGraphics2D.setPaint(Color.white);
            oneGraphics2D.fill(new Rectangle2D.Float(0, 0, onePPTPageSize.width, onePPTPageSize.height));
            pptPageSlideList.get(i).draw(oneGraphics2D);
            /**
             * 设置图片的存放路径和图片格式
             */
            String imgName = images + File.separator + UuidUtil.getUUID() + ".jpg";
            sb.append(imgName);
            sb.append(",");
            fos = new FileOutputStream(imgName);
            /**
             * 图片文件保存的指定的目录中
             */
            ImageIO.write(oneBufferedImage, "jpg", fos);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if(null != fis) {
                fis.close();
            }
            if(null != ppt) {
                ppt.close();
            }
            if(null != fos){
                fos.close();
            }
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
    return sb.toString();
}

(完)