【发布时间】:2019-01-30 13:12:34
【问题描述】:
在我的 React 应用程序(使用 semanticUI)中,我在一个视图中呈现了几个组件,但是当用户想要打印页面时(例如,通过在浏览器上按 Ctrl+P)我只希望一个部分是可打印的
例如,如果这是用户看到的屏幕截图,则在触发浏览器打印时,绿色区域应显示在打印概览中。
到目前为止,我在 SCSS 文件中
@media print{
.no-print, .no-print *{ display: none !important; }
}
添加不需要的组件会使它们消失,但在打印区域有空白空间,绿色部分没有填满页面,如果这个绿色部分是可滚动的并且应该适合几页我只看到一页(一张 A4包含我在屏幕上看到的内容的纸张)
拥有
@media print {
.print-content {
display: block;
width: 100%;
height: 100%;
page-break-after: always;
overflow: visible;
}
}
无法获得相同的可打印视图
import React from 'react'
import { Grid, Image } from 'semantic-ui-react'
const GridExampleCelled = () => (
<Grid celled>
{/*another Grid.Row*/}
<Grid.Row>
<Grid.Column width={3}>
<Image src='/images/wireframe/image.png' />
</Grid.Column>
<Grid.Column width={10}> /* This should be the component to print */
<p> EveryThing Wrapped in this Grid should be printed </p>
</Grid.Column>
<Grid.Column width={3}>
<Image src='/images/wireframe/image.png' />
</Grid.Column>
</Grid.Row>
</Grid>
)
export default GridExampleCelled
【问题讨论】:
-
检查this
-
我认为是以下两种方法之一: 1. 在
print的情况下,将要打印的 div 拉伸到 100% vw/vh,位置固定,白色背景等。 2. 打开一个新页面在您创建的仅用于打印的弹出窗口中(如果打印/显示页面彼此不同,则很有用)。 -
您是否在任何地方使用过类名
.no-print或.print-content?您的自定义样式(在@media print { ... }内)仅影响具有这些类名的元素。 -
您是否仅在寻找 CSS 解决方案?因为您还可以利用“onbeforeprint”和“onafterprint”事件通过 JS 操纵组件的可见性。此外,如何定义应用程序的外部样式也很重要。你的 body 或 html 标签是否使用了“overflow: hidden”?
-
@larrydahooster 好点是的,用 JS 捕获打印命令也是一个不错的选择(我认为比仅使用 CSS 更好)但是阅读 MDN 中的文档并没有真正了解如何使用它
标签: css reactjs html printing semantic-ui-react