Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print media query for header/footer #22

Closed
martynhoyer opened this issue Jun 6, 2021 · 3 comments
Closed

Print media query for header/footer #22

martynhoyer opened this issue Jun 6, 2021 · 3 comments

Comments

@martynhoyer
Copy link

Hi!

I found this repo after reading this thread - the commenter is right - it's a lifesaver, so thank you so much!

I have a question though - I'm trying to make a page that can be viewed as a webpage, but can also have a PDF generated from it - with a header and footer applied (mostly for page numbering).

For the body content I'm using some print CSS (via the @media print {} CSS query) to make a few style changes between the PDF version and the web version which is working well, but if I apply something like:


  @media screen {
    #header,
    #footer {
      display: none;
    }
  }

  @media print {
    #header,
    #footer {
      display: block;
    }
  }

to the CSS, the header/footer aren't rendered at all on the PDF.

I'm not quite sure why that is... I'm sort of assuming the header and footer templates get kind of pre-rendered and inserted to the PDF, so maybe aren't evaluated as being print media? I had a look at the code but can't really work it out.

Do you think it might be an easy fix? Other than this it's working beautifully for me!

@PejmanNik
Copy link
Owner

Hi, Thank you so much🤩

Can you try await page.emulateMedia('print'); before pass the page to this lib? if it works I can embed it inside the lib;

@martynhoyer
Copy link
Author

Thanks for the quick response!

It looks like that worked, although I had to use await page.emulateMediaType("print");. Looks like emulateMedia was deprecated - puppeteer/puppeteer#6084

I'm happy just keeping that in my PDF script if you don't want to force it in this library.

@PejmanNik
Copy link
Owner

Thank you for using it.
Happy to hear that. I think it can be inside the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants