Skip to content

Commit

Permalink
Set print iframe width/height to page viewport (#553)
Browse files Browse the repository at this point in the history
Without this the browser sets the iframe to its default size of width 300px x length 150px, which could cause the browser to print the components differently if there are styles present that change the content based on viewport size.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe
  • Loading branch information
MatthewHerbst authored Nov 15, 2022
1 parent bd2fe60 commit 1c075f0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ node_modules
# Optional npm cache directory
.npm

# Project npm config file
.npmrc

# Build Data
dist/*
es/*
Expand Down
4 changes: 1 addition & 3 deletions examples/ComponentToPrint/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ export class ComponentToPrint extends React.PureComponent<Props, State> {
rel="stylesheet"
href="../disabled.css"
/>
<style type="text/css" media="print">{"\
@page {\ size: landscape;\ }\
"}</style>
<style type="text/css" media="print">{"@page { size: landscape; }"}</style>
<div className="flash" />
<table className="testClass">
<thead>
Expand Down
8 changes: 7 additions & 1 deletion examples/styles/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
}

.relativeCSS {
border: solid 1px #FF0000;
border: solid 1px #FF0000;
}

.testClass {
background: rgba(76, 175, 80, 0.3);
}

@media (max-width: 600px) {
.testClass {
color: rebeccapurple;
}
}
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ export default class ReactToPrint extends React.Component<IReactToPrintProps> {
}

const printWindow = document.createElement("iframe");
printWindow.width = `${document.documentElement.clientWidth}px`;
printWindow.height = `${document.documentElement.clientHeight}px`;
printWindow.style.position = "absolute";
printWindow.style.top = "-1000px";
printWindow.style.left = "-1000px";
Expand Down

0 comments on commit 1c075f0

Please sign in to comment.