Alter the default header/footer when printing to PDF

后端 未结 7 1653
攒了一身酷
攒了一身酷 2020-12-14 02:21

I\'m trying to use Google Chrome as a replacement of PhantomJS to render HTML into PDF. So far it\'s been working well for me. The only issue I have that I have not found an

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 02:54

    This is an update/answer to the question. As of Chromium 64 it is possible using the headerTemplate and footerTemplate parameters to printToPDF

    Using chrome remote interface here's example code that should work:

    return new Promise(async function (resolve, reject) {
        const url = "";
        const [tab] = await Cdp.List()
        const client = await Cdp({ host: '127.0.0.1', target: tab });
        await Promise.all([
           Network.enable(),
           Page.enable()
        ]);
    
        Page.loadEventFired(function () { 
             setTimeout(function () {
        //https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
                 resolve(Page.printToPDF({
                      displayHeaderFooter:true,
                      footerTemplate: " of "
                 }))); 
             }, 3000);
        });
        await Page.navigate({ url }); 
    };
    

提交回复
热议问题