3

有没有办法在Linux的无头版Chrome中启用simulated device modeemulated print media mode如何在无头Chrome中启用打印介质模拟?

可以在DevTools手工完成,像这样:

Enable print media emulation

的目标是采取模拟印刷媒体模式全页面截图不注射或修改任何CSS。我已经可以通过Node.js截取网页截图,但不能在模拟打印媒体模式下截图。我搜索了,但我也无法找到有用的CLI switch

例子:StackOverflow print emulation

如何通过CLI或Node.js的做到这一点编程?它甚至有可能吗?


使用Node.js的与无头的Chrome DevTools协议交互参考:https://developers.google.com/web/updates/2017/04/headless-chrome

-

更新:我已经研究了Chrome DevTools Protocol Viewer文档下仿真,有是Emulation.setEmulatedMedia的一项规定。设置 Emulation.setEmulatedMedia({media: "print"});呈现处于打印模拟模式的页面。

回答

2

仿真的最新文章Chrome DevTools Protocol Viewer文档中,有关于仿真介质的部分。这一条线将使打印介质仿真:如果视口宽度被设置为纸张的8.5in片(〜816px @ 96 DPI)的

Emulation.setEmulatedMedia({media: "print"}); 

此外,,则屏幕截图将类似于彩色打印预习。

const viewportWidth = 816; // 8.5 in 
const viewportHeight = 1056; // 11 in 

const deviceMetrics = { 
    width: viewportWidth, 
    height: viewportHeight, 
    deviceScaleFactor: 0, 
    mobile: false, 
    fitWindow: false 
}; 
Emulation.setDeviceMetricsOverride(deviceMetrics); 
Emulation.setVisibleSize({width: viewportWidth, height: viewportHeight});