如何添加URL以作为可点击链接工作?

发布于 2025-02-07 17:53:26 字数 1222 浏览 3 评论 0原文

我正在使用React-PDF库生成PDF。 一切似乎都正常工作,但是我无法从 @react-pdf/Renderer中制作链接元素以正常工作。

const add_hyperlink = (url_text: string) => {
  return <Text><Link src={url_text}>{url_text}</Link></Text>;
};

在PDF文档中,它看起来像是一个链接,甚至指针在悬停时都会更改,单击链接时,在Chrome中没有发生任何事情,而在Adobe Reader中,它会导致安全块。

我尝试使用&lt; a&gt;标签,但导致相同的输出。 有人知道如何解决这个问题吗?谢谢。

编辑: 我在研究此研究时意识到的事情。注意,因为很容易与微小的差异混淆。

  1. Library @React-PDF/Renderer用于在React中创建PDF。
  2. Library React-PDF用于在React应用中显示现有的PDF。

关于使用注释选项的一些提及,包括“导入'react-pdf/dist/page/annotationlayer.css';“,使链接适当地显示了许多开发人员。但是,这属于React-PDF库,只有在需要将PDF呈现在React-App中时才有效。

在这种情况下,我正在使用@react-pdf/Renderer创建一个新的PDF,但是我正在生成一个链接来下载生成的PDF文件( https://react-pdf.org/advanced#on-the-fly-the-fly-rendering )。

我也找到了解决问题的解决方案。 我使用的链接应该重定向到另一个URL,因此问题。我使用了SRC中的最终链接,然后它运行顺利。

我希望这些观察将来对某人有帮助:)

I am generating a pdf using react-pdf library.
Everything seems to be working fine but I can't make the Link element from @react-pdf/renderer to work properly.

const add_hyperlink = (url_text: string) => {
  return <Text><Link src={url_text}>{url_text}</Link></Text>;
};

In the pdf document, it appears as a link should and even the pointer changes on hover, on clicking the link, in chrome nothing happens, while in adobe reader, it results in a security block.

I tried to use an <a> tag instead, but it resulted in the same output.
Does someone know how to resolve this? Thanks.

EDIT:
Something I realized while researching on this. Noting down because it is easy to get confused with the tiny differences.

  1. library @react-pdf/renderer is used to create pdfs in react.
  2. library react-pdf is used to display existing pdf in a react app.

There were several mentions about using annotation options and including "import 'react-pdf/dist/Page/AnnotationLayer.css';" which made links appear properly for many developers. However this belongs to react-pdf library and will only work if it is needed to render pdf within the react-app.

In this case, I am creating a new PDF with @react-pdf/renderer but I am generating a link to download the generated PDF file (https://react-pdf.org/advanced#on-the-fly-rendering).

I found the solution to my problem as well.
The link I was using is supposed to redirect to another url, and hence the problem. I used the final link in src and then it works smooth.

I hope these observations are of help to someone in the future :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

伴我心暖 2025-02-14 17:53:26
import { Document, Link, Page, Text } from '@react-pdf/renderer'

const doc = () => (
  <Document>
    <Page>
      <Link src='#Footnote'> // Notice the hash symbol
        Click me to get to the footnote
      </Link>

      // Other content here

      <Text id='Footnote'> // No hash symbol
        You are here because you clicked the link above
      </Text>
    </Page>
  </Document>
);
import { Document, Link, Page, Text } from '@react-pdf/renderer'

const doc = () => (
  <Document>
    <Page>
      <Link src='#Footnote'> // Notice the hash symbol
        Click me to get to the footnote
      </Link>

      // Other content here

      <Text id='Footnote'> // No hash symbol
        You are here because you clicked the link above
      </Text>
    </Page>
  </Document>
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文