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

Let user define margin #29

Closed
simonschllng opened this issue Jun 29, 2018 · 18 comments
Closed

Let user define margin #29

simonschllng opened this issue Jun 29, 2018 · 18 comments
Assignees

Comments

@simonschllng
Copy link

simonschllng commented Jun 29, 2018

A printer usually cannot print to the very edge of the paper. This library however sets the page margins to 0 here:

https://github.com/gregnb/react-to-print/blob/725e060ed02a6077420f5be2897990b60f9ee46a/src/index.js#L162

What is the reason for that? It causes my page to look ugly and cuts away some part of the text. Can we make this style optional or over-writable by a prop?

@codesignist
Copy link

As you might notice margin 0 removes the date/time from top.
https://github.com/gregnb/react-to-print/blob/725e060ed02a6077420f5be2897990b60f9ee46a/src/index.js#L160
Need an another solution.

@simonschllng
Copy link
Author

For me this is more a hack than a solution. The browser adds those headings and should give the user a way to remove them. My browser (google chrome 67) offers this option:
image

Could you at least make this optional? Like if you set property pageStyle="@page { size: auto; margin: 25mm; } }" it gets overwritten, if not it takes the current default?

@gregnb
Copy link
Collaborator

gregnb commented Jul 3, 2018

@simonschllng Sorry for the delayed response. So you're proposing two different solutions:

  1. Adding a prop called 'pageStyle' where a user can specify styles for the page itself
  2. A prop to turn the default pageStyle off/on

If you want to submit a PR for solution 1 I would accept it. That way you can style it however you see fit. As pointed out it was added to remove the time, but I would be willing to allow users to override if it they really want to

@codesignist
Copy link

First option is more appropriate.

@gregnb
Copy link
Collaborator

gregnb commented Jul 6, 2018

Anyone want to take this work on up on the first option?

@gregnb gregnb self-assigned this Jul 10, 2018
@gregnb
Copy link
Collaborator

gregnb commented Jul 10, 2018

I've taken care of this issue by adding a prop called pageStyle which you can then override everything and provide whatever styles you wish.

If you want nothing to be there simply:

 <ReactToPrint
    pageStyle={""}
 />

@gregnb gregnb closed this as completed Jul 10, 2018
@NguyenTheSon
Copy link

NguyenTheSon commented Nov 26, 2018

pageStyle not working! I want custom margin top, bottom.

pageStyles={'@media print { body { -webkit-print-color-adjust: exact; } @page { size: A4; margin: 200mm !important }}'}

@MatthewHerbst
Copy link
Owner

@NguyenTheSon does it not work in general, or does it just not work with media/other queries?

@NguyenTheSon
Copy link

NguyenTheSon commented Nov 27, 2018

@MatthewHerbst all not working! can you help me!

@nikhils4
Copy link
Contributor

nikhils4 commented Jun 30, 2021

@gregnb Any concrete solution, on how one can give page margins using @page CSS rule, while using react-to-print?

@nikhils4
Copy link
Contributor

nikhils4 commented Jul 1, 2021

I got the solution for this,

As I was using React, so made a function to get the page margins and called the same inside style tag in the JSX, and it worked,

const getPageMargins = () => {
  return `@page { margin: ${marginTop} ${marginRight} ${marginBottom} ${marginLeft} !important; }`;
};
<style>{getPageMargins()}</style>

Hope it helps!

@MatthewHerbst
Copy link
Owner

@nikhils4 thanks for sharing! If you'd like, feel free to make a PR to add that to the CSS tricks section of the README! I'm happy to do that too if you'd like, but would love for you to take the git credit 😄

@nikhils4
Copy link
Contributor

nikhils4 commented Jul 1, 2021

Yeah sure @MatthewHerbst, would be glad to do that 💯

Done, #394

@15tanvir
Copy link

Hii ,

I got the solution for this,

As I was using React, so made a function to get the page margins and called the same inside style tag in the JSX, and it worked,

const getPageMargins = () => {
  return `@page { margin: ${marginTop} ${marginRight} ${marginBottom} ${marginLeft} !important; }`;
};
<style>{getPageMargins()}</style>

Hope it helps!

Hi @nikhils4 can't we add a margin to the specific page only? As the above function added margin to all pages.

@MatthewHerbst
Copy link
Owner

@15tanvir you'd have to make a class to apply it to and have that class only wrap content on the first page

@15tanvir
Copy link

15tanvir commented Jan 16, 2023 via email

@MatthewHerbst
Copy link
Owner

MatthewHerbst commented Jan 16, 2023

If you look at the docs, I think you want something like:

@page :first {
  /* Applies styles to the first page only */
}

@ngmtuandev
Copy link

I've taken care of this issue by adding a prop called pageStyle which you can then override everything and provide whatever styles you wish.

If you want nothing to be there simply:

 <ReactToPrint
    pageStyle={""}
 />

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

No branches or pull requests

8 participants