41 lines
913 B
TypeScript
41 lines
913 B
TypeScript
import type { Ref } from 'vue'
|
|
import { useApi } from '~/composables/useApi'
|
|
|
|
type PrintFrameRef = Ref<HTMLIFrameElement | null>
|
|
|
|
export const usePdfPrinter = () => {
|
|
const api = useApi()
|
|
|
|
const printPdf = async (url: string, frameRef: PrintFrameRef): Promise<void> => {
|
|
if (!import.meta.client) {
|
|
return
|
|
}
|
|
|
|
const frame = frameRef.value
|
|
if (!frame) {
|
|
return
|
|
}
|
|
|
|
// On charge le PDF en blob pour rester en same-origin dans l'iframe.
|
|
const blob = await api.getBlob(url)
|
|
const blobUrl = URL.createObjectURL(blob)
|
|
|
|
const tryPrint = () => {
|
|
frame.contentWindow?.focus()
|
|
frame.contentWindow?.print()
|
|
}
|
|
|
|
frame.onload = () => {
|
|
tryPrint()
|
|
// On libere l'URL blob apres l'impression.
|
|
setTimeout(() => URL.revokeObjectURL(blobUrl), 2000)
|
|
}
|
|
frame.src = blobUrl
|
|
setTimeout(tryPrint, 1200)
|
|
}
|
|
|
|
return {
|
|
printPdf
|
|
}
|
|
}
|