PrintToPDF working

This commit is contained in:
Jeff Emmett 2024-12-08 13:39:07 -05:00
parent ce50026cc3
commit 4ec6b73fb3
1 changed files with 13 additions and 19 deletions

View File

@ -15,29 +15,17 @@ export const saveToPdf = async (editor: Editor) => {
const blob = await exportToBlob({
editor,
ids: selectedIds,
format: "svg",
format: "png",
opts: {
scale: 2,
background: true,
padding: 10,
preserveAspectRatio: "xMidYMid slice",
preserveAspectRatio: "true",
},
})
if (!blob) return
// Convert blob to data URL
const url = URL.createObjectURL(blob)
// Create image from blob
const img = new Image()
img.src = url
await new Promise((resolve, reject) => {
img.onload = resolve
img.onerror = reject
})
// Create PDF with proper dimensions
const pdf = new jsPDF({
orientation: selectionBounds.width > selectionBounds.height ? "l" : "p",
@ -45,19 +33,25 @@ export const saveToPdf = async (editor: Editor) => {
format: [selectionBounds.width, selectionBounds.height],
})
// Convert blob directly to base64
const reader = new FileReader()
const imageData = await new Promise<string>((resolve, reject) => {
reader.onload = () => resolve(reader.result as string)
reader.onerror = reject
reader.readAsDataURL(blob)
})
// Add the image to the PDF
pdf.addImage(
img,
"SVG",
imageData,
"PNG",
0,
0,
selectionBounds.width,
selectionBounds.height,
)
pdf.save("canvas-selection.pdf")
// Cleanup
URL.revokeObjectURL(url)
pdf.save("canvas-selection.pdf")
} catch (error) {
console.error("Failed to generate PDF:", error)
alert("Failed to generate PDF. Please try again.")