简体   繁体   English

在新窗口中打开PDF并在Rails 4中调用window.print()

[英]Open PDF in new window and call window.print() in Rails 4

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. 我的目标是让用户单击一个链接,该链接当前正在调用控制器操作并返回send_data(label.pdf_text, type: 'application/pdf', disposition: 'inline')这将在其自己的窗口中显示pdf。

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

So in this case, link is pressed and opens up the pdf in a new window/tab and immediately calls the window.print(); 因此,在这种情况下,按下链接并在新窗口/选项卡中打开pdf,然后立即调用window.print();。 javascript function to show a print preview. javascript函数以显示打印预览。

Any suggestions? 有什么建议么?

Add this js to your html file witch contains pdf: 将此js添加到您的html文件中,其中包含pdf:

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

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

ready function will be launch and call window.print(); ready函数将启动并调用window.print(); after your html page is load html页面加载后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM