Skip to content

Commit

Permalink
feat: Derek/export png (#95)
Browse files Browse the repository at this point in the history
* Attempting to use more lightweight version

* Did not work.

* This is not what I wanted

* The image saves correctly. Needs padding

* Padding !!

* Removed downloadjs

* Padding more
  • Loading branch information
DereC4 authored and doprz committed Mar 6, 2024
1 parent a30fecf commit 58c2b46
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"clsx": "^2.1.0",
"highcharts": "^11.3.0",
"highcharts-react-official": "^3.2.1",
"html-to-image": "^1.11.11",
"html2canvas": "^1.4.1",
"react": "^18.2.0",
"react-devtools-core": "^5.0.0",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 39 additions & 10 deletions src/views/components/common/CalendarGrid/CalendarGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { useRef } from 'react';
import html2canvas from 'html2canvas';
// import html2canvas from 'html2canvas';
import * as htmlToImage from 'html-to-image';
import { toPng, toJpeg, toBlob, toPixelData, toSvg } from 'html-to-image';
import { DAY_MAP } from 'src/shared/types/CourseMeeting';
import { CalendarGridCourse } from 'src/views/hooks/useFlattenedCourseSchedule';
import calIcon from 'src/assets/icons/cal.svg';
Expand Down Expand Up @@ -38,15 +40,42 @@ function CalendarGrid({ courseCells, saturdayClass }: React.PropsWithChildren<Pr
const calendarRef = useRef(null); // Create a ref for the calendar grid

const saveAsPNG = () => {
if (calendarRef.current) {
html2canvas(calendarRef.current).then(canvas => {
// Create an a element to trigger download
const a = document.createElement('a');
a.href = canvas.toDataURL('image/png');
a.download = 'calendar.png';
a.click();
});
}
// if (calendarRef.current) {
// html2canvas(calendarRef.current).then(canvas => {
// // Create an a element to trigger download
// const a = document.createElement('a');
// a.href = canvas.toDataURL('image/png');
// a.download = 'calendar.png';
// a.click();
// });
// }
htmlToImage.toPng(calendarRef.current, {
backgroundColor: "white",
style: {
background: "white",
marginTop: "20px",
marginBottom: "20px",
marginRight: "20px",
marginLeft: "20px",}})
.then(function (dataUrl) {
var img = new Image();
img.src = dataUrl;
fetch(dataUrl)
.then(response => response.blob())
.then(blob => {
const href = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = href;
link.download = 'my-schedule.png';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
})
.catch(error => console.error('Error downloading file:', error));
})
.catch(function (error) {
console.error('oops, something went wrong!', error);
});
};

return (
Expand Down

0 comments on commit 58c2b46

Please sign in to comment.