从我的服务器打开 PDF 文件的问题
Issues with open PDF files from my server
open/download 来自我的后端 (express) 的 pdf 有问题。如果我手动转到 chrome (localhost:5000/test) 中的端点,它会打开并显示 PDF。如果在客户端中选择 "Save and download",在邮递员中也能正常工作。
后端的响应如下所示,根据我的理解,它是一个 PDF 流?我如何从前端 view/download 它?我正在使用 React 和 Next。
%PDF-1.4
%����
1 0 obj
<</Creator (Chromium)
/Producer (Skia/PDF m79)
/CreationDate (D:20200127154327+00'00')
/ModDate (D:20200127154327+00'00')>>
endobj
3 0 obj
<</ca 1
/BM /Normal>>
endobj
5 0 obj
<</Type /XObject
/Subtype /Image
/Width 135
/Height 23
/ColorSpace /DeviceRGB
/SMask 6 0 R
/BitsPerComponent 8
/Filter /FlateDecode
/Length 69>> stream
x���
���7
���:���7)AR��I �$%HJ�� )AR��I �$%HJ���7`�˫��
endstream
endobj
6 0 obj
<</Type /XObject
/Subtype /Image
/Width 135
/Height 23
/ColorSpace /DeviceGray
/BitsPerComponent 8
/Filter /FlateDecode
/Length 51>> stream
..... a lot more like this
PDF 文件已损坏,并显示错误 - 尝试打开时无法加载 PDF 文档。我试过将它转换为 base64 字符串,但在打开它们时出现相同的错误。
前端功能由 onClick 运行
const config: AxiosOptions = {
route: 'test',
method: 'GET'
};
const res = await makeAxiosRequest(config);
console.log('res', res.data);
const link = document.createElement('a');
link.setAttribute('href', 'data:"application/pdf;' + res.data);
link.setAttribute('download', 'bp.pdf');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
后端(快速)
const filename = __dirname + '/test.pdf';
const readStream = fs.createReadStream(filename);
const stat = fs.statSync(filename);
res.setHeader('Content-Length', stat.size);
res.setHeader('Content-type', 'application/pdf');
res.setHeader(
'Content-Disposition',
'attachment; inline=test.pdf');
// This will wait until we know the readable stream is actually valid before piping
readStream.on('open', () => {
// This just pipes the read stream to the response object (which goes to the client)
readStream.pipe(res);
});
// This catches any errors that happen while creating the readable stream (usually invalid names)
readStream.on('error', (err) => {
res.end(err);
});
原来我需要将 pdf 转换为 base64 字符串。
在后端做了这个
const filename = __dirname + '/test.pdf';
const base64PDF = fs.readFileSync(filename, { encoding: 'base64' });
然后在前端做了这个
const link = document.createElement('a');
link.setAttribute('href', 'data:"application/pdf;base64,' + res.data.base64PDF);
link.setAttribute('download', res.data.name);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
open/download 来自我的后端 (express) 的 pdf 有问题。如果我手动转到 chrome (localhost:5000/test) 中的端点,它会打开并显示 PDF。如果在客户端中选择 "Save and download",在邮递员中也能正常工作。
后端的响应如下所示,根据我的理解,它是一个 PDF 流?我如何从前端 view/download 它?我正在使用 React 和 Next。
%PDF-1.4
%����
1 0 obj
<</Creator (Chromium)
/Producer (Skia/PDF m79)
/CreationDate (D:20200127154327+00'00')
/ModDate (D:20200127154327+00'00')>>
endobj
3 0 obj
<</ca 1
/BM /Normal>>
endobj
5 0 obj
<</Type /XObject
/Subtype /Image
/Width 135
/Height 23
/ColorSpace /DeviceRGB
/SMask 6 0 R
/BitsPerComponent 8
/Filter /FlateDecode
/Length 69>> stream
x���
���7
���:���7)AR��I �$%HJ�� )AR��I �$%HJ���7`�˫��
endstream
endobj
6 0 obj
<</Type /XObject
/Subtype /Image
/Width 135
/Height 23
/ColorSpace /DeviceGray
/BitsPerComponent 8
/Filter /FlateDecode
/Length 51>> stream
..... a lot more like this
PDF 文件已损坏,并显示错误 - 尝试打开时无法加载 PDF 文档。我试过将它转换为 base64 字符串,但在打开它们时出现相同的错误。
前端功能由 onClick 运行
const config: AxiosOptions = {
route: 'test',
method: 'GET'
};
const res = await makeAxiosRequest(config);
console.log('res', res.data);
const link = document.createElement('a');
link.setAttribute('href', 'data:"application/pdf;' + res.data);
link.setAttribute('download', 'bp.pdf');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
后端(快速)
const filename = __dirname + '/test.pdf';
const readStream = fs.createReadStream(filename);
const stat = fs.statSync(filename);
res.setHeader('Content-Length', stat.size);
res.setHeader('Content-type', 'application/pdf');
res.setHeader(
'Content-Disposition',
'attachment; inline=test.pdf');
// This will wait until we know the readable stream is actually valid before piping
readStream.on('open', () => {
// This just pipes the read stream to the response object (which goes to the client)
readStream.pipe(res);
});
// This catches any errors that happen while creating the readable stream (usually invalid names)
readStream.on('error', (err) => {
res.end(err);
});
原来我需要将 pdf 转换为 base64 字符串。 在后端做了这个
const filename = __dirname + '/test.pdf';
const base64PDF = fs.readFileSync(filename, { encoding: 'base64' });
然后在前端做了这个
const link = document.createElement('a');
link.setAttribute('href', 'data:"application/pdf;base64,' + res.data.base64PDF);
link.setAttribute('download', res.data.name);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);