| @@ -2,7 +2,7 @@ import React, { useEffect, useRef, useState } from 'react'; | |||||
| import { Button, Grid, InputLabel, TextField } from '@mui/material'; | import { Button, Grid, InputLabel, TextField } from '@mui/material'; | ||||
| import { GeneralConfirmWindow, notifySaveSuccess } from "../../../utils/CommonFunction"; | import { GeneralConfirmWindow, notifySaveSuccess } from "../../../utils/CommonFunction"; | ||||
| import axios from 'axios'; | import axios from 'axios'; | ||||
| import {apiPath, adobeAPIKey} from "../../../auth/utils"; | |||||
| import {appURL, apiPath, adobeAPIKey} from "../../../auth/utils"; | |||||
| import { | import { | ||||
| GET_PDF_TEMPLATE_PATH, | GET_PDF_TEMPLATE_PATH, | ||||
| GET_PDF_PATH, | GET_PDF_PATH, | ||||
| @@ -37,55 +37,55 @@ function PDF() { | |||||
| const refId = queryParams.get("refId"); | const refId = queryParams.get("refId"); | ||||
| const loadPdfForm = async (id, templateId = 0, clientId = 0) => { | const loadPdfForm = async (id, templateId = 0, clientId = 0) => { | ||||
| if (!pdfLoaded) { | |||||
| if (id > 0) { | |||||
| // axios.get(`${apiPath}${GET_PDF_TEMPLATE_PATH}`, { | |||||
| axios.get(`${apiPath}${GET_PDF_PATH}/${id}`, { | |||||
| // responseType: 'arraybuffer', // Essential for binary data | |||||
| }) | |||||
| .then((response) => { | |||||
| if (response.status === 200) { | |||||
| const res = response.data; | |||||
| setRecord({ // WIP - allow to update all record | |||||
| id : res.id, | |||||
| clientId: res.clientId, | |||||
| templateId: res.templateId, | |||||
| }) | |||||
| setFormName(res.filename); | |||||
| if (id > 0) { | |||||
| // axios.get(`${apiPath}${GET_PDF_TEMPLATE_PATH}`, { | |||||
| axios.get(`${apiPath}${GET_PDF_PATH}/${id}`, { | |||||
| // responseType: 'arraybuffer', // Essential for binary data | |||||
| }) | |||||
| .then((response) => { | |||||
| if (response.status === 200) { | |||||
| const res = response.data; | |||||
| setRecord({ // WIP - allow to update all record | |||||
| id : res.id, | |||||
| clientId: res.clientId, | |||||
| templateId: res.templateId, | |||||
| }) | |||||
| setFormName(res.filename); | |||||
| //Convert Base64 to binary data | |||||
| handlePdfUrl(base64ToBinary(res.blobValue)); | |||||
| setPdfLoaded(true); | |||||
| } | |||||
| }) | |||||
| .catch(error => { | |||||
| console.log(error); | |||||
| return false; | |||||
| }); | |||||
| //Convert Base64 to binary data | |||||
| handlePdfUrl(base64ToBinary(res.blobValue)); | |||||
| setPdfLoaded(true); | |||||
| } | |||||
| }) | |||||
| .catch(error => { | |||||
| console.log(error); | |||||
| return false; | |||||
| }); | |||||
| } else { | |||||
| axios.get(`${apiPath}${GET_PDF_TEMPLATE_PATH}`, { | |||||
| // responseType: 'arraybuffer', // Essential for binary data | |||||
| params: { | |||||
| templateId: templateId, | |||||
| clientId: clientId, | |||||
| }, | |||||
| }) | |||||
| .then((response) => { | |||||
| if (response.status === 200) { | |||||
| const res = response.data; | |||||
| setFormName(res.filename); | |||||
| //Convert Base64 to binary data | |||||
| handlePdfUrl(base64ToBinary(res.blobValue)); | |||||
| setPdfLoaded(true); | |||||
| } | |||||
| }) | |||||
| .catch(error => { | |||||
| console.log(error); | |||||
| return false; | |||||
| }); | |||||
| } | |||||
| } else { | |||||
| axios.get(`${apiPath}${GET_PDF_TEMPLATE_PATH}`, { | |||||
| // responseType: 'arraybuffer', // Essential for binary data | |||||
| params: { | |||||
| templateId: templateId, | |||||
| clientId: clientId, | |||||
| }, | |||||
| }) | |||||
| .then((response) => { | |||||
| if (response.status === 200) { | |||||
| const res = response.data; | |||||
| setFormName(res.filename); | |||||
| //Convert Base64 to binary data | |||||
| handlePdfUrl(base64ToBinary(res.blobValue)); | |||||
| setPdfLoaded(true); | |||||
| } | |||||
| }) | |||||
| .catch(error => { | |||||
| console.log(error); | |||||
| return false; | |||||
| }); | |||||
| } | } | ||||
| }; | }; | ||||
| const handlePdfUrl = (pdfBytes) => { | const handlePdfUrl = (pdfBytes) => { | ||||
| @@ -134,7 +134,7 @@ function PDF() { | |||||
| ).catch(error => { | ).catch(error => { | ||||
| console.error('Preview error:', error); | console.error('Preview error:', error); | ||||
| setError('Failed to load PDF: ' + error.message); | |||||
| // setError('Failed to load PDF: ' + error.message); // Commented out 'setError' since it's not defined | |||||
| }).then(instance => { | }).then(instance => { | ||||
| URL.revokeObjectURL(pdfUrl); | URL.revokeObjectURL(pdfUrl); | ||||
| @@ -151,7 +151,7 @@ function PDF() { | |||||
| console.log("viewer: ", DCViewer); | console.log("viewer: ", DCViewer); | ||||
| } else { | } else { | ||||
| console.error('AdobeDC not available'); | console.error('AdobeDC not available'); | ||||
| setError('Adobe SDK not loaded'); | |||||
| // setError('Adobe SDK not loaded'); // Commented out 'setError' since it's not defined | |||||
| } | } | ||||
| }; | }; | ||||
| @@ -191,29 +191,42 @@ function PDF() { | |||||
| const handleSavePdf = async (metaData, content, options) => { | const handleSavePdf = async (metaData, content, options) => { | ||||
| try { | try { | ||||
| const filledPdfBlob = new Blob([content], { type: 'application/pdf' }); | const filledPdfBlob = new Blob([content], { type: 'application/pdf' }); | ||||
| // Create FormData to send the file to Spring Boot | |||||
| const formData = new FormData(); | const formData = new FormData(); | ||||
| formData.append('file', filledPdfBlob, 'filled_form.pdf'); | formData.append('file', filledPdfBlob, 'filled_form.pdf'); | ||||
| formData.append('record', JSON.stringify(record)); | formData.append('record', JSON.stringify(record)); | ||||
| setIsSaving(true); | setIsSaving(true); | ||||
| // Send the filled PDF to your Spring Boot backend's save endpoint | |||||
| await axios.post(`${apiPath}${POST_PDF_PATH}`, formData, { | await axios.post(`${apiPath}${POST_PDF_PATH}`, formData, { | ||||
| headers: { | headers: { | ||||
| 'Content-Type': 'multipart/form-data' // Important for file uploads | |||||
| 'Content-Type': 'multipart/form-data' | |||||
| }, | }, | ||||
| }) | }) | ||||
| .then(response => { | .then(response => { | ||||
| setIsSaving(false); | setIsSaving(false); | ||||
| console.log('PDF saved on server:', response.data); | console.log('PDF saved on server:', response.data); | ||||
| const newId = response.data.data.id; | |||||
| setRecord({ | setRecord({ | ||||
| id: response.data.data.id, | |||||
| id: newId, | |||||
| clientId: record.clientId, | clientId: record.clientId, | ||||
| templateId: record.templateId | templateId: record.templateId | ||||
| }); | }); | ||||
| notifySaveSuccess()}); | |||||
| notifySaveSuccess(); | |||||
| /* | |||||
| if(response.data.data.reload){ | |||||
| setPdfLoaded(false); | |||||
| setViewerLoaded(false); | |||||
| setAdobeDCView(null); | |||||
| navigate(`/pdf/maintain/${newId}`, { replace: false }); | |||||
| } | |||||
| */ | |||||
| }); | |||||
| return { | return { | ||||
| code: window.AdobeDC.View.Enum.ApiResponseCode.SUCCESS, | code: window.AdobeDC.View.Enum.ApiResponseCode.SUCCESS, | ||||
| data: { metaData } // Return metaData to prevent t.data undefined | |||||
| data: { metaData } | |||||
| }; | }; | ||||
| } catch (error) { | } catch (error) { | ||||
| console.error('Error saving PDF:', error); | console.error('Error saving PDF:', error); | ||||