-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from Callhub-Connect/update-pdf
Implemented save pdf changes button
- Loading branch information
Showing
3 changed files
with
103 additions
and
18 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
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
54 changes: 40 additions & 14 deletions
54
src/helper-components/pdf-manager-component/PdfViewerComponent.jsx
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 |
---|---|---|
@@ -1,39 +1,65 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { useEffect, useRef, useState } from 'react'; | ||
|
||
export default function PdfViewerComponent(props) { | ||
const containerRef = useRef(null); | ||
const [pdfInstance, setPdfInstance] = useState(null); | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
useEffect(() => { | ||
console.log("loeading new") | ||
const container = containerRef.current; | ||
// eslint-disable-next-line no-unused-vars | ||
let instance, PSPDFKit; | ||
let PSPDFKit; | ||
(async function () { | ||
PSPDFKit = await import('pspdfkit'); | ||
|
||
PSPDFKit.unload(container); // Ensure that there's only one PSPDFKit instance. | ||
|
||
instance = await PSPDFKit.load({ | ||
// Container where PSPDFKit should be mounted. | ||
PSPDFKit.unload(container); // Ensure there's only one instance | ||
|
||
const instance = await PSPDFKit.load({ | ||
container, | ||
// The document to open. | ||
document: props.document, | ||
// Use the public directory URL as a base URL. PSPDFKit will download its library assets from here. | ||
baseUrl: `${window.location.protocol}//${window.location.host}/${process.env.PUBLIC_URL}`, | ||
}); | ||
|
||
setPdfInstance(instance); // Set instance here inside async function | ||
})(); | ||
|
||
return () => { | ||
if (PSPDFKit && container) { | ||
PSPDFKit.unload(container); | ||
} | ||
}; | ||
}, [props.document]); // Dependency array should contain props.document | ||
|
||
return () => PSPDFKit && PSPDFKit.unload(container); | ||
}, [props]); | ||
// eslint-disable-next-line no-unused-vars | ||
const exportPdf = async () => { | ||
if (pdfInstance) { | ||
const pdfData = await pdfInstance.exportPDF(); | ||
return new Blob([pdfData], { type: "application/pdf" }); | ||
} | ||
}; | ||
|
||
// Expose the exportPdf function to parent via props | ||
useEffect(() => { | ||
if (pdfInstance) { | ||
const exportPdf = async () => { | ||
const pdfData = await pdfInstance.exportPDF(); | ||
return new Blob([pdfData], { type: "application/pdf" }); | ||
}; | ||
|
||
props.onInstanceChange({ | ||
exportPdf, | ||
}); | ||
} | ||
// eslint-disable-next-line | ||
}, [pdfInstance]); | ||
|
||
// const instance = PSPDFKit.instance.default; | ||
// instance.exportPDF().then(arrayBuffer => { | ||
// const blob = new Blob ( [arrayBuffer], { type: "application/pdf" }); | ||
// }); | ||
|
||
return ( | ||
<div | ||
ref={containerRef} | ||
style={{ width: '99%', height: '100%' }} | ||
style={{ width: '100%', height: '100%' }} | ||
/> | ||
); | ||
} |