使用JS下载HTML窗口对象

发布于 2025-02-04 13:19:45 字数 1193 浏览 3 评论 0原文

我在这里有这个iframe,我想在内部使用pdf的窗口页面,js function print()仅显示页面,我想自动下载它,bellow是完整的代码(我也尝试了JSPDF)。

<iframe id="conteudoIframe" src="https://eproc.trf4.jus.br/eproc2trf4/controlador.php?acao=acessar_documento_publico&doc=41625504719486351366932807019&evento=20084&key=0562cc6eddee0cc4a81dd869f92328dceab34deeaa59f4a33c43da6361cf42d6&hash=08920b364dc8433ad071d6b10c7e3817"  name="superior" width="100%" height="560px"></iframe>

<script>

    downloadPdfFromIframe();

    function downloadPdfFromIframe() {
        var script = document.createElement('script');
        script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js';
        document.head.appendChild(script);
        script.onload = () => { setTimeout(download(), 20000); };

        function download() {

            var myIframe = document.getElementById("conteudoIframe").contentWindow;

            myIframe.focus();
            myIframe.print();
            myIframe.close();

            var pdf = new jsPDF();

            pdf.fromHTML(myIframe);
            pdf.save('test.pdf');

            return false;
        }
    }

</script>

i have this iframe here, and i want to doanload the window page with pdf inside, js function print() only shows the page, i want to download it automatically, bellow is the full code (i tryed with jspdf too).

<iframe id="conteudoIframe" src="https://eproc.trf4.jus.br/eproc2trf4/controlador.php?acao=acessar_documento_publico&doc=41625504719486351366932807019&evento=20084&key=0562cc6eddee0cc4a81dd869f92328dceab34deeaa59f4a33c43da6361cf42d6&hash=08920b364dc8433ad071d6b10c7e3817"  name="superior" width="100%" height="560px"></iframe>

<script>

    downloadPdfFromIframe();

    function downloadPdfFromIframe() {
        var script = document.createElement('script');
        script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js';
        document.head.appendChild(script);
        script.onload = () => { setTimeout(download(), 20000); };

        function download() {

            var myIframe = document.getElementById("conteudoIframe").contentWindow;

            myIframe.focus();
            myIframe.print();
            myIframe.close();

            var pdf = new jsPDF();

            pdf.fromHTML(myIframe);
            pdf.save('test.pdf');

            return false;
        }
    }

</script>

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

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

发布评论

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

评论(1

清晰传感 2025-02-11 13:19:45

IFRAME只是通往远程对象的窗口,因此在查看时,它们既显示已经下载的HTML环绕and和已下载的PDF,但PDF是独立的,它可以在框架之前或之后下载,您可以在框架之前看到PDF,您可以在“打印HTML”对话框后面看到PDF 。因此,如您的HTML的第二个浏览器视图所示,PDF在不同的应用程序中。

因此,您必须一次保存/打印 一次 。框架对象或PDF对象,要获得两者并非不可能,只需要使用合适的应用程序(不是浏览器)来完成。因此,就像我有屏幕截图一样,这些画布可以很容易地保存或打印为PDF。

一个基于铬的浏览器查看iframe 用于打印

一个基于Firefox的浏览器查看iframe 要使用其他任何方法(例如在第二个应用程序窗口中打开)进行打印。

如果您需要在浏览器外下载两个部分,则使用两个下载命令,以便以下是my.pdf

“

curl -o my.pdf "https://eproc.trf4.jus.br/eproc2trf4/controlador.php?acao=acessar_documento_implementacao&doc=41625504719486351366932807019&evento=20084&key=0562cc6eddee0cc4a81dd869f92328dceab34deeaa59f4a33c43da6361cf42d6&hash=08920b364dc8433ad071d6b10c7e3817&termosPesquisados="

Iframes are only a window to remote objects thus while viewing they are showing both the already downloaded html surround and the already downloaded PDF but the PDF is independent It may be downloaded before or after the frame, you can see the pdf behind the print html dialog. So the PDF is in a different application as seen in the second browser view of your HTML.

Thus you have to save/print either one at a time. Frame object or Pdf object, to get both is not impossible, just needs to be done with a suitable application (not browser). So just like here I have ScreenShots, those canvases can be easily saved or printed as PDF.

A Chrome based Browser viewing the iFrame for printing

enter image description here

A Firefox based Browser viewing the iFrame to print using any other method like open in a second application window.

![enter image description here

If you need to download the two parts outside a browser then use two Download Commands so here is my.pdf

enter image description here

curl -o my.pdf "https://eproc.trf4.jus.br/eproc2trf4/controlador.php?acao=acessar_documento_implementacao&doc=41625504719486351366932807019&evento=20084&key=0562cc6eddee0cc4a81dd869f92328dceab34deeaa59f4a33c43da6361cf42d6&hash=08920b364dc8433ad071d6b10c7e3817&termosPesquisados="
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文