将Chrome打印为PDF,没有边距

时间:2019-07-15 20:19:28

标签: google-chrome-headless

我知道以前已经发布过此消息,但是似乎没有可用的解决方案对我有用。我正在使用节点包html-pdf-chrome,它只是一个无头的镀铬包装。

我的问题是我需要将html页面转换为PDF,并且我希望PDF能够边到边显示。但是,我只能删除上边距。即使将边距设置为零,左右边距仍然存在。我还将整个页面明确固定为8.5 / 11大小。

在CSS中,我设置了媒体打印选项:

@media print {
    @page {
        size: 8.5in 11in;
        margin: 0 !important;
    }
}

进行实际转换时,所有边距都设置为零:

htmlPdf.create(html, {
    host: 'localhost',
    port: 9222,
    printOptions: {
        marginTop: 0,
        marginBottom: 0,
        marginLeft: 0,
        marginRight: 0,
        printBackground: true
    },
    totalPages: 1
}).then((pdf) => {...});

尽管如此,左右仍有大约10px的空白不会消失。还有其他方法可以删除边距吗?

0 个答案:

没有答案
相关问题