我的任务:基于应用程序中的数据,使用react-pdf生成PDF。然后,我想将这个生成的PDF与现有的PDF合并。
我的问题是:使用react-pdf我可以访问文档的blob数据,但据我所知,这个文件没有保存在任何地方。然而,我找到的所有用于合并PDF的JS工具,只有当您将路径传递到文件时才能合并PDF。
你能看到任何解决这个问题的方法吗?我的想法是保存这个生成的PDF服务器端,以某种方式将其从BLOB转换为PDF,使用一些合并工具,然后将最终合并的PDF带回客户端,但它似乎太(?)很复杂。
发布于 2021-09-06 05:41:33
下面是和你一样的情况下我的代码:
<Button
className={classes.download}
onClick={
async () => {
const doc = <InvoicePDF number={number} date={date} totalHours={totalHours} formattedHours={formattedHours} />
const asPdf = pdf()
asPdf.updateContainer(doc)
let initialBlob = new Blob([fileRef.current.files[0]], { type: 'application/pdf' })
let appendixBlob = await asPdf.toBlob()
let merger = new PDFMerger()
await Promise.all([initialBlob, appendixBlob].map(async (file) => await merger.add(file, null)))
const mergedPdf = await merger.saveAsBlob()
// merger = null
// initialBlob = null
// appendixBlob = null
saveAs(mergedPdf, `Appendix${number}.pdf`)
}}
>
Download
</Button>其中InvoicePDF是用react-pdf/renderer创建的,而pdf-merger-js是我合并它们的。
它工作,它做它所需要的,但有时它不会按我需要的顺序合并它们。
https://stackoverflow.com/questions/66199932
复制相似问题