-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 fix: check for browser PDF print support
- Loading branch information
Showing
5 changed files
with
57 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from "react"; | ||
import { RiFilePdf2Line } from "react-icons/ri"; | ||
import { useReactToPrint } from "react-to-print"; | ||
|
||
import { messageContainer } from "@/helpers/MessageContainer"; | ||
|
||
interface PrintPdfButtonProps { | ||
printRef: React.RefObject<HTMLDivElement>; | ||
} | ||
|
||
const PrintPdfButton: React.FC<PrintPdfButtonProps> = ({ printRef }) => { | ||
const handlePrint = useReactToPrint({ | ||
content: () => printRef.current, | ||
onBeforePrint() { | ||
window.printStart = Date.now(); | ||
}, | ||
onAfterPrint() { | ||
// 某些游览器没有弹出打印框,确依旧触发 onAfterPrint | ||
let printEnd = Date.now(); | ||
if (!window.printStart || printEnd - window.printStart < 1000) { | ||
messageContainer.info("Failed to print PDF"); | ||
} | ||
}, | ||
}); | ||
|
||
return ( | ||
<button | ||
onClick={handlePrint} | ||
className="p-2 text-sm font-semibold bg-white text-slate-800" | ||
> | ||
<div className="flex items-center px-5 py-1"> | ||
<RiFilePdf2Line className="mr-4" /> | ||
<span className="text-xs">PDF</span> | ||
</div> | ||
</button> | ||
); | ||
}; | ||
|
||
export default PrintPdfButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
declare global { | ||
interface Window { | ||
printStart?: number; | ||
} | ||
} | ||
|
||
export {}; |