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

如果你正在發送標題,然後用郵遞員打 –