PrintToPDF working
This commit is contained in:
parent
ce50026cc3
commit
4ec6b73fb3
|
|
@ -15,29 +15,17 @@ export const saveToPdf = async (editor: Editor) => {
|
||||||
const blob = await exportToBlob({
|
const blob = await exportToBlob({
|
||||||
editor,
|
editor,
|
||||||
ids: selectedIds,
|
ids: selectedIds,
|
||||||
format: "svg",
|
format: "png",
|
||||||
opts: {
|
opts: {
|
||||||
scale: 2,
|
scale: 2,
|
||||||
background: true,
|
background: true,
|
||||||
padding: 10,
|
padding: 10,
|
||||||
preserveAspectRatio: "xMidYMid slice",
|
preserveAspectRatio: "true",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!blob) return
|
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
|
// Create PDF with proper dimensions
|
||||||
const pdf = new jsPDF({
|
const pdf = new jsPDF({
|
||||||
orientation: selectionBounds.width > selectionBounds.height ? "l" : "p",
|
orientation: selectionBounds.width > selectionBounds.height ? "l" : "p",
|
||||||
|
|
@ -45,19 +33,25 @@ export const saveToPdf = async (editor: Editor) => {
|
||||||
format: [selectionBounds.width, selectionBounds.height],
|
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
|
// Add the image to the PDF
|
||||||
pdf.addImage(
|
pdf.addImage(
|
||||||
img,
|
imageData,
|
||||||
"SVG",
|
"PNG",
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
selectionBounds.width,
|
selectionBounds.width,
|
||||||
selectionBounds.height,
|
selectionBounds.height,
|
||||||
)
|
)
|
||||||
pdf.save("canvas-selection.pdf")
|
|
||||||
|
|
||||||
// Cleanup
|
pdf.save("canvas-selection.pdf")
|
||||||
URL.revokeObjectURL(url)
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to generate PDF:", error)
|
console.error("Failed to generate PDF:", error)
|
||||||
alert("Failed to generate PDF. Please try again.")
|
alert("Failed to generate PDF. Please try again.")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue