节点js中的Pdf下载功能 [英] Pdf download functionality in node js

查看:60
本文介绍了节点js中的Pdf下载功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用此代码下载pdf文件,但我们遇到了问题。我想当我点击这条路线然后pdf文件将下载。

但是当我在postman中点击这个api并点击下载按钮然后pdf文件下载。所以我想下载包含数据的pdf文件。



我尝试过:



router.get('/ demo',function(req,res){

async function getinvoicelist(){



var chunks = [];

var options = {method:'GET',

url:'https://api.fortnox.se/3/invoices/ 31 /预览',



请求(选项,功能(错误,响应,正文){



console.log(正文);



});



%PDF-1.4



3 0 obj

<< / Length 1438 / Filter / FlateDecode>> stream

xYr6 +Ɍ3SxXڭHI M2 + STBN>有?!_ / p〜^< H? ?N 10 XTI} T,&安培;????kgxU | *҂GkJPSH#13וו)。?  ` ? /

解决方案

通过AJAX请求下载文件是tric KY。您可能能够使用 createObjectURL 来执行此操作:

请求(选项) , function (错误,响应){
var blob = new Blob([response],{type:' application / pdf' });
var url = window .URL.createObjectURL(blob);

var link = document .createElement( ' a');
link.href = url;
link.download = Preview.pdf;

document .body .appendChild(link);
link.click();
documen吨 .body.removeChild(联系);

// 建议:在一段时间后撤销对象URL以释放资源。
// 无法知道下载何时完成。
setTimeout( function (){ window .URL.revokeObjectURL(url);}, 60000 );
});



正在下载来自Ajax POST请求的文件 [ ^ ]

Javascript - 使用AJAX下载文件并显示Progess Bar [ ^ ]



我可以使用... createObjectURL [ ^ ]

我可以使用... Blob [ ^ ]


We are using this code to download the pdf file but we are getting problem. i want when i hit this route then pdf file will download.
But when i hit this api in postman and click on the download button then pdf file download. So i wnat to download the pdf file with data.

What I have tried:

router.get('/demo',function (req,res){
async function getinvoicelist(){

var chunks = [];
var options = { method: 'GET',
url: 'https://api.fortnox.se/3/invoices/31/preview',

request(options, function (error, response, body) {

console.log(body);

});

%PDF-1.4
%����
3 0 obj
<</Length 1438/Filter/FlateDecode>>stream
x��Y�r�6��+�Ɍ3S�xXڭ�h���IM���STB�n>�_�/p~!�^<H��N�xti���}��T�, ��&k��g���xU|*҂�Gk�JPSH#13�װ�).�?�����`�������/��

解决方案

Downloading files via AJAX requests is tricky. You might be able to use createObjectURL to do this:

request(options, function (error, response) {
    var blob = new Blob([response], { type: 'application/pdf' });
    var url = window.URL.createObjectURL(blob);
    
    var link = document.createElement('a');
    link.href = url;
    link.download = "Preview.pdf";

    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    
    // Recommended: revoke the object URL after some time to free up resources.
    // There is no way to know when the download is complete.
    setTimeout(function() { window.URL.revokeObjectURL(url); }, 60000);
});


Downloading files from Ajax POST Requests[^]
Javascript - Downloading Files with AJAX and Showing a Progess Bar[^]

Can I use... createObjectURL[^]
Can I use... Blob[^]


这篇关于节点js中的Pdf下载功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆