如何通过CSS覆盖Mozilla Firefox打印设置 实现FullCalendar打印自动适配
核心问题解答
目前所有主流浏览器均未开放JS接口允许页面直接设置window.print()触发的打印参数(包括缩放比例、纸张大小等),该限制是出于保护用户打印配置自主权的设计,没有绕过方法。你要的效果完全可以通过修正打印CSS实现,不需要升级FullCalendar。
原有代码的问题
zoom是非标准属性,Firefox完全不支持,所以写了不生效size属性只能作用于@page规则,写在table选择器下无效@-moz-document规则在Firefox 61+版本已经被默认禁用,用来做Firefox专属适配已经失效
可行适配方案
改用标准CSS属性实现缩放,同时可直接裁剪时间线区域只保留8:00-23:00的内容,不需要依赖缩放硬塞:
- 用
transform: scale()替代zoom,配合transform-origin: top left避免缩放后内容偏移 - 调整
.fc-time-area的位置,直接隐藏8点前和23点后的时间刻度,不需要展示多余内容 - 统一在
@page中指定纸张规格,不需要做浏览器区分
修正后的打印CSS
@media print { @page { size: A4 landscape !important; margin: 10mm !important; /* 统一设置页边距避免内容被裁切 */ } body { margin: 0; } .fc-unthemed { margin-top: -5rem !important; .fc-view-container { .fc-head { .fc-resource-area { width: 8% !important; } .fc-scroller { margin-bottom: 0 !important; border-bottom: #000 0.2rem solid !important; } .fc-time-area .fc-scroller .fc-scroller-canvas { width: 1200px; /* 裁剪不需要的时段:左移8小时对应的宽度,隐藏0-8点内容 */ overflow: hidden; position: relative; left: calc(100% * 8 / 24 * -1); } } .fc-body .fc-timeline-event { border-top-width: 10px !important; height: 90px !important; margin-top: 1px !important; } } } table { width: auto; /* 用标准transform实现缩放,全浏览器兼容 */ transform-origin: top left; transform: scale(0.6); /* 缩放后元素实际占位不变,调整负margin抵消空白 */ margin-right: -40%; margin-bottom: -40%; } }
调试方法
在Firefox中打开页面后按Ctrl+P唤起打印预览,调整transform: scale()的数值和.fc-scroller-canvas的左移距离,直到预览效果满足:时间线仅显示8:00-23:00、内容完整铺满纸张即可,不需要用户手动调整缩放。
内容的提问来源于stack exchange,提问作者Miska Rantala
相关产品推荐
相关产品推荐

