How to print a web page without breaking the table content in JavaScript All In One
How to print a web page without breaking the table content in JavaScript All In One
使用 JavaScript 如何在不破坏表格内容的情况下打印一个网页
error
the table content is divided into two parts ❌
原理分析
- 导出全屏截图
- 把截图转换成 PDF 文件
solutions
html2canvas
puppeteer
print.css
Chrome API
Screen Capture API
async function startCapture(displayMediaOptions) {
let captureStream = null;
try {
captureStream =
await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);
} catch (err) {
console.error(`Error: ${err}`);
}
return captureStream;
}
function startCapture(displayMediaOptions) {
return navigator.mediaDevices
.getDisplayMedia(displayMediaOptions)
.catch((err) => {
console.error(err);
return null;
});
}
https://developer.mozilla.org/en-US/docs/Web/API/Screen_Capture_API/Using_Screen_Capture
getDisplayMedia
getDisplayMedia()
getDisplayMedia(options)
https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getDisplayMedia
https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API
https://developer.mozilla.org/en-US/docs/Web/API/MediaStream_Recording_API
demos
const capture = async () => {
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
const video = document.createElement("video");
try {
const captureStream = await navigator.mediaDevices.getDisplayMedia();
video.srcObject = captureStream;
context.drawImage(video, 0, 0, window.width, window.height);
const frame = canvas.toDataURL("image/png");
captureStream.getTracks().forEach(track => track.stop());
window.location.href = frame;
} catch (err) {
console.error("Error: " + err);
}
};
capture();
refs
https://stackoverflow.com/questions/77071886/how-to-remove-table-breaking-from-puppeteer-pdf
https://hackernoon.com/how-to-take-screenshots-in-the-browser-using-javascript-l92k3xq7
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17690324.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2022-09-09 js charAt & String.fromCharCode All In One
2022-09-09 LeetCode 两数相加算法题解 All In One
2021-09-09 el-form & dynamic required & form validator All In One
2021-09-09 GitLab CI/CD bugs All In One
2020-09-09 CSS Multiple Columns
2020-09-09 Professional JavaScript for Web Developers 4th Edition
2020-09-09 js 金融数字格式化 All In One