REECT:下载带有道具和React-PDF的PDF文件?

发布于 2025-02-03 01:10:43 字数 2262 浏览 5 评论 0原文

我想在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风蛊 2025-02-10 01:10:43

使用Fetch API如下

  const onButtonClick = () => {
fetch(decodeURI(`url`)).then((response) => {
  response.blob().then((blob) => {
    const fileURL = window.URL.createObjectURL(blob)
    console.log(fileURL)
    let alink = document.createElement('a')
    alink.href = fileURL
    alink.download = 'pdf_title'
    alink.click()
  })
})

}

use the fetch api to achieve as below

  const onButtonClick = () => {
fetch(decodeURI(`url`)).then((response) => {
  response.blob().then((blob) => {
    const fileURL = window.URL.createObjectURL(blob)
    console.log(fileURL)
    let alink = document.createElement('a')
    alink.href = fileURL
    alink.download = 'pdf_title'
    alink.click()
  })
})

}

苦行僧 2025-02-10 01:10:43

REACT:使用Props和React-PDF下载PDF文件?

创建一个文档组件并通过以下方式通过Props

const [data, setData] = useState([])

    const MyDocument = () => (
  <Document>
    <Page size="A4">
      <View >
        <Text>{data.text}</Text>
      </View>
    </Page>
  </Document>
);

使用React-PDF pdfDownloadlink 为您的PDF创建下载链接

<PDFDownloadLink
    document={
        <Document
              author=''
              keywords=''
              subject=''
              title=''
            >
              <MyDocument size='A4' />
            </Document>
          }
          fileName='mypdf.pdf'
        >
          {({ blob, url, loading, error }) =>
            loading ? 'Loading document...' : 'Download now!'
          }
        </PDFDownloadLink>

React: download a pdf file onClick with props and react-pdf?.

Create a document component and pass props as below

const [data, setData] = useState([])

    const MyDocument = () => (
  <Document>
    <Page size="A4">
      <View >
        <Text>{data.text}</Text>
      </View>
    </Page>
  </Document>
);

use react-pdf PDFDownloadLink to create a download link for your pdf above

<PDFDownloadLink
    document={
        <Document
              author=''
              keywords=''
              subject=''
              title=''
            >
              <MyDocument size='A4' />
            </Document>
          }
          fileName='mypdf.pdf'
        >
          {({ blob, url, loading, error }) =>
            loading ? 'Loading document...' : 'Download now!'
          }
        </PDFDownloadLink>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文