0

我使用春季启动,我用这个代码来生成pdf文档。白色标签错误,当我genereate pdf

@GetMapping(value = "/members/{memberId}/contract/{contractId}/generalcontracts", produces = "application/pdf") 
public ResponseEntity<byte[]> getMemberContract(@PathVariable("memberId") Long memberId, @PathVariable("contractId") Long contractId) throws IOException { 
    byte[] content = reportService.generateMemberContractReport(contractId); 
    return prepareReport(content); 
} 

private ResponseEntity<byte[]> prepareReport(byte[] content) throws IOException { 
    HttpHeaders headers = new HttpHeaders(); 
    headers.setContentType(MediaType.parseMediaType("application/pdf")); 
    String filename = "report.pdf"; 
    headers.setContentDispositionFormData(filename, filename); 
    headers.setCacheControl("must-revalidate, post-check=0, pre-check=0"); 
    ResponseEntity<byte[]> response = new ResponseEntity<>(content, headers, HttpStatus.OK); 
    return response; 
} 

在JS,我做

<button id="memberPrintReport" type="button" class="btn btn-primary">Imprimer</button> 

$("#memberPrintReport").on('click', function (e) { 
    tryit(getHostName() + "/members/" + memberId + "/contract/" + contractId + "/generalcontracts"); 
}  

function tryit(urlServer) { 
    var win = window.open('_blank'); 
    downloadFile(urlServer, function (blob) { 
     var url = URL.createObjectURL(blob); 
     win.location = url; 
    }); 
} 

新标签中打开,几秒钟的白色标签错误时我看到后,我看到了PDF格式。

我不明白为什么我的几个瞬间在得到这个白色标签错误的白色标签错误 https://imagebin.ca/v/3Wnqxfpq1yR6

编辑

图片:

function downloadFile(url, success) { 
    var xhr = new XMLHttpRequest(); 
    xhr.open('GET', url, true); 
    xhr.setRequestHeader("Authorization", "Basic " + $.cookie('authorization')); 
    xhr.responseType = "blob"; 
    xhr.onreadystatechange = function() { 
     if (xhr.readyState == 4) { 
      if (success) 
       success(xhr.response); 
     } 
    }; 
    xhr.send(null); 
} 

编辑

该工作与铬,但不是与火狐

function tryit(urlServer) { 
    downloadFile(urlServer, function (blob) { 
    var url = URL.createObjectURL(blob); 
    window.open(url, '_blank'); 

    }); 
} 
+0

什么是白色标签错误的内容? – Kirby

+0

@Kirby,更新的消息 –

回答

0

您正在执行此行:var win = window.open('_blank');导致开放http://localhost:8080/_blank,因为javascript将_blank理解为url。所以你需要更新你的tryit功能如下:

function tryit(urlServer) { 
    downloadFile(urlServer, function (blob) { 
     var url = URL.createObjectURL(blob); 
     window.open(url,'_blank'); 
    }); 
} 
+0

不能与firefox ...打开和关闭。与Chrome什么都不做 –

+0

你可以直接在浏览器中打开PDF API。检查是否下载或只是在浏览器内打开 –

+0

如果你正在发送标题,然后用邮递员打 –