Try to download pdf nextjs api route / client page
Answered
Yuss posted this in #help-forum
YussOP
I try to download a pdf get from a back end with format blob application / pdf
Answered by Ray
try this
try {
const response = await fetch(`${apiUrl}/action/export/${body.year}/${body.month}/${body.clientId}/${body.state}/${body.type}/${getPrefix(body.type)}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Cookie': `token=${cookies.token}`
},
});
if (response.ok) {
const buff = Buffer.from(await response.arrayBuffer())
res.setHeader('Content-Type', 'application/pdf');
res.status(200).send(buff);
return;
} else {
const errorData = await response.json();
throw new Error(errorData.message);
}
} catch (error) {
res.status(500).json({ error: 'Erreur interne du serveur' });
}8 Replies
YussOP
// api/file
try {
const response = await fetch(`${apiUrl}/action/export/${body.year}/${body.month}/${body.clientId}/${body.state}/${body.type}/${getPrefix(body.type)}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Cookie': `token=${cookies.token}`
},
});
if (response.ok) {
const blob = await response.blob();
console.log(blob);
res.setHeader('Content-Type', 'application/pdf');
res.status(200).send(blob);
return;
} else {
const errorData = await response.json();
throw new Error(errorData.message);
}
} catch (error) {
res.status(500).json({ error: 'Erreur interne du serveur' });
}page client
@Yuss ts
// api/file
try {
const response = await fetch(`${apiUrl}/action/export/${body.year}/${body.month}/${body.clientId}/${body.state}/${body.type}/${getPrefix(body.type)}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Cookie': `token=${cookies.token}`
},
});
if (response.ok) {
const blob = await response.blob();
console.log(blob);
res.setHeader('Content-Type', 'application/pdf');
res.status(200).send(blob);
return;
} else {
const errorData = await response.json();
throw new Error(errorData.message);
}
} catch (error) {
res.status(500).json({ error: 'Erreur interne du serveur' });
}
try this
try {
const response = await fetch(`${apiUrl}/action/export/${body.year}/${body.month}/${body.clientId}/${body.state}/${body.type}/${getPrefix(body.type)}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Cookie': `token=${cookies.token}`
},
});
if (response.ok) {
const buff = Buffer.from(await response.arrayBuffer())
res.setHeader('Content-Type', 'application/pdf');
res.status(200).send(buff);
return;
} else {
const errorData = await response.json();
throw new Error(errorData.message);
}
} catch (error) {
res.status(500).json({ error: 'Erreur interne du serveur' });
}Answer
does it work?
YussOP
yeah first try