惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

博客园 - 星小梦

Linux系统调整java程序启动用户,导出xlsx时抛出Permission denied异常问题。 vue2的devtools开发工具卡死现象的解决方法 apache echarts数据点重影或 Cannot read properties of undefined (reading 'type')错误问题 yarn install出现error Error: certificate has expired异常 git多代码仓库合并的方式 docker容器oshi如何获取宿主机的运行状态信息? su命令引起的nohup进程以root身份启动导致的问题 docker-compose启动服务,影响其他服务的原因 xxl-job provider netty_http server caught exception flutter升级导致的旧项目的运行环境问题排查记录 vue3插件库以及对JSX的支持。 JSX、TSX扩展语法学习材料 commitlint Lint 提交消息格式控制 Chromium历史版本下载方式 Window10 关闭Edge浏览器的多选项卡通过Alt+Tab组合键切换的方式 Postgres16数据库集成外部库dblink和postgres_fdw扩展的方式 java 泛型类型如何保留类型的信息的方式 Postgres16 常见问题 docker镜像安装字体支持,解决jdk服务验证码生成找不到字体问题 window和Linux命令行执行多条命令的方法
echarts图表在浏览器上打印出现裁剪的问题
星小梦 · 2025-12-18 · via 博客园 - 星小梦

环境

vue3
浏览器,如Chrome
打印方法,使用window.print()函数,win10快捷键Ctrl+P

js前端打印pdf

https://user-images.githubusercontent.com/50699824/211144671-53e3744a-9e36-4c87-8c61-e1c07c677b83.png
原因就是在打印时,通过@media print应用的css样式,到这一步没问题。
但是如果你在打印媒体中有改变元素或容器的宽度,同时你又使用了如echarts图表这类具有交互的canvas插件。
那么就会在打印时,出现元素或画面裁切等情况,因为chart图表这类渲染出来的尺寸并没有适应改变后的新的宽度。

我使用了beforeprint事件的方式,但是依旧不行,如下

window.addEventListener('beforeprint', ()=>{
  console.log("beforeprint")

  nextTick(()=>{
    chartInstance.resize();
  })
  // 或
  chartInstance.resize();
});
window.addEventListener('afterprint', () => {
  console.log("afterprint")
});

结论不行。

下面说个最终可以的结论,
就是改变宽度的css样式不要写在@media print中,
写到普通的class类里,当真正要打印的时候,再在根节点上应用这一块的样式,


.login-container {
// 这里写普通的样式
}

.login-container--print {
// 这里写打印是要使用的样式,在这里写会改变元素宽度的样式
}

@media print {
// 这里写不会影响宽度的样式,如隐藏一块元素,或改变颜色等等。
}

<script setup>

const printMode = ref(false);

window.addEventListener('beforeprint', ()=>{
  console.log("beforeprint")
  printMode.value = true;
  nextTick(()=>{
   // 在这里可以应用图表的方法,适应新的尺寸。
   // 唯一的缺点就是会影响页面展示的布局,如果没被`window.print()`函数的打印弹窗遮挡住,你会看到变化。
    chartInstance.resize();
  })
});
window.addEventListener('afterprint', () => {
  console.log("afterprint")
  // 这里是反操作,以恢复打印前原先的布局展示。
  printMode.value = false;
  nextTick(()=>{
   // 在这里可以应用图表的方法,适应新的尺寸。
    chartInstance.resize();
  })
});
</script>

<template>
<div class="login-container" :class="{'login-container--print': printMode}">
...
</div>
</template>

唯一的缺点就是会影响页面展示的布局,如果没被window.print()函数的打印弹窗遮挡住,你会看到变化。


Playwright生成pdf的方式

由于我使用的是Java后端连接Playwright服务生成pdf的需求,
那么该如何确保不会出现裁剪呢?

...
// 1. 先导航到指定页面
page.navigate(url);
...
// 2. 模拟打印媒体
page.emulateMedia(new Page.EmulateMediaOptions().setMedia(Media.PRINT));
...
// 3. 打印pdf,参数根据需求自行更改就行,不是核心逻辑。
page.pdf(pdfOptions);

对,这样就OK了,不会出现裁剪的情况,可以把无头模式设置为false,这样就会在开发环境
弹出浏览器观察了。

你会说打印样式该如何控制?
直接在@media print{}中书写样式就行。如

@media print {

.cls1 {
  width: 600px;
  color: #000;
  ...
}

...

}

又如果你想网网页注入js脚本或者想执行网页定义的函数?
那么你一定要看看playwright evaluating文档的用法。

到此完结。

其他参考:https://github.com/apache/echarts/issues/18147