-
Notifications
You must be signed in to change notification settings - Fork 0
Export Single Graph to PDF 📂 - Will & Justin #48 #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5899aef
472d09a
be25d22
60dc2ac
7cc6830
939cc40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,13 +9,14 @@ export default function ConditionalLayout({ | |
| }: { | ||
| children: React.ReactNode; | ||
| }) { | ||
| const pathname = usePathname(); | ||
| const isAuthPage = pathname === "/signin"; | ||
| //const pathname = usePathname(); | ||
|
|
||
| //const isAuthPage = pathname === "/signin"; | ||
|
|
||
| // If on auth pages, just render children without sidebar | ||
| if (isAuthPage) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why |
||
| return <main className="w-full h-full">{children}</main>; | ||
| } | ||
| // if (isAuthPage) { | ||
| // return <main className="w-full h-full">{children}</main>; | ||
| // } | ||
|
|
||
| // Otherwise, render with sidebar and responsive layout | ||
| return ( | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why |
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: call this file |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /*************************************************************** | ||
| * | ||
| * /src/lib/export-to-pdf.ts | ||
| * | ||
| * Author: Will and Justin | ||
| * Date: 2/1/2025 | ||
| * | ||
| * Summary: Export an svg graph as a pdf | ||
| **************************************************************/ | ||
|
|
||
| import React from "react"; | ||
| import html2canvas from "html2canvas-pro"; | ||
| import jsPDF from "jspdf"; | ||
|
|
||
| export async function downloadGraph( | ||
| svgRef: React.RefObject<SVGSVGElement | null>, | ||
| ) { | ||
| const newSVG = getClonedSvg(svgRef); | ||
| if (!newSVG) return; | ||
|
|
||
| // Get SVG dimensions from viewBox or attributes with fallbacks | ||
| const viewBox = newSVG.getAttribute("viewBox"); | ||
| let svgWidth = 1000; | ||
| let svgHeight = 400; | ||
| let aspectRatio = svgHeight / svgWidth; | ||
|
|
||
| if (viewBox) { | ||
| const viewBoxValues = viewBox.split(" "); | ||
| svgWidth = parseFloat(viewBoxValues[2]) || 1000; | ||
| svgHeight = parseFloat(viewBoxValues[3]) || 400; | ||
| } else { | ||
| const width = newSVG.getAttribute("width"); | ||
| const height = newSVG.getAttribute("height"); | ||
| if (width) svgWidth = parseFloat(width) || 1000; | ||
| if (height) svgHeight = parseFloat(height) || 400; | ||
| } | ||
|
|
||
| aspectRatio = svgHeight / svgWidth; | ||
|
|
||
| // Adds the svg element to the page temporarily (offscreen) | ||
| const wrapper = document.createElement("div"); | ||
| wrapper.style.position = "fixed"; | ||
| wrapper.style.left = "-9999px"; | ||
| wrapper.style.top = "-9999px"; | ||
| wrapper.style.width = `${svgWidth}px`; | ||
| wrapper.style.height = `${svgHeight}px`; | ||
| wrapper.appendChild(newSVG); | ||
| document.body.append(wrapper); | ||
|
|
||
| const canvas = await html2canvas(wrapper, { | ||
| backgroundColor: "#fff", | ||
| scale: 2, | ||
| }); | ||
|
|
||
| const pdf = new jsPDF(); | ||
| const pdfWidth = pdf.internal.pageSize.getWidth(); | ||
| const pdfHeight = pdfWidth * aspectRatio; | ||
|
|
||
| // Add image with proper dimensions that match PDF width while preserving aspect ratio | ||
| pdf.addImage(canvas, "JPEG", 0, 0, pdfWidth, pdfHeight); | ||
| pdf.save("graph.pdf"); | ||
|
|
||
| document.body.removeChild(wrapper); | ||
| } | ||
|
|
||
| export function getClonedSvg( | ||
| svgRef: React.RefObject<SVGSVGElement | null>, | ||
| ): SVGSVGElement | null { | ||
| const original = svgRef.current; | ||
| if (!original) return null; | ||
|
|
||
| //Creates and returns a clone of the svg element passed in | ||
| const clone = original.cloneNode(true) as SVGSVGElement; | ||
|
|
||
| return clone; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why install a dep if you're not going to use it