javascript 在导轨上打印东西(字面意思是打印机)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11563480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing stuff in rails (literally to a printer)
提问by TangoKilo
I'm wondering if there was any library or gem available in Rails for printing the contents of a web page, as in literally on to paper via a printer. I was also wondering if there was any way you can specify that only a specific part of the page (e.g. a div or something) would be printed? Any pointers, advice, or links to tutorials would be appreciated!
我想知道 Rails 中是否有任何库或 gem 可用于打印网页的内容,就像通过打印机打印到纸上一样。我还想知道是否有任何方法可以指定仅打印页面的特定部分(例如 div 或其他内容)?任何指针、建议或教程链接将不胜感激!
EDIT
编辑
so I've made a stab at creating a stylesheet which will create a print friendly view, let's call it "print.css":
所以我尝试创建一个样式表,它将创建一个打印友好的视图,我们称之为“print.css”:
div.transpose-keys, div#editSong, div#navigation, div#debug{
display: none;
}
And I was wondering if there was any way I could apply it only when my application fires the print action? So that when the following is link is clicked the application applies the css above before it prints? Here's the link in my embedded ruby html:
我想知道是否有任何方法可以仅在我的应用程序触发打印操作时应用它?因此,当单击以下链接时,应用程序会在打印之前应用上面的 css?这是我嵌入的 ruby html 中的链接:
<%= link_to "PRINT", '#', onclick: "printpage()" %>
And finally my javascript calling the print function:
最后我的 javascript 调用打印函数:
function printpage()
{
window.print()
}
采纳答案by Peter Pajchl
If you add your print.css as follows
如果你添加你的 print.css 如下
<link rel="stylesheet" type="text/css" media="print" href="print.css">
and use browser print dialog, it should automatically format the page according to the style in print.css
并使用浏览器打印对话框,它应该根据print.css中的样式自动格式化页面
回答by TangoKilo
I got around my problem by using this simple solution:
我通过使用这个简单的解决方案解决了我的问题:
print.css:
打印.css:
@media print {
div.info, div#editStuff, div#navigation, div#debug {
display: none;
}
}
the @media tag ensures these are notdisplayed in the printed version
@media 标签确保这些不会显示在打印版本中
application.js:
应用程序.js:
function printpage()
{
window.print()
}
show.html.erb:
show.html.erb:
<%= link_to "PRINT", '#', onclick: "printpage()" %>
回答by Becka
<script type="text/javascript">
function PrintDiv() {
var divToPrint = document.getElementById('divToPrint');
var popupWin = window.open('', '_blank', 'width=300,height=300');
popupWin.document.open();
popupWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
popupWin.document.close();
}
</script>
<%= image_tag("print.jpg", :width=>50, :height=>30, :onclick=>"PrintDiv();", :class=>'btn') %>
回答by lightswitch05
I believe the way this is generally done is to open up a 'printer friendly' window with only the desired info on it, and from there the user prints through their web browser. You could make it a bit more user-friendly by adding some javascript in that window:
我相信通常这样做的方法是打开一个“打印机友好”窗口,其中只包含所需的信息,然后用户从那里通过他们的网络浏览器进行打印。您可以通过在该窗口中添加一些 javascript 使其更加用户友好:
window.print();
回答by Josh Moore
I use wkhtmltopdf, http://code.google.com/p/wkhtmltopdf/, and PDFKit, http://railscasts.com/episodes/220-pdfkit.
我使用 wkhtmltopdf、http://code.google.com/p/wkhtmltopdf/和 PDFKit、http://railscasts.com/episodes/220-pdfkit 。
The RailsCast is going to have a complete example while the wkhtmltopdf is a rack utility that grabs and outputs a PDF document.
RailsCast 将有一个完整的示例,而 wkhtmltopdf 是一个机架实用程序,用于抓取和输出 PDF 文档。