From 61531efa64d0376641c1eb426e8bb64b0a9b8ea1 Mon Sep 17 00:00:00 2001 From: JOYCEQL <1449239013@qq.com> Date: Wed, 15 Jan 2025 23:58:22 +0800 Subject: [PATCH] feat: export json && window.print to pdf --- .../src/app/app/workbench/[id]/page.tsx | 2 +- .../components/preview/EducationSection.tsx | 3 +- .../src/components/preview/ProjectSection.tsx | 2 +- .../src/components/shared/PdfExport.tsx | 222 ++++++++++++++++-- .../components/templates/ClassicTemplate.tsx | 3 +- apps/fronted/src/i18n/locales/zh.json | 12 +- 6 files changed, 219 insertions(+), 25 deletions(-) diff --git a/apps/fronted/src/app/app/workbench/[id]/page.tsx b/apps/fronted/src/app/app/workbench/[id]/page.tsx index 054bc15..5258f6c 100644 --- a/apps/fronted/src/app/app/workbench/[id]/page.tsx +++ b/apps/fronted/src/app/app/workbench/[id]/page.tsx @@ -229,7 +229,7 @@ export default function Home() { return (
edu.visible); return ( = ({ return ( { const styleCache = new Map(); @@ -70,9 +76,11 @@ const optimizeImages = async (element: HTMLElement) => { const PdfExport = () => { const [isExporting, setIsExporting] = useState(false); + const [isExportingJson, setIsExportingJson] = useState(false); const { activeResume } = useResumeStore(); const { globalSettings = {}, title } = activeResume || {}; const t = useTranslations("pdfExport"); + const printFrameRef = useRef(null); const handleExport = async () => { const exportStartTime = performance.now(); @@ -131,22 +139,200 @@ const PdfExport = () => { } }; + const handleJsonExport = () => { + try { + setIsExportingJson(true); + if (!activeResume) { + throw new Error("No active resume"); + } + + const jsonStr = JSON.stringify(activeResume, null, 2); + const blob = new Blob([jsonStr], { type: "application/json" }); + const url = window.URL.createObjectURL(blob); + const link = document.createElement("a"); + link.href = url; + link.download = `${title}.json`; + link.click(); + + window.URL.revokeObjectURL(url); + toast.success(t("toast.jsonSuccess")); + } catch (error) { + console.error("JSON export error:", error); + toast.error(t("toast.jsonError")); + } finally { + setIsExportingJson(false); + } + }; + + const handlePrint = () => { + if (!printFrameRef.current) { + console.error("Print frame not found"); + return; + } + + const resumeContent = document.getElementById("resume-preview"); + if (!resumeContent) { + console.error("Resume content not found"); + return; + } + + const actualContent = resumeContent.parentElement; + if (!actualContent) { + console.error("Actual content not found"); + return; + } + + console.log("Found content:", actualContent); + + const pagePadding = globalSettings?.pagePadding; + const iframeWindow = printFrameRef.current.contentWindow; + if (!iframeWindow) { + console.error("IFrame window not found"); + return; + } + + try { + iframeWindow.document.open(); + const htmlContent = ` + + + + Print Resume + + + + + + + `; + + iframeWindow.document.write(htmlContent); + iframeWindow.document.close(); + + setTimeout(() => { + try { + iframeWindow.focus(); + iframeWindow.print(); + } catch (error) { + console.error("Error print:", error); + } + }, 1000); + } catch (error) { + console.error("Error setting up print:", error); + } + }; + + const isLoading = isExporting || isExportingJson; + const loadingText = isExporting + ? t("button.exporting") + : isExportingJson + ? t("button.exportingJson") + : ""; + return ( -
- -
+ <> + + + + + + + + {t("button.exportPdf")} + + + + {t("button.exportJson")} + + + + {t("button.print")} + + + +