parserror错误

xam8gpfp  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(323)

我从jqueryajax调用endpoint,使用springmvc生成jasperreport,并希望在新的浏览器选项卡中以pdf格式查看生成的报告。问题是,我得到的报告parserror从文本到应用程序的转换失败/pdf错误返回到ajax,如果我从浏览器调试请求,我会看到响应中获得的pdf文本
下面是调用springmvc方法的ajax代码

var developerData = {};
        developerData["from"] = $("#productivity_dateTimeFrom").val();
        developerData["to"] = $("#productivity_dateTimeTo").val();
        $.ajax({
            type : "POST",
            contentType : "application/json; charset=utf-8",
            url : 'Productivity',
            data : JSON.stringify(developerData),
            dataType : 'application/pdf',
            success : function(data) {                  
                window.open(data.fileUrl);
            },
            error : function(request,status,error){
                alert(request)
                alert(status)
                alert(error)
            }
        });

要从ajax调用的spring mvc方法:

@RequestMapping(value = "/Productivity", method = RequestMethod.POST, produces ="application/pdf")
@ResponseBody
public ResponseEntity<byte[]> runReport(@RequestBody String object) throws IOException, JRException {

    db_con db_con = new db_con();
    Resource resource = new ClassPathResource("Productivity.jasper");
    InputStream jasperStream = null;
    Gson g = new Gson();
    try {
        jasperStream = resource.getInputStream();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    DateTimeRange p = g.fromJson(object, DateTimeRange.class);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("p_from_date",dateUtils.convertStringToTimestamp(p.getFrom().replace("T", " ")+":00.000"));
    params.put("p_to_date",dateUtils.convertStringToTimestamp(p.getTo().replace("T", " ")+":00.000"));
    params.put("username", "admin");
    params.put("p2", 1L);
    params.put("p3", "All");
    JasperReport jasperReport = null;

    try {
        jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
    } catch (JRException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Connection rep_connection = null;

    try {
        rep_connection = db_con.get_connection();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JasperPrint jasperPrint = null;
    try {
        jasperPrint = JasperFillManager.fillReport(jasperReport, params, rep_connection);
    } catch (JRException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();

    try {
        JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);
    } catch (JRException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    HttpHeaders headers = new HttpHeaders();

    headers.setContentType(MediaType.APPLICATION_PDF);
    String filename = "report.pdf";

    headers.add("content-disposition", "inline;filename=" + filename);

    return new ResponseEntity<>(outStream.toByteArray(), headers, HttpStatus.OK);

}
5us2dqdw

5us2dqdw1#

尝试下载pdf作为blob并使用blob url查看它

$.ajax({
        type : "POST",
        contentType : "application/json; charset=utf-8",
        url : 'Productivity',
        data : JSON.stringify(developerData),
        xhrFields:{
            responseType: 'blob'
        },
        success : function(data) {                  
            window.open(URL.createObjectURL(data));
        },
        error : function(request,status,error){
            alert(request)
            alert(status)
            alert(error)
        }
    });

相关问题