Alter the default header/footer when printing to PDF

后端 未结 7 1607
攒了一身酷
攒了一身酷 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 03:01

    If you really want to do this in a non-hacky way, you have to get down to the chrome devtools protocol since the command line interface doesn't support much (aka --print-to-pdf is all you get, no print options)

    The protocol is documented here: https://chromedevtools.github.io/devtools-protocol/tot

    Client libraries are available in many languages, available via package managers.

    Here's some examples I put together to demonstrate usage:

    • PHP: https://github.com/jehoshua02/experiment-chrome-devtools-protocol-php
    • Node: https://github.com/jehoshua02/experiment-chrome-remote-interface-node

    The Page.printToPDF protocol method supports arguments for passing custom markup for header and footer.

    Basically, the protocol is defined in a protocol.json, which client libraries consume to generate the classes/methods for use in any application. These methods encapsulate the lower level communications with the protocol.

    All I had to do was install a client library package (via npm or composer), make sure chrome was installed, write a little code, and I'm generating pdfs with no header/footer! Fantastic.

    0 讨论(0)
提交回复
热议问题