Server Error Error: PDFDownloadLink is a web specific API. You're either using this component on Node, or your bundler is not loading react-pdf f

huangapple go评论64阅读模式
英文:

Server Error Error: PDFDownloadLink is a web specific API. You're either using this component on Node, or your bundler is not loading react-pdf f

问题

以下是代码的翻译部分:

我已经在Next.js中创建了这个报告卡生成器但在将其应用到我的项目之前我尝试使用这个简单的示例来测试`@react-pdf/renderer`但是我收到了以下错误消息

`服务器错误
错误:PDFDownloadLink是一个特定于Web的API。您要么在Node上使用此组件,要么您的捆绑器未从适当的Web构建加载react-pdf。`

```javascript
import React from 'react';
// import Report from '@/pages/report/index';
import { PDFDownloadLink, Text, Document, Page } from '@react-pdf/renderer';

const TestComponent = () => {
  return (
    <Document>
      <Page>
        <Text>Hello World</Text>
      </Page>
    </Document>
  );
};

export default function ReportGenerator() {
  return (
    <>
      <div>
        <PDFDownloadLink document={<TestComponent />} fileName="report.pdf">
          下载PDF
        </PDFDownloadLink>
        <TestComponent />
      </div>
    </>
  );
}

希望这可以帮助您理解代码部分的内容。

英文:

I have created this Report card generator in Next.js, but trying to test @react-pdf/renderer with this simple example before implement it in my project. Yet I'm receiving this error.

Server Error
Error: PDFDownloadLink is a web specific API. You&#39;re either using this component on Node, or your bundler is not loading react-pdf from the appropriate web build.

import React from &#39;react&#39;;
// import Report from &#39;@/pages/report/index&#39;;
import { PDFDownloadLink, Text, Document, Page } from &#39;@react-pdf/renderer&#39;;

const TestComponent = () =&gt; {
  return (
    &lt;Document&gt;
      &lt;Page&gt;
        &lt;Text&gt;Hello World&lt;/Text&gt;
      &lt;/Page&gt;
    &lt;/Document&gt;
  );
};

export default function ReportGenerator() {
  return (
    &lt;&gt;
      &lt;div&gt;
        &lt;PDFDownloadLink document={&lt;TestComponent /&gt;} fileName=&quot;report.pdf&quot;&gt;
          Download PDF
        &lt;/PDFDownloadLink&gt;
        &lt;TestComponent /&gt;
      &lt;/div&gt;
    &lt;/&gt;
  );
}

答案1

得分: 1

Next.js试图在构建时静态呈现PDF,但与PDFDownloadLink不兼容,因为它需要在客户端处理。我通过使用钩子来有条件地在客户端而不是在静态站点构建中呈现它来解决了这个问题。

英文:

Next.js is trying to statically render the PDF at build time which doesn't work with PDFDownloadLink since it needs to be handled client side. I was able to resolve this by using hooks to conditionally render it by the client instead of in the static site build.

export default function ReportGenerator () {

  const [isClient, setIsClient] = useState(false)

  useEffect(() =&gt; {
    setIsClient(true)
  }, [])
  
  return (
    &lt;&gt;
      { isClient ?
        &lt;div&gt;
          &lt;PDFDownloadLink document={&lt;TestComponent /&gt;} fileName=&quot;report.pdf&quot;&gt;
            Download PDF
          &lt;/PDFDownloadLink&gt;
          &lt;TestComponent /&gt;
        &lt;/div&gt;
      : null}
    &lt;/&gt;
  )
}

答案2

得分: 0

我遇到了完全相同的错误,并尝试了几种解决方案,但这是最简单且已获 Nextjs 认可的方法。只需在文件顶部包含&#39;use client&#39;指令。有关更多信息,请查看:https://nextjs.org/docs/getting-started/react-essentials#the-use-client-directive

英文:

I ran into the exact same error, and tried several solutions, but this is the easiest and Nextjs aproved.
Just include the &#39;use client&#39; directive in the top of your file.
For more informaton you can check this: https://nextjs.org/docs/getting-started/react-essentials#the-use-client-directive

huangapple
  • 本文由 发表于 2023年4月13日 23:38:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定