我正在尝试理解如何让PDF Kit在React JS中工作。
我的简单要求是使用ReactJS和PDFKit在浏览器中呈现一个文件。
看一下教程,其中有一些参考,其中PDFKit可以在浏览器中工作,但是还不清楚这将如何应用于React JS的世界,特别是创建基于React App的项目...
http://pdfkit.org/demo/browser.html
https://www.npmjs.com/package/pdfkit#browser-usage
有没有人遇到过基于React JS和PDFKit创建React应用的工作示例?
谷歌今天晚上似乎缺少答案。
发布于 2020-07-30 05:08:23
我正是想这样做(在React应用程序中使用PdfKit客户端渲染,使用create-react- app启动)。我设法使用pdfkit-webpack-example和customize-cra让它正常工作。
发布于 2020-04-28 23:40:56
我设法找到了这个问题的答案。
只是为了给出一个概述/关键代码。在我的react应用程序中,我有这个(向我的API服务器发出post请求):
<Button
onClick={(e) => {
const { _id } = record;
fetch(`/api/pdf`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
filename: "test",
content: "Testing Content"
}),
}).then(async res => {
if (res.status === 200) {
const blob = await res.blob();
const file = new Blob(
[blob],
{type: 'application/pdf'}
);
//Build a URL from the file
const fileURL = URL.createObjectURL(file);
//Open the URL on new Window
window.open(fileURL);
}
})
>
Download
</Button>在API服务器(node express应用程序)中,我有:
const postMethod = () => async (req, res, next) => {
const doc = new PDFDocument()
let filename = req.body.filename
// Stripping special characters
filename = encodeURIComponent(filename) + '.pdf'
// Setting response to 'attachment' (download).
// If you use 'inline' here it will automatically open the PDF
res.setHeader('Content-disposition', 'attachment; filename="' + filename + '"')
res.setHeader('Content-type', 'application/pdf')
const content = req.body.content
doc.y = 300
doc.text(content, 50, 50)
doc.pipe(res)
doc.end()
}https://stackoverflow.com/questions/58798867
复制相似问题