[英]how to make pdf downloadable using pdf.js (data rendered here unit8array in canvas)
[英]Save datastream received as unit8array as pdf to filesystem on the front end
const reader = response.body.getReader();
reader.read()
.then(({ done, value }) => {
var link = document.createElement("a");
value = window.btoa(value) //<-tried with and without
link.download = "test.pdf";
link.href = 'data:application/pdf;base64,'+value
link.click();
// console.log(window.atob(value))
})
這只會保存一個沒有任何內容或無法讀取的pdf文件。
回調中的value是將pdf文件讀入數據stram中的unicode,因此它只是unit8array中的一堆數字。 我嘗試將其轉換為base64並通過數據uri下載,但沒有任何運氣。
這是在電子應用中
編輯:
當我從后端接收數據時,請求將其顯示為unicode,但響應將其作為可讀流提供給我。 我100%確定我能夠解析unicode並從中保存PDF文件,但是我不知道如何從給定的可讀流中獲取unicode形式。
嘗試給文件命名:
link.name = "file.pdf";
然后使用base64制作數據URI。 如果這樣不起作用,請嘗試使數據URI像這樣:
'data:application/octet-stream;charset=utf-16le;base64,' + value;
如果可能的話,調試dataURI可能是值得的,開始獲取的數據可能會被破壞,從而使您難以為繼。
您也可以嘗試將Uint8Array轉換為File對象:
var arr = [];
var arr.push(value);
var pdf = new File(arr, 'test.pdf', {type: 'application/pdf'});
然后將其附加到FormData對象並將其發布到您的服務器。 編輯*哎呀,您沒有看到您正在使用電子進行此操作,因此我不確定上述方法是否適用,但可能仍會有所幫助。 祝你好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.