如何在无头Chrome实例上设置缩放级别?

时间:2018-08-29 08:19:22

标签: google-chrome-headless

我想模仿用户在查看我的网页时缩小到80%的情况。类似于

`google-chrome --headless --disable-gpu --screenshot --zoom=0.8 --window-size=1920,1080 http://www.example.com/`

但是“ zoom”属性不存在,在此列表中我找不到任何可比的东西:

https://peter.sh/experiments/chromium-command-line-switches/

对于人们想做的事情,这肯定是很普遍的事情,所以我觉得我一定缺少一些简单的东西...

2 个答案:

答案 0 :(得分:1)

重新添加这个已删除但正是我正在寻找的答案。

--force-device-scale-factor=1.5 

当您获得屏幕截图时,这将在无头实例上设置比例因子。假设您想要 1000 像素 x 1000 像素网页的 500 像素 x 500 像素图像。然后使用:

google-chrome --headless --force-device-scale-factor=0.5 --window-size=1000,1000 --screenshot=out.png https://mywebsite.com

答案 1 :(得分:0)

您不需要为此使用chrome无头功能-只要您在那里进行缩放,chrome就会渲染您的CSS。这是我使用的CSS:

@media print{
    @page { margin: 2%; size: A4 }
     body { margin: 1.6cm; zoom: .7;}
     footer {
        position: fixed;
        bottom: 1;
     }
     header {
        position: fixed;
        top: 1;
     }
  }

注意正文中的zoom: .7 ...发出以下Chrome无头调用:

chrome --headless --disable-gpu --print-to-pdf \
    'https://qto.fi:442/qto/view/installations_doc?&bid=0&as=print-doc'

奖金:如果您想要带有某些徽标等的自定义页眉和页脚,只需添加页眉和页脚标签:

 <header><p>header</p></header>
 <footer><p>footer</p></footer>