Skip to content

Commit

Permalink
Vote-2725: Load PDF on App Load (#302)
Browse files Browse the repository at this point in the history
* Update app.jsx with new loaded pdf

* Create new file to load PDF

* Generate PDF files updates with new loaded pdf function
  • Loading branch information
clmedders authored Sep 5, 2024
1 parent aa13758 commit 2b1a6e1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
14 changes: 14 additions & 0 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import PathSelection from 'Views/PathSelection.jsx';
import MultiStepForm from 'Views/MultiStepForm.jsx';
import {fetchData, fetchStaticData, sanitizeDOM} from 'Utils/JsonHelper.jsx';
import { HelmetProvider } from "react-helmet-async";
import loadPdf from './Utils/pdfLoader';


const currentStateId = document.getElementById('root').getAttribute('data-stateId');
const returnPath = document.getElementById('root').getAttribute('data-returnPath');
Expand All @@ -16,6 +18,16 @@ function App() {
const [fieldContent, setFieldContent] = useState('')
const [stringContent, setStringContent] = useState('')

const [pdfDoc, setPdfDoc] = useState(null);
const [form, setForm] = useState(null);

useEffect(() => {
loadPdf().then(({ pdfDoc, form }) => {
setPdfDoc(pdfDoc);
setForm(form);
});
}, []);

useEffect(() => {
fetchData("states.json", setStates);
fetchData("pages.json", setContent);
Expand Down Expand Up @@ -146,6 +158,8 @@ function App() {
registrationPath={registrationPath}
getFormStep={getFormStep}
stringContent={stringContent}
pdfDoc={pdfDoc}
form={form}
/>}

{step >= 1 &&
Expand Down
14 changes: 3 additions & 11 deletions src/Utils/GenerateFilledPDF_en.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { PDFDocument} from 'pdf-lib';
import download from "downloadjs";
import loadPdf from './pdfLoader';

const GenerateFilledPDF = async function (btnType,formData,pagesKept) {
// Fetch the PDF with form fields
const lang = document.documentElement.lang;
const locale = lang !== "en" ? `/${lang}` : "";
const formUrl = `/data${locale}/Federal_Voter_Registration_${lang}.pdf`;
const formPdfBytes = await fetch(formUrl).then(res => res.arrayBuffer())
// Load a PDF with form fields
const pdfDoc = await PDFDocument.load(formPdfBytes)

// Get the form containing all the fields
const form = pdfDoc.getForm()
// Fetch the PDF with form field
const { pdfDoc, form } = await loadPdf();

//-------- Get PDF Fields by machine name ------------------
const citizen = form.getRadioGroup('citizen');
Expand Down
15 changes: 3 additions & 12 deletions src/Utils/GenerateFilledPDF_es.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { PDFDocument} from 'pdf-lib';
import download from "downloadjs";
import loadPdf from './pdfLoader';

const GenerateFilledPDF = async function (btnType,formData,pagesKept) {
// Fetch the PDF with form fields
const lang = document.documentElement.lang;
//TEST SPANISH const lang = "es";
const locale = lang !== "en" ? `/${lang}` : "";
const formUrl = `/data${locale}/Federal_Voter_Registration_${lang}.pdf`;
const formPdfBytes = await fetch(formUrl).then(res => res.arrayBuffer())
// Load a PDF with form fields
const pdfDoc = await PDFDocument.load(formPdfBytes)

// Get the form containing all the fields
const form = pdfDoc.getForm()
// Fetch the PDF with form field
const { pdfDoc, form } = await loadPdf();

//-------- Get PDF Fields by machine name ------------------
const citizen = form.getDropdown('citizen'); //TEMP spanish condition field type
Expand Down
13 changes: 13 additions & 0 deletions src/Utils/pdfLoader.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { PDFDocument } from 'pdf-lib';

const loadPdf = async () => {
const lang = document.documentElement.lang;
const locale = lang!== "en"? `/${lang}` : "";
const formUrl = `/data${locale}/Federal_Voter_Registration_${lang}.pdf`;
const formPdfBytes = await fetch(formUrl).then(res => res.arrayBuffer())
const pdfDoc = await PDFDocument.load(formPdfBytes)
const form = pdfDoc.getForm()
return { pdfDoc, form };
};

export default loadPdf;

0 comments on commit 2b1a6e1

Please sign in to comment.