From bca8ae3b147539bf3f004ba14b39a8c24be840d9 Mon Sep 17 00:00:00 2001 From: Mr-Sheep <24599209+Mr-Sheep@users.noreply.github.com> Date: Sun, 19 Nov 2023 02:56:05 -0600 Subject: [PATCH] customizable diploma --- app/components/generate-diploma.tsx | 14 +++++++- app/diploma/page.tsx | 48 +++++++++++++++++++++++++--- public/template_diploma.pdf | Bin 1099151 -> 1102950 bytes 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/app/components/generate-diploma.tsx b/app/components/generate-diploma.tsx index facc294..cc021dd 100644 --- a/app/components/generate-diploma.tsx +++ b/app/components/generate-diploma.tsx @@ -5,9 +5,10 @@ import downloadPDF from "./download-pdf"; type DiplomaProps = { username: string; major: string; + degree: string; }; -async function generateDiploma({ username, major }: DiplomaProps) { +async function generateDiploma({ username, major, degree }: DiplomaProps) { // Fetch the PDF with form fields const formUrl = "/template_diploma.pdf"; // const formUrl = "https://raw.githubusercontent.com/WildChickenUniversity/WildChickenUniversity/master/assets/template_diploma.pdf" @@ -39,13 +40,16 @@ async function generateDiploma({ username, major }: DiplomaProps) { // Get all fields in the PDF by their names const majorField = form.getTextField("major"); const nameField = form.getTextField("name"); + const degreeField = form.getTextField("degree"); // idiot-proof // just please don't enter a super long major name :) const widthMajorField = 450; + const widthDegreeField = 450; const widthNameField = 350; const widthMajor = major.length * 40; const widthName = username.length * 40; + const widthDegree = degree.length * 40; if (widthMajorField < widthMajor) { let fontSizeMajor = widthMajorField / major.length; majorField.setFontSize(fontSizeMajor); @@ -54,10 +58,15 @@ async function generateDiploma({ username, major }: DiplomaProps) { let fontSizeName = widthNameField / major.length; majorField.setFontSize(fontSizeName); } + if (widthNameField < widthName) { + let fontSizeName = widthNameField / major.length; + majorField.setFontSize(fontSizeName); + } // Fill in the name field majorField.setText(major); nameField.setText(username); + degreeField.setText(degree); majorField.updateAppearances( englishUnicode.test(major) ? chomskyFont : sourceHanSerif @@ -65,6 +74,9 @@ async function generateDiploma({ username, major }: DiplomaProps) { nameField.updateAppearances( englishUnicode.test(username) ? chomskyFont : sourceHanSerif ); + degreeField.updateAppearances( + englishUnicode.test(degree) ? chomskyFont : sourceHanSerif + ); // Flatten the form fields form.flatten(); diff --git a/app/diploma/page.tsx b/app/diploma/page.tsx index 74d4a64..91fdee6 100644 --- a/app/diploma/page.tsx +++ b/app/diploma/page.tsx @@ -6,22 +6,30 @@ import NavPath from "../components/nav-path"; export default function Diploma() { const [username, setUsername] = useState(""); const [major, setMajor] = useState(""); + const [degree, setDegree] = useState("Bachelor of Chicken"); const [customMajor, setCustomMajor] = useState(""); const [enableCustomMajor, setEnableCustomMajor] = useState(false); - + const [customDegree, setCustomDegree] = useState(""); + const [enableCustomDegree, setEnableCustomDegree] = useState(false); const handleSumbit = async (event: React.FormEvent) => { event.preventDefault(); const majorField: string = enableCustomMajor && customMajor !== "" ? customMajor : major; - await createDiplomaPDF({ username, major: majorField }); + await createDiplomaPDF({ username, major: majorField, degree }); }; - const handleSelectChange = async (value: string) => { + const handleSelectChangeMajor = async (value: string) => { setMajor(value); // Show the input field if the user selects "other" setEnableCustomMajor(value === "other"); }; + const handleSelectChangeDegree = async (value: string) => { + setDegree(value); + // Show the input field if the user selects "other" + setEnableCustomDegree(value === "other"); + }; + return (
@@ -77,7 +85,7 @@ export default function Diploma() { name="major" value={major} onChange={(e) => { - handleSelectChange(e.target.value); + handleSelectChangeMajor(e.target.value); setMajor(e.target.value); }} > @@ -102,6 +110,38 @@ export default function Diploma() { )}
+
+
+ + + {enableCustomDegree && ( + setCustomDegree(e.target.value)} + /> + )} +
+