我使用react-pdf/renderer
软件包添加功能,从我的网站下载一个pdf。但是我收到了一条错误消息:./node_modules/@react-pdf/font/lib/index.browser.es.js Attempted import error: 'create' is not exported from 'fontkit' (imported as 'fontkit').
我试着使用这个包的不同版本,如v2.2.0、v2.3.0和v3.0.0,但不幸的是,没有什么对我有用。我在用react v^17.0.2
。
PDF文档代码:
import { Document, Page, StyleSheet, Text, View } from "@react-pdf/renderer";
import React from "react";
const styles = StyleSheet.create({
page: {
flexDirection: "row",
backgroundColor: "#E4E4E4",
},
section: {
margin: 10,
padding: 10,
flexGrow: 1,
},
});
const InvoicePDF = () => {
return (
<Document>
<Page size="A4" style={styles.page}>
<View style={styles.section}>
<Text>Section #1</Text>
</View>
<View style={styles.section}>
<Text>Section #2</Text>
</View>
</Page>
</Document>
);
};
export default InvoicePDF;
PDF下载按钮:
import React from "react";
import InvoicePDF from "../invoicePDF/InvoicePDF";
import { pdf } from "@react-pdf/renderer";
import { saveAs } from "file-saver";
const InvoiceFooter = ({ data }) => {
return (
<button
className="w-full text-white text-sm font-bold px-6 py-4 rounded-full transition bg-borderOne hover:bg-gray-200 hover:text-borderOne"
onClick={async () => {
const doc = <InvoicePDF />;
const asPdf = pdf([]);
asPdf.updateContainer(doc);
const blob = await asPdf.toBlob();
saveAs(blob, "document.pdf");
}}
>
Download PDF
</button>
);
};
export default InvoiceFooter;
发布于 2022-09-01 06:20:01
在做了很多研究之后,这个对我有用。添加:
"@react-pdf/rendered":" 2.1.0",
"@react-pdf/font": "2.2.0",
To your package.json dependencies.
然后添加:
"resolutions": {
"@react-pdf/font": "2.2.0"
},
如果您已经有了解决方案对象,那么只需将此版本添加到其中即可。注意版本号中没有^。
然后拆卸yarn.lock或Packy-lock.json并重新运行纱线/npm安装。
发布于 2022-09-01 13:10:25
"@react-pdf/renderer": "2.1.0",
"@react-pdf/font": "2.2.0",
"resolutions": {
"@react-pdf/font": "2.2.0"
},
到package.json依赖项。
发布于 2022-09-13 18:06:56
如果您使用的是npm
而不是yarn
,您可以尝试以下方法
从3.0.0
@react-pdf/renderer
从^2.2.0
更新为overrides
,这是my package.json
的一个overrides
键,看起来像;"overrides": {
"@react-pdf/font": "2.2.1",
"@react-pdf/pdfkit": "2.1.0"
}
您的package.json
文件应该类似于;
// other stuff in the package.json file
"dependencies": {
// other dependencies in the package.json file
"@react-pdf/renderer": "3.0.0"
}
"overrides": {
"@react-pdf/font": "2.2.1",
"@react-pdf/pdfkit": "2.1.0"
}
// other stuff in the package.json file
https://stackoverflow.com/questions/73543912
复制相似问题