| import DOMPurify from 'dompurify'; |
| import { toast } from 'svelte-sonner'; |
|
|
| import { createNewNote } from '$lib/apis/notes'; |
|
|
| export const downloadPdf = async (note) => { |
| const [{ default: jsPDF }, { default: html2canvas }] = await Promise.all([ |
| import('jspdf'), |
| import('html2canvas-pro') |
| ]); |
|
|
| |
| const virtualWidth = 1024; |
| const virtualHeight = 1400; |
|
|
| |
| const html = DOMPurify.sanitize(note.data?.content?.html ?? ''); |
| const isDarkMode = document.documentElement.classList.contains('dark'); |
|
|
| let node; |
| if (html instanceof HTMLElement) { |
| node = html; |
| } else { |
| const virtualWidth = 800; |
|
|
| |
| node = document.createElement('div'); |
|
|
| |
| const titleNode = document.createElement('div'); |
| titleNode.textContent = note.title; |
| titleNode.style.fontSize = '24px'; |
| titleNode.style.fontWeight = 'medium'; |
| titleNode.style.paddingBottom = '20px'; |
| titleNode.style.color = isDarkMode ? 'white' : 'black'; |
| node.appendChild(titleNode); |
|
|
| const contentNode = document.createElement('div'); |
|
|
| contentNode.innerHTML = html; |
|
|
| node.appendChild(contentNode); |
|
|
| node.classList.add('text-black'); |
| node.classList.add('dark:text-white'); |
| node.style.width = `${virtualWidth}px`; |
| node.style.position = 'absolute'; |
| node.style.left = '-9999px'; |
| node.style.height = 'auto'; |
| node.style.padding = '40px 40px'; |
|
|
| console.log(node); |
| document.body.appendChild(node); |
| } |
|
|
| |
| const canvas = await html2canvas(node, { |
| useCORS: true, |
| backgroundColor: isDarkMode ? '#000' : '#fff', |
| scale: 2, |
| width: virtualWidth, |
| windowWidth: virtualWidth, |
| windowHeight: virtualHeight |
| }); |
|
|
| |
| if (!(html instanceof HTMLElement)) { |
| document.body.removeChild(node); |
| } |
|
|
| const imgData = canvas.toDataURL('image/jpeg', 0.7); |
|
|
| |
| const pdf = new jsPDF('p', 'mm', 'a4'); |
| const imgWidth = 210; |
| const pageWidthMM = 210; |
| const pageHeight = 297; |
| const pageHeightMM = 297; |
|
|
| if (isDarkMode) { |
| pdf.setFillColor(0, 0, 0); |
| pdf.rect(0, 0, pageWidthMM, pageHeightMM, 'F'); |
| } |
|
|
| |
| const imgHeight = (canvas.height * imgWidth) / canvas.width; |
| let heightLeft = imgHeight; |
| let position = 0; |
|
|
| pdf.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight); |
| heightLeft -= pageHeight; |
|
|
| |
| while (heightLeft > 0) { |
| position -= pageHeight; |
| pdf.addPage(); |
|
|
| if (isDarkMode) { |
| pdf.setFillColor(0, 0, 0); |
| pdf.rect(0, 0, pageWidthMM, pageHeightMM, 'F'); |
| } |
|
|
| pdf.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight); |
| heightLeft -= pageHeight; |
| } |
|
|
| pdf.save(`${note.title}.pdf`); |
| }; |
|
|
| export const createNoteHandler = async (title: string, md?: string, html?: string) => { |
| |
| const res = await createNewNote(localStorage.token, { |
| |
| title: title, |
| data: { |
| content: { |
| json: null, |
| html: html || md || '', |
| md: md || '' |
| } |
| }, |
| meta: null, |
| access_grants: [] |
| }).catch((error) => { |
| toast.error(`${error}`); |
| return null; |
| }); |
|
|
| if (res) { |
| return res; |
| } |
| }; |
|
|