在新窗口中打开PDF并在Rails 4中调用window.print() [英] Open PDF in new window and call window.print() in Rails 4

查看:101
本文介绍了在新窗口中打开PDF并在Rails 4中调用window.print()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是让用户单击一个链接,该链接现在当前称为控制器操作并返回 send_data(label.pdf_text, type: 'application/pdf', disposition: 'inline') 这将在其自己的窗口中显示pdf.

My objective is for the user to click on a link which right now currently calls a controller action and returns send_data(label.pdf_text, type: 'application/pdf', disposition: 'inline') This displays the pdf in its own window.

我想做的是,当按下link_to时,它将调用控制器动作以打开一个虚拟html文件,该文件会自动在javascript中调用, window.print();

What I want to do is when the link_to is pressed, it calls the controller action to open up a dummy html file which automatically calls in javascript, window.print();

因此,在这种情况下,按下链接并在新的窗口/选项卡中打开pdf,然后立即调用window.print();. javascript功能以显示打印预览.

So in this case, link is pressed and opens up the pdf in a new window/tab and immediately calls the window.print(); javascript function to show a print preview.

有什么建议吗?

推荐答案

将此js添加到您的html文件中,其中包含pdf:

Add this js to your html file witch contains pdf:

<script>
  function ready() {
    window.print();
  }

  document.addEventListener("DOMContentLoaded", ready)
</script>

就绪功能将启动,并在加载HTML页面后调用 window.print();

ready function will be launch and call window.print(); after your html page is load

这篇关于在新窗口中打开PDF并在Rails 4中调用window.print()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆