Skip to content

Commit

Permalink
fix: multiple blank pages in generated pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
asartalo committed Aug 8, 2021
1 parent aa4997e commit 1941b72
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"@typescript-eslint/parser": "^4.28.1",
"canvas": "^2.8.0",
"html2pdf.js": "git+https://github.com/eKoopmans/html2pdf.js.git#ccb3444d461844bd4949eb1f698a693af2bab17a",
"jspdf": "^2.3.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^5.2.0",
Expand Down Expand Up @@ -108,4 +107,4 @@
"standard-version": "^9.3.0",
"ts-node": "^10.0.0"
}
}
}
6 changes: 6 additions & 0 deletions src/components/PaperPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ const paperPageStyles = makeStyles(() => ({
boxShadow: 'none',
margin: 0,
},

'.print-ready &': {
borderRadius: 0,
boxShadow: 'none',
margin: 0,
},
},

content: {
Expand Down
9 changes: 7 additions & 2 deletions src/components/forms/CustomizeForm.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import React, {
ReactNode, FormEvent, ChangeEvent,
} from 'react';
import html2pdf from 'html2pdf.js';
import {
Button, ButtonGroup, Collapse, makeStyles, Select, Typography,
Button, Collapse, makeStyles, Select, Typography,
} from '@material-ui/core';
import Alert from '@material-ui/lab/Alert';
import FieldSet from './FieldSet';
Expand Down Expand Up @@ -63,17 +64,20 @@ const CustomizeForm = ({
if (submitType === 'pdf') {
const element = document.querySelector('#paper-preview');
if (element !== null) {
element.classList.add('print-ready');
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
html2pdf(element, {
filename: `${name} ${timeStamp()}`,
pagebreak: { mode: 'css' },
html2canvas: {
scale: 3,
},
jsPDF: {
format: options.paperSize.code,
orientation: options.orientation,
},
});
}).then(() => element.classList.remove('print-ready'))
.catch((e) => console.error(e));
}
} else {
window.print();
Expand Down Expand Up @@ -187,6 +191,7 @@ const CustomizeForm = ({
>
Generate PDF
</Button>
<p>Warning: Generate PDF is experimental.</p>
</form>
</div>
);
Expand Down
7 changes: 5 additions & 2 deletions src/custom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ interface HTML2PDFOptions {
html2canvas?: HTML2CanvasOptions;
jsPDF?: JsPDFOptions;
pagebreak?: {
mode: PageBreakMode | PageBreakMode[];
mode?: PageBreakMode | PageBreakMode[];
before?: string | string[];
after?: string | string[];
avoid?: string | string[];
};
enableLinks?: boolean;
}
type Html2PDF = (element: AnyElement, options?: HTML2PDFOptions) => void;
type Html2PDF = (element: AnyElement, options?: HTML2PDFOptions) => Promise<unknown>;
declare module 'html2pdf.js' {
const html2pdf: Html2PDF;
export default html2pdf;
Expand Down

0 comments on commit 1941b72

Please sign in to comment.