首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React JS + PDFKit

React JS + PDFKit
EN

Stack Overflow用户
提问于 2019-11-11 17:39:39
回答 2查看 3.7K关注 0票数 4

我正在尝试理解如何让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应用的工作示例?

谷歌今天晚上似乎缺少答案。

EN

回答 2

Stack Overflow用户

发布于 2020-07-30 05:08:23

我正是想这样做(在React应用程序中使用PdfKit客户端渲染,使用create-react- app启动)。我设法使用pdfkit-webpack-examplecustomize-cra让它正常工作。

Source

Live Demo

票数 2
EN

Stack Overflow用户

发布于 2020-04-28 23:40:56

我设法找到了这个问题的答案。

它是thisthis的组合。

只是为了给出一个概述/关键代码。在我的react应用程序中,我有这个(向我的API服务器发出post请求):

代码语言:javascript
复制
            <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应用程序)中,我有:

代码语言:javascript
复制
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()
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58798867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档