REECT:下载带有道具和React-PDF的PDF文件?
我想在UI中生成基于Props的PDF并下载。
单击按钮后,将从API获取道具数据
有这样的文件。
const MyDoc = ({ text = "default" }) => {
return (
<Document>
<Page>
<Text>dsdsds{text}</Text>
</Page>
</Document>
);
};
- 使用PDFViewer,可以更新或刷新查看器吗?
const [text, setText] = useState("state");
function change(){
setText("updateText")
}
return (
<>
<PDFViewer width="100%" height="850px">
<MyDoc text={{text}}/>
</PDFViewer>
<button onClick={change}>change props</button>
</>
);
};
- 使用BlobProvider,可以更新或刷新URL吗?
const [text, setText] = useState("state");
function change(){
setText("updateText")
}
return (
<>
<BlobProvider
document={<MyDoc text={{text}}/>} >
{({ url }) => (
<a
href={url}
target="_blank"
rel="noreferrer noopener"
>
<b>Go to PDF</b>
</a>
)}
</BlobProvider>
<button onClick={change}>change props</button>
</>
);
};
- 我认为使用PDFDownloadlink是不可能的,但是使用PDF方法或USEPDF挂钩
const blob = pdf(MyDoc).toBlob();
//how can I add props to MyDoc
const [instance, updateInstance] = usePDF({ document: MyDoc });
//how can I add props to MyDoc
function change(){
updateInstance({ document: MyDoc });
}
if (instance.loading) return <div>Loading ...</div>;
if (instance.error) return <div>Something went wrong: {error}</div>;
return (
<>
<a href={instance.url} download="test.pdf">
Download
</a>
<button onClick={change}>change props</button>
</>
);
,或者react-pdf是否有其他更简单的方法,或者我使用的是错误的,非常感谢您
I want to generate a pdf based in props from the UI and download it.
The props data will be fetched from an API after button click
Having a document like this.
const MyDoc = ({ text = "default" }) => {
return (
<Document>
<Page>
<Text>dsdsds{text}</Text>
</Page>
</Document>
);
};
- Using PDFViewer,can update or refresh the viewer?
const [text, setText] = useState("state");
function change(){
setText("updateText")
}
return (
<>
<PDFViewer width="100%" height="850px">
<MyDoc text={{text}}/>
</PDFViewer>
<button onClick={change}>change props</button>
</>
);
};
- Using BlobProvider,can update or refresh the URL?
const [text, setText] = useState("state");
function change(){
setText("updateText")
}
return (
<>
<BlobProvider
document={<MyDoc text={{text}}/>} >
{({ url }) => (
<a
href={url}
target="_blank"
rel="noreferrer noopener"
>
<b>Go to PDF</b>
</a>
)}
</BlobProvider>
<button onClick={change}>change props</button>
</>
);
};
- I don't think it will be possible using PDFDownloadLink, but neither with pdf method or usePDF hook
const blob = pdf(MyDoc).toBlob();
//how can I add props to MyDoc
const [instance, updateInstance] = usePDF({ document: MyDoc });
//how can I add props to MyDoc
function change(){
updateInstance({ document: MyDoc });
}
if (instance.loading) return <div>Loading ...</div>;
if (instance.error) return <div>Something went wrong: {error}</div>;
return (
<>
<a href={instance.url} download="test.pdf">
Download
</a>
<button onClick={change}>change props</button>
</>
);
Or is there any other simpler way with react-pdf or am I using react wrong, thank you very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用Fetch API如下
}
use the fetch api to achieve as below
}
创建一个文档组件并通过以下方式通过Props
使用React-PDF pdfDownloadlink 为您的PDF创建下载链接
Create a document component and pass props as below
use react-pdf PDFDownloadLink to create a download link for your pdf above