alvarcarto / url-to-pdf-api

Web page PDF/PNG rendering done right. Self-hosted service for rendering receipts, invoices, or any content.
MIT License
7.01k stars 774 forks source link

Adding a footer and header on every page #22

Closed kojuka closed 6 years ago

kojuka commented 6 years ago

Great work here -- its 2017 and generating PDFs is still unnecessarily complicated. I'm currently using wfhtmltopdf. I'd love to stop using it, and use this project as a micro service to handle all my pdf needs. However the one thing I can't figure out how to do is add a footer and/or header to every generated page. Header/footer would need to have stuff like logo, page number, warning, date, invoice number etc, so it needs to be more custom than the standard pdf.displayHeaderFooter option allows.

Does anyone have any experience with this? Is there something I'm missing? Thanks again for this awesome project.

kimmobrunfeldt commented 6 years ago

Thanks, I totally agree. To do that, I can think of three possible options:

  1. Using CSS. https://stackoverflow.com/questions/1360869/how-to-use-html-to-print-header-and-footer-on-every-printed-page-of-a-document

  2. Modify the PDF with a separate library after Chrome has rendered.

  3. Check if Puppeteer/Chrome print protocol would allow injecting headers and footers in a more custom way. I doubt this is supported, but haven't checked.

Out of these options, I'd recommend to investigate the first one. I'm not so excited about the second option.

fabiosussetto commented 6 years ago

@webular assuming you control how the HTML for those pages is generated, why don't you add the header/footer to the HTML using something like a template language (e.g. Jinja, Twig,..), a bit how you would handle email templates.

kojuka commented 6 years ago

@fabiosussetto I do control the html. But I find it nontrivial - at the least I don't have much experience with this. I'm open to direction -- All I really need is to add a logo and project name, and page number at the top of every generated PDF. I'd also need to add a one line footer to each page. Got any examples you can point me to on doing this with email templates like you suggest?

I'm really pleased to see the traction this project has received and would love to make the switch.

SpaceK33z commented 6 years ago

With Puppeteer 1.0.0 it is now possible to add a custom header/footer (source). This repo currently uses ^0.11.0, so after an update it might work :).

SpaceK33z commented 6 years ago

Made a PR #56 to upgrade to v1.0.0, which would fix this issue 😃.

kimmobrunfeldt commented 6 years ago

The support for this is now in master, and also released to the demo Heroku app. However there are some caveats with Chrome's footer and header support. See https://github.com/alvarcarto/url-to-pdf-api/issues/77

RayAnti2 commented 6 years ago

I found puppeteer is a good tool for converting from html to pdf. But the footerTemplate must be the same on all pages? Now I need, for example, pages' footer like this:

(page 1) pageNumber of totalNumber---------------------------vocabulary list

(page 2) vocabulary list-------------------------------------pageNumber of totalNumber

(page 3) pageNumber of totalNumber---------------------------vocabulary list

I prepare 2 Templates for odd and even num pages What should i do?

tiotamara commented 6 years ago

@RayAnti2 you can using page_script, I hope this help u