如何在React中下载iFrame作为PDF文档?我尝试使用JSPDF和Kendo-ReAct-PDF,但获取空白文档

发布于 2025-02-05 18:02:29 字数 1015 浏览 3 评论 0原文

import { PDFExport, savePDF } from '@progress/kendo-react-pdf';

const [contentRef, setContentRef] = useState('');

const downloadCertificate = () => {
    const element: any =
        document.querySelector('#certificate') || document.body;
    savePDF(element, { paperSize: 'A4' });
};

const onClickDownload = () => {
    downloadCertificate();
};

return (
    <div>
        <PDFExport ref={pdfExportComponent} paperSize="A4">
            <iframe
                id="certificate"
                title="View your certificate"
                className="u-els-margin-left-3x u-els-margin-right-3x"
                width="776px"
                height="600px"
                srcDoc={contentRef}
            />
        </PDFExport>
    </div>
);

使用上面的代码生成PDF,我正在导入PDF导出并将其包裹在我想导出为PDF的代码块上。在这里,iframe的srcdoc是我确切要导出的,分配给usestate。因此,在该页面呈现后,该信息存储在srcdoc中,我想将其导出为PDF,单击“返回”的按钮。

import { PDFExport, savePDF } from '@progress/kendo-react-pdf';

const [contentRef, setContentRef] = useState('');

const downloadCertificate = () => {
    const element: any =
        document.querySelector('#certificate') || document.body;
    savePDF(element, { paperSize: 'A4' });
};

const onClickDownload = () => {
    downloadCertificate();
};

return (
    <div>
        <PDFExport ref={pdfExportComponent} paperSize="A4">
            <iframe
                id="certificate"
                title="View your certificate"
                className="u-els-margin-left-3x u-els-margin-right-3x"
                width="776px"
                height="600px"
                srcDoc={contentRef}
            />
        </PDFExport>
    </div>
);

Using the above set of code to generate the pdf, I am importing the PDF Export and wrapping it around the block of code i want to export as pdf. Here the srcDoc of iframe is what I exactly want to export which assigned to a useState. So after the page renders the info is stored in srcDoc and I want to export this as pdf on click of the button which is part of the return.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文