generated from SverreNystad/template_python_application
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add PDF frontend rendering functionality and related components
- Loading branch information
1 parent
011ebef
commit 7c0f212
Showing
6 changed files
with
132 additions
and
8 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
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,68 @@ | ||
// UploadComponent.tsx | ||
|
||
import React, { useState } from 'react'; | ||
import uploadPDF from '../services/uploadService'; | ||
import { Document, Page, pdfjs } from 'react-pdf'; | ||
import 'react-pdf/dist/Page/AnnotationLayer.css'; | ||
|
||
// Configure PDF.js worker | ||
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`; | ||
|
||
|
||
const UploadComponent: React.FC = () => { | ||
const [selectedFile, setSelectedFile] = useState<File | null>(null); | ||
const [pdfData, setPdfData] = useState<any>(null); | ||
const [numPages, setNumPages] = useState<number | null>(null); | ||
const [pageNumber, setPageNumber] = useState<number>(1); | ||
|
||
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
if (event.target.files && event.target.files[0]) { | ||
const file = event.target.files[0]; | ||
setSelectedFile(file); | ||
setPdfData(file); | ||
|
||
// Convert file to a data URL for react-pdf | ||
const reader = new FileReader(); | ||
reader.onload = (e) => setPdfData(e.target?.result); | ||
reader.readAsDataURL(file); | ||
} | ||
}; | ||
|
||
const onDocumentLoadSuccess = ({ numPages }: { numPages: number }) => { | ||
setNumPages(numPages); | ||
}; | ||
|
||
const handleSubmit = async (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
if (selectedFile) { | ||
try { | ||
const response = await uploadPDF(selectedFile); | ||
console.log(response); | ||
// Handle the response data | ||
} catch (error) { | ||
console.error('Error uploading file:', error); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<form onSubmit={handleSubmit}> | ||
<input type="file" accept=".pdf" onChange={handleFileChange} /> | ||
<button type="submit">Upload PDF</button> | ||
</form> | ||
|
||
{pdfData && ( | ||
<Document file={pdfData} onLoadSuccess={onDocumentLoadSuccess}> | ||
<Page pageNumber={pageNumber} /> | ||
</Document> | ||
)} | ||
|
||
{pdfData && numPages && ( | ||
<p>Page {pageNumber} of {numPages}</p> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default UploadComponent; |
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,29 @@ | ||
import axios from "axios"; | ||
import apiRoutes from "../routes/routesDefinitions"; | ||
|
||
const uploadPDF = async (file: File): Promise<Response> => { | ||
let formData = new FormData(); | ||
formData.append("pdf", file); | ||
console.log(formData); | ||
|
||
for (let [key, value] of formData.entries()) { | ||
console.log(key, value); | ||
} | ||
|
||
const response = await axios | ||
.post(apiRoutes.upload, { | ||
method: "POST", | ||
body: formData, | ||
// Add headers if necessary (e.g., for authentication) | ||
}) | ||
.then((res) => { | ||
return res; | ||
}) | ||
.catch((err) => { | ||
return err; | ||
}); | ||
|
||
return response; | ||
}; | ||
|
||
export default uploadPDF; |
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,7 +1,27 @@ | ||
import { defineConfig } from 'vite' | ||
import react from '@vitejs/plugin-react-swc' | ||
import { defineConfig } from "vite"; | ||
import react from "@vitejs/plugin-react-swc"; | ||
|
||
import path from "path"; | ||
import { createRequire } from "node:module"; | ||
import { viteStaticCopy } from "vite-plugin-static-copy"; | ||
|
||
const require = createRequire(import.meta.url); | ||
const cMapsDir = path.join( | ||
path.dirname(require.resolve("pdfjs-dist/package.json")), | ||
"cmaps" | ||
); | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig({ | ||
plugins: [react()], | ||
}) | ||
plugins: [ | ||
react(), | ||
viteStaticCopy({ | ||
targets: [ | ||
{ | ||
src: cMapsDir, | ||
dest: "", | ||
}, | ||
], | ||
}), | ||
], | ||
}); |