【问题标题】:get a page of a pdf from a java server, and embed into an existing html从 java 服务器获取 pdf 页面,并嵌入到现有的 html 中
【发布时间】:2014-05-30 09:04:06
【问题描述】:

嗨,我需要一些帮助:我有这个 java 例程:

@RequestMapping("/visualizarPdf.htm")

    @ResponseBody

    public ResponseEntity<byte[]> generatePdf() throws IOException {
    PDDocument document = null;

    confPdfDTO.setIndex(13);
    confPdfDTO.setDocumento("/Users/martinLequerica/Desktop/directoriosServidor/republica 4.pdf");

    try {
        document = new PDDocument();
        // PDPage page = new PDPage();
        PDPage page = corta.cut(confPdfDTO);


        document.addPage(page);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        document.save(baos);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "pdf"));
        headers.setContentLength(baos.toByteArray().length);
        return new ResponseEntity<byte[]>(baos.toByteArray(), headers,
                HttpStatus.CREATED);
    } catch (Exception e) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.TEXT_PLAIN);
        return new ResponseEntity<byte[]>("BROKEN".getBytes(), headers,
                HttpStatus.CREATED);
    } finally {
        if (document != null) {
            document.close();
        }
    }
}

使用该代码,我获得了 pdf 的特定页面(此时它已被编码),现在我需要将其嵌入到现有的 html 页面中。为此,我使用了这个 jquery 函数:

    $.ajax({
        type : "POST",
        traditional : true,
        url : "/visualizarPdf.htm",

        success : function(response) {
            $("#contenedor_secundario").html(response);

        },
        error : function(e) {
            alert('Error: ' + e);
        }
    });

但我只得到很多奇怪的字符,我知道我需要告诉浏览器我会给它发送一个 pdf 文件,但我不知道如何。

我在 java 中使用 springMVC 3.0,在 javascript 中使用 jquery。

工作流程是:我告诉服务器我想要一个特定的页面,服务器响应我的 pdf 页面(该页面是一个只有一页的 pdf 文档)并且我需要将它嵌入一个 div。 (div 的名称是“contenedor_secundario”)。

谢谢

【问题讨论】:

    标签: java jquery spring-mvc pdf embed


    【解决方案1】:

    我调用了一个java函数,该函数的响应是二进制的pdf,objectpdf javascript库,获取该二进制文件并将其嵌入到div中。

    $(document).ready(function() {
    
    
    var pdf = new PDFObject({
          url: "visualizarPdf.htm",
          id: "pdfRendered",
          pdfOpenParams: {
            view: "FitH"
          }
        }).embed("pdfRenderer");
    
    });
    

    这是html

    <form action="visualizarPdf.htm" id ="formulario"></form>
    <div id="pdfRenderer"></div>
    

    这是以二进制形式返回我的 pdf 的 java 函数

    @RequestMapping("/visualizarPdf.htm")
    @ResponseBody
    public ResponseEntity<byte[]> generatePdf() throws IOException {
        PDDocument document = null;
    confPdfDTO.setIndex(13);
        confPdfDTO
                .setDocumento("/home/ubuntu/Escritorio/directoriosServidor/republica 4.pdf");
    
        try {
            document = new PDDocument();
            // PDPage page = new PDPage();
            PDPage page = corta.cut(confPdfDTO);
    
    
            document.addPage(page);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            document.save(baos);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(new MediaType("application", "pdf"));
            headers.setContentLength(baos.toByteArray().length);
            return new ResponseEntity<byte[]>(baos.toByteArray(), headers,
                    HttpStatus.CREATED);
        } catch (Exception e) {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.TEXT_PLAIN);
            return new ResponseEntity<byte[]>("BROKEN".getBytes(), headers,
                    HttpStatus.CREATED);
        } finally {
            if (document != null) {
                document.close();
            }
        }
     }
    

    这里是pdfObject项目页面,你可以在那里下载最新版本。 http://pdfobject.com/

    是的,我的英语很烂,很抱歉。

    【讨论】:

      【解决方案2】:

      告诉浏览器将 HTTP 响应正文呈现为 HTML 页面或尝试将结果作为附件打开的是响应的 Content-Disposition 标头。

      尝试在响应中设置此标头:

      headers.set("Content-Disposition", "attachment");
      

      这样,浏览器将被告知响应的正文是附件,并将其视为附件,并建议使用嵌入式 PDF 阅读器打开它。

      【讨论】:

        猜你喜欢
        • 2020-10-02
        • 2014-03-31
        • 2015-09-07
        • 2011-01-21
        • 2015-03-24
        • 1970-01-01
        • 2017-08-04
        • 2012-12-03
        • 2015-09-18
        相关资源
        最近更新 更多