简体   繁体   English

从 Rest API 下载 PDF

[英]Download PDF From Rest API

I have the following text that i get from java with a get request.我有以下文本,是我通过 get 请求从 java 获得的。 GET: http://localhost:8101/raportet/Cinema.jasper获取: http://localhost:8101/raportet/Cinema.jasper

%PDF-1.4
%âãÏÓ
3 0 obj
<</Filter/FlateDecode/Length 1644>>stream
xœ½œ]SÛF†ïý+ö2½ˆ²_úêUÝ`Rh;étz¡`aDü‘H†ß•Ç
8ïJø5ã³ ôé<^Yþ<ø}20‘ˆÃHL¦ƒÑdð×@‹ãê·JH÷¨¾Ç©“ÅàÕ¡JŠÉåàÅ 
..............

I want to download this file in frontend using js.我想使用js在前端下载这个文件。 I tried a lot of methods but i cannot download it.我尝试了很多方法,但我无法下载它。 Is there any method i can do it.有什么方法我可以做到。 I've almost the same problem as this: Opening PDF String in new window with javascript我遇到了与此几乎相同的问题: 使用 javascript 在新窗口中打开 PDF 字符串

you can use this method你可以用这个方法

 axios.get(exportUrl, {responseType:'blob'})
        .then(response => {
            // Create blob link to download
            var blob = new Blob([response.data], { type: 'application/pdf' });

            var URL = window.URL || window.webkitURL;
            const url = window.URL.createObjectURL(blob);
            const link = document.createElement('a');
            link.target   = '_blank';
            link.href = url;
            link.setAttribute(    //if you just want to preview pdf and dont want download delete this three lines 
               'download',
               `Factor.pdf`,
             );
        
            // Append to html link element page
            document.body.appendChild(link);
        
            // Start download
            link.click();
        
            // Clean up and remove the link
            link.parentNode.removeChild(link);
            URL.revokeObjectURL(url);

        })
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM