diff --git a/components/Chat/Chat.tsx b/components/Chat/Chat.tsx index fa6b69d..5c8f36f 100644 --- a/components/Chat/Chat.tsx +++ b/components/Chat/Chat.tsx @@ -1,4 +1,4 @@ -import { IconClearAll, IconSettings } from '@tabler/icons-react'; +import { IconClearAll, IconSettings, IconPdf } from '@tabler/icons-react'; import { MutableRefObject, memo, @@ -34,6 +34,9 @@ import { SystemPrompt } from './SystemPrompt'; import { TemperatureSlider } from './Temperature'; import { MemoizedChatMessage } from './MemoizedChatMessage'; +import {jsPDF} from "jspdf"; +import html2canvas from "html2canvas"; + interface Props { stopConversationRef: MutableRefObject; } @@ -300,6 +303,34 @@ export const Chat = memo(({ stopConversationRef }: Props) => { } }; + const onPdf = () => { + if (chatContainerRef.current === null) { + return; + } + else { + chatContainerRef.current.classList.remove('max-h-full') + html2canvas(chatContainerRef.current).then((canvas) => { + if (chatContainerRef.current) { + chatContainerRef.current.classList.add('max-h-full') + } + const imgData = canvas.toDataURL('image/png'); + const orientation = canvas.width > canvas.height ? "l" : "p"; + const pixelRatio = window.devicePixelRatio > 2 ? window.devicePixelRatio : 2 + const pdf = new jsPDF( + orientation, + "pt", + [canvas.width / pixelRatio, canvas.height / pixelRatio], + true, + ); + var pdfWidth = pdf.internal.pageSize.getWidth(); + var pdfHeight = pdf.internal.pageSize.getHeight(); + pdf.addImage(imgData, "PNG", 0, 0, pdfWidth, pdfHeight, "", "FAST"); + const title = `${selectedConversation?.name || 'conversation'}.pdf` + pdf.save(title) + }) + } + }; + const scrollDown = () => { if (autoScrollEnabled) { messagesEndRef.current?.scrollIntoView(true); @@ -454,6 +485,12 @@ export const Chat = memo(({ stopConversationRef }: Props) => { > + {showSettings && (
diff --git a/package.json b/package.json index ccb2cdb..ce622e4 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,9 @@ "@dqbd/tiktoken": "^1.0.2", "@tabler/icons-react": "^2.9.0", "eventsource-parser": "^0.1.0", + "html2canvas": "^1.4.1", "i18next": "^22.4.13", + "jspdf": "^2.5.1", "next": "13.2.4", "next-i18next": "^13.2.2", "openai": "^3.2.1",