feat(pdf): rasterize SVG via canvg → PNG bytes

This commit is contained in:
Bastien Chanot committed 2026-05-18 21:43:58 +02:00
1 parent 3ea8f522c0
commit 55d9c32076
2 files changed
+45 -10

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
import { describe, it, expect, vi } from 'vitest'
import { rasterizeSvg } from '@/pdf/render/svgToPng'
vi.mock('canvg', () => ({
Canvg: { from: vi.fn(async () => ({ render: vi.fn(async () => {}) })) },
}))
describe('rasterizeSvg', () => {
it('returns a Uint8Array', async () => {
const canvas = document.createElement('canvas')
canvas.getContext = (() => ({})) as unknown as HTMLCanvasElement['getContext']
canvas.toBlob = ((cb: BlobCallback) => {
cb(new Blob([new Uint8Array([1, 2, 3])], { type: 'image/png' }))
}) as HTMLCanvasElement['toBlob']
vi.spyOn(document, 'createElement').mockImplementationOnce(() => canvas)
const out = await rasterizeSvg('<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10" viewBox="0 0 10 10"/>', 2)
expect(out).toBeInstanceOf(Uint8Array)
expect(out.length).toBeGreaterThan(0)
})
})