如何创建打印机友好的 HTML 页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23792124/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:51:30  来源:igfitidea点击:

How do I create a printer friendly HTML page?

htmlcssruby-on-railsprinting

提问by pyRabbit

How do I change or remove layouts from my application on a specific Rails view to allow my users to have a more enjoyable printing experience? (No navbar, or other extra data)

如何在特定 Rails 视图上更改或删除应用程序中的布局,以使我的用户获得更愉快的打印体验?(没有导航栏或其他额外数据)

I have been banging my head against the table for a few hours now trying to figure this out. I have tried using render layout: falsewith no success.

几个小时以来,我一直把头撞在桌子上,试图弄清楚这一点。我尝试使用但render layout: false没有成功。

I have tried creating an action like below but it didn't work for me either:

我曾尝试创建一个像下面这样的动作,但它对我也不起作用:

def print
  respond_to do |format|
    format.html { render layout: false } 
  end
end

I tried linking to this action by using the following:

我尝试使用以下方法链接到此操作:

<%= link_to 'Printer Friendly Version', product_path(@product), :action => 'print', target: '_new' %>

<%= link_to 'Printer Friendly Version', product_path(@product), :action => 'print', target: '_new' %>

Am I just completely off base here? How can I better approach this obstacle? I do not want to use PDF or Javascript, I just want to render printer-friendly HTML.

我完全不在基地吗?我怎样才能更好地解决这个障碍?我不想使用 PDF 或 Javascript,我只想呈现对打印机友好的 HTML。

回答by Paul Sasik

You can do it just with CSS like this: See full article here.

您可以像这样使用 CSS 来实现:在此处查看完整文章。

<style type="text/css">
@media print{
  body{ background-color:#FFFFFF; background-image:none; color:#000000 }
  #ad{ display:none;}
  #leftbar{ display:none;}
  #contentarea{ width:100%;}
}
</style>

It uses a simplified CSS when it detects that the page is being sent to the printer.

当它检测到页面正在发送到打印机时,它使用简化的 CSS。

-- W3Schools link --.

- W3Schools 链接 -

Also see this article:

另见这篇文章

Abstract: How to make printer friendly web pages

If your web site delivers value to your audience, chances are they are going to want to print pages out. Making sure desktop printers handle your web site well is another aspect of building a great user-experience.

This article takes a quick look at using Cascading Style Sheets to design printed web pages.

摘要:如何制作打印机友好的网页

如果您的网站为您的受众提供价值,那么他们很可能想要打印页面。确保桌面打印机能够很好地处理您的网站是建立良好用户体验的另一个方面。

本文快速了解如何使用级联样式表来设计印刷网页。